2020-03-27 11:01:41 +01:00
|
|
|
/*
|
|
|
|
Package entity implementing sharing with WebDAV servers.
|
|
|
|
|
|
|
|
Additional information can be found in our Developer Guide:
|
|
|
|
|
|
|
|
https://github.com/photoprism/photoprism/wiki
|
|
|
|
*/
|
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/studio-b12/gowebdav"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = event.Log
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
client *gowebdav.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect creates a new WebDAV client.
|
|
|
|
func Connect(url, user, pass string) Client {
|
|
|
|
clt := gowebdav.NewClient(url, user, pass)
|
|
|
|
|
|
|
|
result := Client{client: clt}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
// Files returns all files in path as string slice.
|
2020-03-31 17:26:25 +02:00
|
|
|
func (c Client) Files(dir string) (result []string, err error) {
|
|
|
|
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-03-31 17:26:25 +02:00
|
|
|
result = append(result, fmt.Sprintf("%s/%s", dir, file.Name()))
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directories returns all sub directories in path as string slice.
|
2020-03-31 17:26:25 +02:00
|
|
|
func (c Client) Directories(root string, recursive bool) (result []string, err error) {
|
|
|
|
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-03-31 17:26:25 +02:00
|
|
|
dir := fmt.Sprintf("%s/%s", root, file.Name())
|
|
|
|
|
2020-03-27 11:56:24 +01:00
|
|
|
result = append(result, dir)
|
|
|
|
|
|
|
|
if recursive {
|
|
|
|
subDirs, err := c.Directories(dir, true)
|
|
|
|
|
|
|
|
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.
|
|
|
|
func (c Client) Download(from, to string) error {
|
|
|
|
dir := path.Dir(to)
|
|
|
|
dirInfo, err := os.Stat(dir)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// Create directory
|
|
|
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("webdav: can't create %s (%s)", dir, err)
|
|
|
|
}
|
|
|
|
} else if !dirInfo.IsDir() {
|
|
|
|
return fmt.Errorf("webdav: %s is not a directory", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := c.client.Read(from)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(to, bytes, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadDir downloads all files from a remote to a local directory.
|
|
|
|
func (c Client) DownloadDir(from, to string, recursive bool) (errs []error) {
|
|
|
|
files, err := c.Files(from)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
dest := to + string(os.PathSeparator) + file
|
|
|
|
|
|
|
|
if _, err := os.Stat(dest); err == nil {
|
|
|
|
// File exists
|
|
|
|
msg := fmt.Errorf("webdav: %s exists", dest)
|
|
|
|
errs = append(errs, msg)
|
|
|
|
log.Error(msg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Download(file, dest); err != nil {
|
|
|
|
msg := fmt.Errorf("webdav: %s", err)
|
|
|
|
errs = append(errs, msg)
|
|
|
|
log.Error(msg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !recursive {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:56:24 +01:00
|
|
|
dirs, err := c.Directories(from, false)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
errs = append(errs, c.DownloadDir(dir, to, true)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if dir == "" || dir == "/" {
|
|
|
|
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.
|
|
|
|
func (c Client) Upload(from, to string) error {
|
|
|
|
file, err := os.Open(from)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
return c.client.WriteStream(to, file, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a single file or directory on a remote server.
|
|
|
|
func (c Client) Delete(path string) error {
|
|
|
|
return c.client.Remove(path)
|
|
|
|
}
|