mirror of
https://github.com/openziti/zrok.git
synced 2024-11-23 00:23:48 +01:00
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
|
package sync
|
||
|
|
||
|
import (
|
||
|
"github.com/pkg/errors"
|
||
|
"github.com/studio-b12/gowebdav"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
type WebDAVTargetConfig struct {
|
||
|
URL string
|
||
|
Username string
|
||
|
Password string
|
||
|
}
|
||
|
|
||
|
type WebDAVTarget struct {
|
||
|
c *gowebdav.Client
|
||
|
}
|
||
|
|
||
|
func NewWebDAVTarget(cfg *WebDAVTargetConfig) (*WebDAVTarget, error) {
|
||
|
c := gowebdav.NewClient(cfg.URL, cfg.Username, cfg.Password)
|
||
|
if err := c.Connect(); err != nil {
|
||
|
return nil, errors.Wrap(err, "error connecting to webdav target")
|
||
|
}
|
||
|
return &WebDAVTarget{c: c}, nil
|
||
|
}
|
||
|
|
||
|
func (t *WebDAVTarget) Inventory() ([]*Object, error) {
|
||
|
tree, err := t.recurse("", nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return tree, nil
|
||
|
}
|
||
|
|
||
|
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 {
|
||
|
if v, ok := f.(gowebdav.File); ok {
|
||
|
tree = append(tree, &Object{
|
||
|
Path: filepath.ToSlash(filepath.Join(path, f.Name())),
|
||
|
Size: v.Size(),
|
||
|
Modified: v.ModTime(),
|
||
|
ETag: v.ETag(),
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return tree, nil
|
||
|
}
|