webdav: remove File.Stat, add FileSystem.Stat

References: https://github.com/emersion/go-webdav/issues/15
This commit is contained in:
Simon Ser 2020-01-21 19:55:02 +01:00
parent 6526cef9eb
commit e851e6e3f1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 29 additions and 25 deletions

View File

@ -31,4 +31,12 @@ func (fs LocalFileSystem) Open(name string) (File, error) {
return os.Open(p)
}
func (fs LocalFileSystem) Stat(name string) (os.FileInfo, error) {
p, err := fs.path(name)
if err != nil {
return nil, err
}
return os.Stat(p)
}
var _ FileSystem = LocalFileSystem("")

View File

@ -2,6 +2,7 @@ package webdav
import (
"encoding/xml"
"io"
"mime"
"net/http"
"os"
@ -11,11 +12,15 @@ import (
)
type File interface {
http.File
io.Closer
io.Reader
io.Seeker
Readdir(count int) ([]os.FileInfo, error)
}
type FileSystem interface {
Open(name string) (File, error)
Stat(name string) (os.FileInfo, error)
}
type Handler struct {
@ -38,18 +43,12 @@ type backend struct {
}
func (b *backend) Options(r *http.Request) ([]string, error) {
f, err := b.FileSystem.Open(r.URL.Path)
fi, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) {
return []string{http.MethodOptions}, nil
} else if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.IsDir() {
return []string{http.MethodOptions, "PROPFIND"}, nil
@ -59,34 +58,31 @@ func (b *backend) Options(r *http.Request) ([]string, error) {
}
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
fi, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) {
return &internal.HTTPError{Code: http.StatusNotFound, Err: err}
} else if err != nil {
return err
}
if fi.IsDir() {
return &internal.HTTPError{Code: http.StatusMethodNotAllowed}
}
f, err := b.FileSystem.Open(r.URL.Path)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
return &internal.HTTPError{Code: http.StatusMethodNotAllowed}
}
http.ServeContent(w, r, r.URL.Path, fi.ModTime(), f)
return nil
}
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
f, err := b.FileSystem.Open(r.URL.Path)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
fi, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) {
return nil, &internal.HTTPError{Code: http.StatusNotFound, Err: err}
} else if err != nil {
return nil, err
}