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
43
fs_local.go
43
fs_local.go
@ -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
|
||||||
if err != nil {
|
err = filepath.Walk(p, func(p string, fi os.FileInfo, err error) error {
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
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) {
|
||||||
|
57
server.go
57
server.go
@ -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,43 +116,32 @@ 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 {
|
||||||
return nil, err
|
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
|
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) {
|
func (b *backend) propfindFile(propfind *internal.Propfind, fi *FileInfo) (*internal.Response, error) {
|
||||||
props := make(map[xml.Name]internal.PropfindFunc)
|
props := make(map[xml.Name]internal.PropfindFunc)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user