2023-11-27 21:44:38 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2023-12-01 18:36:06 +01:00
|
|
|
"fmt"
|
2023-12-14 20:25:24 +01:00
|
|
|
"github.com/openziti/zrok/environment/env_core"
|
2023-12-01 17:44:50 +01:00
|
|
|
"github.com/openziti/zrok/util/sync/webdavClient"
|
2023-11-27 21:44:38 +01:00
|
|
|
"github.com/pkg/errors"
|
2023-11-27 23:31:13 +01:00
|
|
|
"io"
|
2023-12-14 20:25:24 +01:00
|
|
|
"net/url"
|
2023-11-27 23:31:13 +01:00
|
|
|
"os"
|
2023-11-27 21:44:38 +01:00
|
|
|
"path/filepath"
|
2023-11-28 03:11:19 +01:00
|
|
|
"time"
|
2023-11-27 21:44:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebDAVTargetConfig struct {
|
2023-12-14 20:25:24 +01:00
|
|
|
URL *url.URL
|
2023-11-27 21:44:38 +01:00
|
|
|
Username string
|
|
|
|
Password string
|
2023-12-14 20:25:24 +01:00
|
|
|
Root env_core.Root
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebDAVTarget struct {
|
2023-12-15 18:10:35 +01:00
|
|
|
cfg *WebDAVTargetConfig
|
|
|
|
c *webdavClient.Client
|
|
|
|
isDir bool
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebDAVTarget(cfg *WebDAVTargetConfig) (*WebDAVTarget, error) {
|
2023-12-14 20:25:24 +01:00
|
|
|
c, err := webdavClient.NewZrokClient(cfg.URL, cfg.Root, webdavClient.NewAutoAuth(cfg.Username, cfg.Password))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-27 21:44:38 +01:00
|
|
|
if err := c.Connect(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error connecting to webdav target")
|
|
|
|
}
|
2023-12-15 18:10:35 +01:00
|
|
|
return &WebDAVTarget{cfg: cfg, c: c}, nil
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebDAVTarget) Inventory() ([]*Object, error) {
|
2023-12-15 18:10:35 +01:00
|
|
|
fi, err := t.c.Stat("")
|
2024-01-03 22:58:32 +01:00
|
|
|
|
|
|
|
if fi != nil && !fi.IsDir() {
|
2023-12-15 18:10:35 +01:00
|
|
|
t.isDir = false
|
|
|
|
return []*Object{{
|
|
|
|
Path: fi.Name(),
|
|
|
|
Size: fi.Size(),
|
|
|
|
Modified: fi.ModTime(),
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.isDir = true
|
2023-11-27 21:44:38 +01:00
|
|
|
tree, err := t.recurse("", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:10:35 +01:00
|
|
|
func (t *WebDAVTarget) IsDir() bool {
|
|
|
|
return t.isDir
|
|
|
|
}
|
|
|
|
|
2023-11-27 21:44:38 +01:00
|
|
|
func (t *WebDAVTarget) recurse(path string, tree []*Object) ([]*Object, error) {
|
|
|
|
files, err := t.c.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
sub := filepath.ToSlash(filepath.Join(path, f.Name()))
|
|
|
|
if f.IsDir() {
|
|
|
|
tree, err = t.recurse(sub, tree)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-01 17:44:50 +01:00
|
|
|
if v, ok := f.(webdavClient.File); ok {
|
2023-11-27 21:44:38 +01:00
|
|
|
tree = append(tree, &Object{
|
|
|
|
Path: filepath.ToSlash(filepath.Join(path, f.Name())),
|
|
|
|
Size: v.Size(),
|
|
|
|
Modified: v.ModTime(),
|
|
|
|
ETag: v.ETag(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tree, nil
|
|
|
|
}
|
2023-11-27 23:31:13 +01:00
|
|
|
|
|
|
|
func (t *WebDAVTarget) ReadStream(path string) (io.ReadCloser, error) {
|
2023-12-15 18:10:35 +01:00
|
|
|
if t.isDir {
|
|
|
|
return t.c.ReadStream(path)
|
|
|
|
}
|
|
|
|
return t.c.ReadStream("")
|
2023-11-27 23:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebDAVTarget) WriteStream(path string, stream io.Reader, mode os.FileMode) error {
|
|
|
|
return t.c.WriteStream(path, stream, mode)
|
|
|
|
}
|
2023-11-28 03:11:19 +01:00
|
|
|
|
|
|
|
func (t *WebDAVTarget) SetModificationTime(path string, mtime time.Time) error {
|
2023-12-01 19:53:36 +01:00
|
|
|
modtimeUnix := mtime.Unix()
|
|
|
|
body := "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
|
|
|
|
"<propertyupdate xmlns=\"DAV:\" xmlns:z=\"zrok:\"><set><prop><z:lastmodified>" +
|
|
|
|
fmt.Sprintf("%d", modtimeUnix) +
|
|
|
|
"</z:lastmodified></prop></set></propertyupdate>"
|
|
|
|
if err := t.c.Proppatch(path, body, nil, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-28 03:11:19 +01:00
|
|
|
return nil
|
|
|
|
}
|