mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
webdav: move recursive PROPFIND to backend
Closes: https://github.com/emersion/go-webdav/issues/22
This commit is contained in:
parent
307a998a46
commit
72c96af206
37
fs_local.go
37
fs_local.go
@ -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)
|
||||
var l []FileInfo
|
||||
err = filepath.Walk(p, func(p string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
l := make([]FileInfo, len(fis))
|
||||
for i, fi := range fis {
|
||||
l[i] = *fileInfoFromOS(path.Join(name, fi.Name()), fi)
|
||||
href, err := fs.href(p)
|
||||
if err != nil {
|
||||
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) {
|
||||
|
43
server.go
43
server.go
@ -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,41 +116,30 @@ 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 {
|
||||
if depth != internal.DepthZero && fi.IsDir {
|
||||
children, err := b.FileSystem.Readdir(r.URL.Path, depth == internal.DepthInfinity)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (b *backend) propfind(propfind *internal.Propfind, fi *FileInfo, depth internal.Depth, resps *[]internal.Response) error {
|
||||
// TODO: use partial error Response on error
|
||||
|
||||
resps[i] = *resp
|
||||
}
|
||||
} else {
|
||||
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
|
||||
return nil, err
|
||||
}
|
||||
|
||||
children, err := b.FileSystem.Readdir(fi.Href)
|
||||
if err != nil {
|
||||
return err
|
||||
resps = []internal.Response{*resp}
|
||||
}
|
||||
|
||||
for _, child := range children {
|
||||
if err := b.propfind(propfind, &child, childDepth, resps); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return internal.NewMultistatus(resps...), nil
|
||||
}
|
||||
|
||||
func (b *backend) propfindFile(propfind *internal.Propfind, fi *FileInfo) (*internal.Response, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user