-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56ecc33
commit 0832f28
Showing
3 changed files
with
84 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package sync | ||
|
||
import "time" | ||
|
||
type Object struct { | ||
Path string | ||
Size int64 | ||
Modified time.Time | ||
ETag string | ||
} | ||
|
||
type Target interface { | ||
Inventory() ([]*Object, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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 | ||
} |