package storage import ( "errors" "io" "os" "github.com/studio-b12/gowebdav" ) type WebDav struct { client *gowebdav.Client } func NewWebDavStorage(root string, username string, password string) *WebDav { c := gowebdav.NewClient(root, username, password) return &WebDav{client: c} } func (wd *WebDav) Mkdir(path string) error { if err := wd.client.MkdirAll(path, 0644); err != nil { return err } return nil } func (wd *WebDav) Put(path string, f io.Reader) error { return wd.client.WriteStream(path, f, 0644) } func (wd *WebDav) Move(src string, dest string) error { if src != dest { if err := wd.client.Rename(src, dest, true); err != nil { return nil } } return nil } func (wd *WebDav) Exists(path string) (bool, error) { _, err := wd.client.Stat(path) if err == nil { return true, nil } if errors.Is(err, os.ErrNotExist) { return false, nil } return false, err } func (wd *WebDav) Copy(src string, dest string) error { if src != dest { if err := wd.client.Copy(src, dest, true); err != nil { return nil } } return nil } func (wd *WebDav) Get(path string) (io.Reader, error) { return wd.client.ReadStream(path) } func (wd *WebDav) List(path string) ([]string, error) { files, err := wd.client.ReadDir(path) if err != nil { return nil, err } r := make([]string, len(files)) for i, v := range files { r[i] = v.Name() } return r, nil } func (wd *WebDav) Walk(folder string, walkMode WalkMode) <-chan string { c := make(chan string) go func(string, chan string) { var queue []string queue = append(queue, folder) for len(queue) > 0 { f := queue[0] queue = queue[1:] files, _ := wd.client.ReadDir(f) for _, fileInfo := range files { if fileInfo.IsDir() { queue = append(queue, f+"/"+fileInfo.Name()) } switch walkMode { case Folder: if fileInfo.IsDir() { c <- f + "/" + fileInfo.Name() } case File: if !fileInfo.IsDir() { c <- f + "/" + fileInfo.Name() } case FileFolder: c <- f + "/" + fileInfo.Name() } } } close(c) }(folder, c) return c }