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
}
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) {
p, err := fs.path(name)
if err != nil {
@ -56,27 +65,31 @@ func (fs LocalFileSystem) Stat(name string) (*FileInfo, error) {
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)
if err != nil {
return nil, err
}
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
fis, err := f.Readdir(-1)
if err != nil {
return nil, err
}
var l []FileInfo
err = filepath.Walk(p, func(p string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
l := make([]FileInfo, len(fis))
for i, fi := range fis {
l[i] = *fileInfoFromOS(path.Join(name, fi.Name()), fi)
}
return l, nil
href, err := fs.href(p)
if err != nil {
return err
}
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) {

View File

@ -14,7 +14,7 @@ import (
type FileSystem interface {
Open(name string) (io.ReadCloser, error)
Stat(name string) (*FileInfo, error)
Readdir(name string) ([]FileInfo, error)
Readdir(name string, recursive bool) ([]FileInfo, error)
Create(name string) (io.WriteCloser, error)
RemoveAll(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) {
// TODO: use partial error Response on error
fi, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) {
return nil, &internal.HTTPError{Code: http.StatusNotFound, Err: err}
@ -114,43 +116,32 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i
}
var resps []internal.Response
if err := b.propfind(propfind, fi, depth, &resps); err != nil {
return nil, err
if depth != internal.DepthZero && fi.IsDir {
children, err := b.FileSystem.Readdir(r.URL.Path, depth == internal.DepthInfinity)
if err != nil {
return nil, err
}
resps = make([]internal.Response, len(children))
for i, child := range children {
resp, err := b.propfindFile(propfind, &child)
if err != nil {
return nil, err
}
resps[i] = *resp
}
} else {
resp, err := b.propfindFile(propfind, fi)
if err != nil {
return nil, err
}
resps = []internal.Response{*resp}
}
return internal.NewMultistatus(resps...), nil
}
func (b *backend) propfind(propfind *internal.Propfind, fi *FileInfo, depth internal.Depth, resps *[]internal.Response) error {
// TODO: use partial error Response on error
resp, err := b.propfindFile(propfind, fi)
if err != nil {
return 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)
if err != nil {
return err
}
for _, child := range children {
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) {
props := make(map[xml.Name]internal.PropfindFunc)