webdav: move recursive PROPFIND to backend

Closes: https://github.com/emersion/go-webdav/issues/22
This commit is contained in:
Simon Ser 2020-01-22 10:41:20 +01:00
parent 307a998a46
commit 72c96af206
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 52 additions and 48 deletions

View File

@ -25,6 +25,15 @@ func (fs LocalFileSystem) path(name string) (string, error) {
return filepath.Join(string(fs), filepath.FromSlash(name)), nil return filepath.Join(string(fs), filepath.FromSlash(name)), nil
} }
func (fs LocalFileSystem) href(path string) (string, error) {
rel, err := filepath.Rel(string(fs), path)
if err != nil {
return "", err
}
href := "/" + filepath.ToSlash(rel)
return href, nil
}
func (fs LocalFileSystem) Open(name string) (io.ReadCloser, error) { func (fs LocalFileSystem) Open(name string) (io.ReadCloser, error) {
p, err := fs.path(name) p, err := fs.path(name)
if err != nil { if err != nil {
@ -56,27 +65,31 @@ func (fs LocalFileSystem) Stat(name string) (*FileInfo, error) {
return fileInfoFromOS(name, fi), nil return fileInfoFromOS(name, fi), nil
} }
func (fs LocalFileSystem) Readdir(name string) ([]FileInfo, error) { func (fs LocalFileSystem) Readdir(name string, recursive bool) ([]FileInfo, error) {
p, err := fs.path(name) p, err := fs.path(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
fis, err := f.Readdir(-1) var l []FileInfo
err = filepath.Walk(p, func(p string, fi os.FileInfo, err error) error {
if err != nil { if err != nil {
return nil, err return err
} }
l := make([]FileInfo, len(fis)) href, err := fs.href(p)
for i, fi := range fis { if err != nil {
l[i] = *fileInfoFromOS(path.Join(name, fi.Name()), fi) return err
} }
return l, nil
l = append(l, *fileInfoFromOS(href, fi))
if !recursive && fi.IsDir() {
return filepath.SkipDir
}
return nil
})
return l, err
} }
func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) { func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {

View File

@ -14,7 +14,7 @@ import (
type FileSystem interface { type FileSystem interface {
Open(name string) (io.ReadCloser, error) Open(name string) (io.ReadCloser, error)
Stat(name string) (*FileInfo, error) Stat(name string) (*FileInfo, error)
Readdir(name string) ([]FileInfo, error) Readdir(name string, recursive bool) ([]FileInfo, error)
Create(name string) (io.WriteCloser, error) Create(name string) (io.WriteCloser, error)
RemoveAll(name string) error RemoveAll(name string) error
Mkdir(name string) error Mkdir(name string) error
@ -106,6 +106,8 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
} }
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) { func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
// TODO: use partial error Response on error
fi, err := b.FileSystem.Stat(r.URL.Path) fi, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, &internal.HTTPError{Code: http.StatusNotFound, Err: err} return nil, &internal.HTTPError{Code: http.StatusNotFound, Err: err}
@ -114,41 +116,30 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i
} }
var resps []internal.Response var resps []internal.Response
if err := b.propfind(propfind, fi, depth, &resps); err != nil { if depth != internal.DepthZero && fi.IsDir {
children, err := b.FileSystem.Readdir(r.URL.Path, depth == internal.DepthInfinity)
if err != nil {
return nil, err return nil, err
} }
return internal.NewMultistatus(resps...), nil resps = make([]internal.Response, len(children))
} for i, child := range children {
resp, err := b.propfindFile(propfind, &child)
func (b *backend) propfind(propfind *internal.Propfind, fi *FileInfo, depth internal.Depth, resps *[]internal.Response) error { if err != nil {
// TODO: use partial error Response on error return nil, err
}
resps[i] = *resp
}
} else {
resp, err := b.propfindFile(propfind, fi) resp, err := b.propfindFile(propfind, fi)
if err != nil { if err != nil {
return err return nil, err
}
*resps = append(*resps, *resp)
if depth != internal.DepthZero && fi.IsDir {
childDepth := depth
if depth == internal.DepthOne {
childDepth = internal.DepthZero
} }
children, err := b.FileSystem.Readdir(fi.Href) resps = []internal.Response{*resp}
if err != nil {
return err
} }
for _, child := range children { return internal.NewMultistatus(resps...), nil
if err := b.propfind(propfind, &child, childDepth, resps); err != nil {
return err
}
}
}
return nil
} }
func (b *backend) propfindFile(propfind *internal.Propfind, fi *FileInfo) (*internal.Response, error) { func (b *backend) propfindFile(propfind *internal.Propfind, fi *FileInfo) (*internal.Response, error) {