2020-03-27 11:01:41 +01:00
|
|
|
/*
|
2020-06-23 13:44:14 +02:00
|
|
|
|
|
|
|
Package webdav implements sharing with WebDAV servers.
|
|
|
|
|
2022-02-20 17:36:51 +01:00
|
|
|
Copyright (c) 2018 - 2022 Michael Mayer <hello@photoprism.app>
|
2020-06-23 13:44:14 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2022-02-21 15:30:18 +01:00
|
|
|
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
|
|
|
|
<https://docs.photoprism.app/license/agpl>
|
2020-06-23 13:44:14 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
2022-02-21 15:30:18 +01:00
|
|
|
The AGPL is supplemented by our Trademark and Brand Guidelines,
|
|
|
|
which describe how our Brand Assets may be used:
|
|
|
|
<https://photoprism.app/trademark>
|
2020-06-23 13:44:14 +02:00
|
|
|
|
2022-02-21 15:30:18 +01:00
|
|
|
Feel free to send an e-mail to hello@photoprism.app if you have questions,
|
2020-06-23 13:44:14 +02:00
|
|
|
want to support our work, or just want to say hello.
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
Additional information can be found in our Developer Guide:
|
2022-02-27 17:32:54 +01:00
|
|
|
<https://docs.photoprism.app/developer-guide/>
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-11-09 11:42:10 +01:00
|
|
|
"runtime/debug"
|
2020-04-24 14:11:17 +02:00
|
|
|
"time"
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
"github.com/studio-b12/gowebdav"
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-02 18:17:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2022-03-27 21:37:11 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-03-27 11:01:41 +01:00
|
|
|
)
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Global log instance.
|
2020-03-27 11:01:41 +01:00
|
|
|
var log = event.Log
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
type Timeout string
|
|
|
|
|
|
|
|
// Request Timeout options.
|
|
|
|
const (
|
|
|
|
TimeoutHigh Timeout = "high" // 120 * Second
|
|
|
|
TimeoutDefault Timeout = "" // 60 * Second
|
|
|
|
TimeoutMedium Timeout = "medium" // 60 * Second
|
|
|
|
TimeoutLow Timeout = "low" // 30 * Second
|
|
|
|
TimeoutNone Timeout = "none" // 0
|
|
|
|
)
|
|
|
|
|
|
|
|
// Second represents a second on which other timeouts are based.
|
|
|
|
const Second = time.Second
|
2020-12-05 01:24:33 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// MaxRequestDuration is the maximum request duration e.g. for recursive retrieval of large remote directory structures.
|
|
|
|
const MaxRequestDuration = 30 * time.Minute
|
|
|
|
|
|
|
|
// Durations maps Timeout options to specific time durations.
|
|
|
|
var Durations = map[Timeout]time.Duration{
|
|
|
|
TimeoutHigh: 120 * Second,
|
|
|
|
TimeoutDefault: 60 * Second,
|
|
|
|
TimeoutMedium: 60 * Second,
|
|
|
|
TimeoutLow: 30 * Second,
|
|
|
|
TimeoutNone: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client represents a gowebdav.Client wrapper.
|
2020-03-27 11:01:41 +01:00
|
|
|
type Client struct {
|
2022-03-27 21:37:11 +02:00
|
|
|
client *gowebdav.Client
|
|
|
|
timeout Timeout
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:08:49 +02:00
|
|
|
// New creates a new WebDAV client.
|
2022-03-27 21:37:11 +02:00
|
|
|
func New(url, user, pass string, timeout Timeout) Client {
|
|
|
|
// Create a new gowebdav.Client instance.
|
|
|
|
client := gowebdav.NewClient(url, user, pass)
|
2020-04-24 14:11:17 +02:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Create a new gowebdav.Client wrapper.
|
2020-12-05 01:24:33 +01:00
|
|
|
result := Client{
|
2022-03-27 21:37:11 +02:00
|
|
|
client: client,
|
|
|
|
timeout: timeout,
|
2020-12-05 01:24:33 +01:00
|
|
|
}
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:56:24 +01:00
|
|
|
func (c Client) readDir(path string) ([]os.FileInfo, error) {
|
|
|
|
if path == "" {
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.client.ReadDir(path)
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
// Files returns all files in a directory as string slice.
|
2020-04-02 18:17:07 +02:00
|
|
|
func (c Client) Files(dir string) (result fs.FileInfos, err error) {
|
2021-11-09 11:42:10 +01:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("webdav: %s (panic while listing files)\nstack: %s", r, debug.Stack())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-31 17:26:25 +02:00
|
|
|
files, err := c.readDir(dir)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2020-03-27 18:17:07 +01:00
|
|
|
if !file.Mode().IsRegular() {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-02 18:17:07 +02:00
|
|
|
|
|
|
|
info := fs.NewFileInfo(file, dir)
|
|
|
|
|
|
|
|
result = append(result, info)
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Directories returns all subdirectories in a path as string slice.
|
2020-12-05 01:24:33 +01:00
|
|
|
func (c Client) Directories(root string, recursive bool, timeout time.Duration) (result fs.FileInfos, err error) {
|
|
|
|
start := time.Now()
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
if timeout == 0 {
|
|
|
|
timeout = Durations[c.timeout]
|
|
|
|
}
|
|
|
|
|
2020-12-05 01:24:33 +01:00
|
|
|
result, err = c.fetchDirs(root, recursive, start, timeout)
|
|
|
|
|
|
|
|
if time.Now().Sub(start) >= timeout {
|
|
|
|
log.Warnf("webdav: read dir timeout reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchDirs recursively fetches all directories until the timeout is reached.
|
|
|
|
func (c Client) fetchDirs(root string, recursive bool, start time.Time, timeout time.Duration) (result fs.FileInfos, err error) {
|
2020-03-31 17:26:25 +02:00
|
|
|
files, err := c.readDir(root)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:26:25 +02:00
|
|
|
if root == "/" {
|
|
|
|
root = ""
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
for _, file := range files {
|
2020-03-27 18:17:07 +01:00
|
|
|
if !file.Mode().IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-27 11:56:24 +01:00
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
info := fs.NewFileInfo(file, root)
|
2020-03-31 17:26:25 +02:00
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
result = append(result, info)
|
2020-03-27 11:56:24 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
if recursive && (timeout < time.Second || time.Now().Sub(start) < timeout) {
|
2020-12-05 01:24:33 +01:00
|
|
|
subDirs, err := c.fetchDirs(info.Abs, true, start, timeout)
|
2020-03-27 11:56:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, subDirs...)
|
|
|
|
}
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download downloads a single file to the given location.
|
2021-11-09 11:42:10 +01:00
|
|
|
func (c Client) Download(from, to string, force bool) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2022-03-27 21:37:11 +02:00
|
|
|
log.Errorf("webdav: %s (panic)\nstack: %s", r, sanitize.Log(from))
|
|
|
|
err = fmt.Errorf("webdav: unexpected error while downloading %s", sanitize.Log(from))
|
2021-11-09 11:42:10 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Skip if file already exists.
|
2020-04-06 16:34:29 +02:00
|
|
|
if _, err := os.Stat(to); err == nil && !force {
|
2022-03-27 21:37:11 +02:00
|
|
|
return fmt.Errorf("webdav: download skipped, %s already exists", sanitize.Log(to))
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
dir := path.Dir(to)
|
|
|
|
dirInfo, err := os.Stat(dir)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-03-27 21:37:11 +02:00
|
|
|
// Create local storage path.
|
2020-03-27 11:01:41 +01:00
|
|
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
2022-03-27 21:37:11 +02:00
|
|
|
return fmt.Errorf("webdav: cannot create folder %s (%s)", sanitize.Log(dir), err)
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
} else if !dirInfo.IsDir() {
|
2022-03-27 21:37:11 +02:00
|
|
|
return fmt.Errorf("webdav: %s is not a folder", sanitize.Log(dir))
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
var bytes []byte
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Start download.
|
2021-11-09 11:42:10 +01:00
|
|
|
bytes, err = c.client.Read(from)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Error?
|
2020-03-27 11:01:41 +01:00
|
|
|
if err != nil {
|
2022-03-27 21:37:11 +02:00
|
|
|
log.Errorf("webdav: %s", sanitize.Log(err.Error()))
|
|
|
|
return fmt.Errorf("webdav: failed downloading %s", sanitize.Log(from))
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Write data to file and return.
|
|
|
|
return os.WriteFile(to, bytes, os.ModePerm)
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadDir downloads all files from a remote to a local directory.
|
2020-04-06 16:34:29 +02:00
|
|
|
func (c Client) DownloadDir(from, to string, recursive, force bool) (errs []error) {
|
2020-03-27 11:01:41 +01:00
|
|
|
files, err := c.Files(from)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2020-04-02 18:17:07 +02:00
|
|
|
dest := to + string(os.PathSeparator) + file.Abs
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
if _, err = os.Stat(dest); err == nil {
|
|
|
|
// File already exists.
|
2022-03-27 21:37:11 +02:00
|
|
|
msg := fmt.Errorf("webdav: %s already exists", sanitize.Log(dest))
|
2021-11-09 11:42:10 +01:00
|
|
|
log.Warn(msg)
|
2022-03-27 21:37:11 +02:00
|
|
|
errs = append(errs, msg)
|
2020-03-27 11:01:41 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
if err = c.Download(file.Abs, dest, force); err != nil {
|
|
|
|
// Failed to download file.
|
|
|
|
errs = append(errs, err)
|
|
|
|
log.Error(err)
|
2020-03-27 11:01:41 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !recursive {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
dirs, err := c.Directories(from, false, MaxRequestDuration)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
for _, dir := range dirs {
|
2020-04-06 16:34:29 +02:00
|
|
|
errs = append(errs, c.DownloadDir(dir.Abs, to, true, force)...)
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
// CreateDir recursively creates directories if they don't exist.
|
|
|
|
func (c Client) CreateDir(dir string) error {
|
2020-04-07 12:51:01 +02:00
|
|
|
if dir == "" || dir == "/" || dir == "." {
|
2020-04-01 12:00:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.client.MkdirAll(dir, os.ModePerm)
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
// Upload uploads a single file to the remote server.
|
2021-11-09 11:42:10 +01:00
|
|
|
func (c Client) Upload(from, to string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("webdav: %s (panic while uploading)\nstack: %s", r, debug.Stack())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
file, err := os.Open(from)
|
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
if err != nil || file == nil {
|
2020-03-27 11:01:41 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:42:10 +01:00
|
|
|
defer func(file *os.File) {
|
|
|
|
_ = file.Close()
|
|
|
|
}(file)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
return c.client.WriteStream(to, file, os.ModePerm)
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a single file or directory on a remote server.
|
|
|
|
func (c Client) Delete(path string) error {
|
|
|
|
return c.client.Remove(path)
|
|
|
|
}
|