webdav: replace os.FileInfo with our own type

This commit is contained in:
Simon Ser 2020-01-21 22:36:42 +01:00
parent 6023eb58a0
commit c673e7c7e7
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 71 additions and 74 deletions

View File

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"path"
"time" "time"
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
@ -47,27 +45,20 @@ func (c *Client) FindCurrentUserPrincipal() (string, error) {
return prop.Href, nil return prop.Href, nil
} }
type fileInfo struct { func fileInfoFromResponse(resp *internal.Response) (*FileInfo, error) {
filename string
size int64
modTime time.Time
isDir bool
}
func fileInfoFromResponse(resp *internal.Response) (*fileInfo, error) {
href, err := resp.Href() href, err := resp.Href()
if err != nil { if err != nil {
return nil, err return nil, err
} }
filename, _ := path.Split(href)
fi := &fileInfo{filename: filename} fi := &FileInfo{Href: href}
var resType internal.ResourceType var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil { if err := resp.DecodeProp(&resType); err != nil {
return nil, err return nil, err
} }
if resType.Is(internal.CollectionName) { if resType.Is(internal.CollectionName) {
fi.isDir = true fi.IsDir = true
} else { } else {
var getLen internal.GetContentLength var getLen internal.GetContentLength
var getMod internal.GetLastModified var getMod internal.GetLastModified
@ -75,41 +66,13 @@ func fileInfoFromResponse(resp *internal.Response) (*fileInfo, error) {
return nil, err return nil, err
} }
fi.size = getLen.Length fi.Size = getLen.Length
fi.modTime = time.Time(getMod.LastModified) fi.ModTime = time.Time(getMod.LastModified)
} }
return fi, nil return fi, nil
} }
func (fi *fileInfo) Name() string {
return fi.filename
}
func (fi *fileInfo) Size() int64 {
return fi.size
}
func (fi *fileInfo) Mode() os.FileMode {
if fi.isDir {
return os.ModePerm | os.ModeDir
} else {
return os.ModePerm
}
}
func (fi *fileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi *fileInfo) IsDir() bool {
return fi.isDir
}
func (fi *fileInfo) Sys() interface{} {
return nil
}
// TODO: getetag, getcontenttype // TODO: getetag, getcontenttype
var fileInfoPropfind = internal.NewPropNamePropfind( var fileInfoPropfind = internal.NewPropNamePropfind(
internal.ResourceTypeName, internal.ResourceTypeName,
@ -117,7 +80,7 @@ var fileInfoPropfind = internal.NewPropNamePropfind(
internal.GetLastModifiedName, internal.GetLastModifiedName,
) )
func (c *Client) Stat(name string) (os.FileInfo, error) { func (c *Client) Stat(name string) (*FileInfo, error) {
resp, err := c.ic.PropfindFlat(name, fileInfoPropfind) resp, err := c.ic.PropfindFlat(name, fileInfoPropfind)
if err != nil { if err != nil {
return nil, err return nil, err
@ -139,7 +102,7 @@ func (c *Client) Open(name string) (io.ReadCloser, error) {
return resp.Body, nil return resp.Body, nil
} }
func (c *Client) Readdir(name string) ([]os.FileInfo, error) { func (c *Client) Readdir(name string) ([]FileInfo, error) {
// TODO: filter out the directory we're listing // TODO: filter out the directory we're listing
ms, err := c.ic.Propfind(name, internal.DepthOne, fileInfoPropfind) ms, err := c.ic.Propfind(name, internal.DepthOne, fileInfoPropfind)
@ -147,13 +110,13 @@ func (c *Client) Readdir(name string) ([]os.FileInfo, error) {
return nil, err return nil, err
} }
l := make([]os.FileInfo, 0, len(ms.Responses)) l := make([]FileInfo, 0, len(ms.Responses))
for _, resp := range ms.Responses { for _, resp := range ms.Responses {
fi, err := fileInfoFromResponse(&resp) fi, err := fileInfoFromResponse(&resp)
if err != nil { if err != nil {
return l, err return l, err
} }
l = append(l, fi) l = append(l, *fi)
} }
return l, nil return l, nil

View File

@ -32,15 +32,28 @@ func (fs LocalFileSystem) Open(name string) (io.ReadCloser, error) {
return os.Open(p) return os.Open(p)
} }
func (fs LocalFileSystem) Stat(name string) (os.FileInfo, error) { func fileInfoFromOS(href string, fi os.FileInfo) *FileInfo {
return &FileInfo{
Href: href,
Size: fi.Size(),
ModTime: fi.ModTime(),
IsDir: fi.IsDir(),
}
}
func (fs LocalFileSystem) Stat(name string) (*FileInfo, error) {
p, err := fs.path(name) p, err := fs.path(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return os.Stat(p) fi, err := os.Stat(p)
if err != nil {
return nil, err
}
return fileInfoFromOS(name, fi), nil
} }
func (fs LocalFileSystem) Readdir(name string) ([]os.FileInfo, error) { func (fs LocalFileSystem) Readdir(name string) ([]FileInfo, error) {
p, err := fs.path(name) p, err := fs.path(name)
if err != nil { if err != nil {
return nil, err return nil, err
@ -50,7 +63,17 @@ func (fs LocalFileSystem) Readdir(name string) ([]os.FileInfo, error) {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
return f.Readdir(-1)
fis, err := f.Readdir(-1)
if err != nil {
return nil, err
}
l := make([]FileInfo, len(fis))
for i, fi := range fis {
l[i] = *fileInfoFromOS(path.Join(name, fi.Name()), fi)
}
return l, nil
} }
func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) { func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"mime" "mime"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -16,8 +15,8 @@ import (
// FileSystem is a WebDAV server backend. // FileSystem is a WebDAV server backend.
type FileSystem interface { type FileSystem interface {
Open(name string) (io.ReadCloser, error) Open(name string) (io.ReadCloser, error)
Stat(name string) (os.FileInfo, error) Stat(name string) (*FileInfo, error)
Readdir(name string) ([]os.FileInfo, error) Readdir(name string) ([]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
@ -53,7 +52,7 @@ func (b *backend) Options(r *http.Request) ([]string, error) {
return nil, err return nil, err
} }
if fi.IsDir() { if fi.IsDir {
return []string{ return []string{
http.MethodOptions, http.MethodOptions,
http.MethodDelete, http.MethodDelete,
@ -79,7 +78,7 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
} else if err != nil { } else if err != nil {
return err return err
} }
if fi.IsDir() { if fi.IsDir {
return &internal.HTTPError{Code: http.StatusMethodNotAllowed} return &internal.HTTPError{Code: http.StatusMethodNotAllowed}
} }
@ -91,7 +90,7 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
if rs, ok := f.(io.ReadSeeker); ok { if rs, ok := f.(io.ReadSeeker); ok {
// If it's an io.Seeker, use http.ServeContent which supports ranges // If it's an io.Seeker, use http.ServeContent which supports ranges
http.ServeContent(w, r, r.URL.Path, fi.ModTime(), rs) http.ServeContent(w, r, r.URL.Path, fi.ModTime, rs)
} else { } else {
// TODO: fallback to http.DetectContentType // TODO: fallback to http.DetectContentType
t := mime.TypeByExtension(path.Ext(r.URL.Path)) t := mime.TypeByExtension(path.Ext(r.URL.Path))
@ -99,11 +98,11 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Content-Type", t) w.Header().Set("Content-Type", t)
} }
if modTime := fi.ModTime(); !modTime.IsZero() { if !fi.ModTime.IsZero() {
w.Header().Set("Last-Modified", modTime.UTC().Format(http.TimeFormat)) w.Header().Set("Last-Modified", fi.ModTime.UTC().Format(http.TimeFormat))
} }
w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10)) w.Header().Set("Content-Length", strconv.FormatInt(fi.Size, 10))
if r.Method != http.MethodHead { if r.Method != http.MethodHead {
io.Copy(w, f) io.Copy(w, f)
@ -121,35 +120,35 @@ 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, r.URL.Path, fi, depth, &resps); err != nil { if err := b.propfind(propfind, fi, depth, &resps); err != nil {
return nil, err return nil, err
} }
return internal.NewMultistatus(resps...), nil return internal.NewMultistatus(resps...), nil
} }
func (b *backend) propfind(propfind *internal.Propfind, name string, fi os.FileInfo, depth internal.Depth, resps *[]internal.Response) error { func (b *backend) propfind(propfind *internal.Propfind, fi *FileInfo, depth internal.Depth, resps *[]internal.Response) error {
// TODO: use partial error Response on error // TODO: use partial error Response on error
resp, err := b.propfindFile(propfind, name, fi) resp, err := b.propfindFile(propfind, fi)
if err != nil { if err != nil {
return err return err
} }
*resps = append(*resps, *resp) *resps = append(*resps, *resp)
if depth != internal.DepthZero && fi.IsDir() { if depth != internal.DepthZero && fi.IsDir {
childDepth := depth childDepth := depth
if depth == internal.DepthOne { if depth == internal.DepthOne {
childDepth = internal.DepthZero childDepth = internal.DepthZero
} }
children, err := b.FileSystem.Readdir(name) children, err := b.FileSystem.Readdir(fi.Href)
if err != nil { if err != nil {
return err return err
} }
for _, child := range children { for _, child := range children {
if err := b.propfind(propfind, path.Join(name, child.Name()), child, childDepth, resps); err != nil { if err := b.propfind(propfind, &child, childDepth, resps); err != nil {
return err return err
} }
} }
@ -158,23 +157,23 @@ func (b *backend) propfind(propfind *internal.Propfind, name string, fi os.FileI
return nil return nil
} }
func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.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)
props[internal.ResourceTypeName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.ResourceTypeName] = func(*internal.RawXMLValue) (interface{}, error) {
var types []xml.Name var types []xml.Name
if fi.IsDir() { if fi.IsDir {
types = append(types, internal.CollectionName) types = append(types, internal.CollectionName)
} }
return internal.NewResourceType(types...), nil return internal.NewResourceType(types...), nil
} }
if !fi.IsDir() { if !fi.IsDir {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) {
return &internal.GetContentLength{Length: fi.Size()}, nil return &internal.GetContentLength{Length: fi.Size}, nil
} }
props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) {
t := mime.TypeByExtension(path.Ext(name)) t := mime.TypeByExtension(path.Ext(fi.Href))
if t == "" { if t == "" {
// TODO: use http.DetectContentType // TODO: use http.DetectContentType
return nil, &internal.HTTPError{Code: http.StatusNotFound} return nil, &internal.HTTPError{Code: http.StatusNotFound}
@ -182,13 +181,12 @@ func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.F
return &internal.GetContentType{Type: t}, nil return &internal.GetContentType{Type: t}, nil
} }
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) {
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime())}, nil return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime)}, nil
} }
// TODO: getetag // TODO: getetag
} }
u := url.URL{Path: name} return internal.NewPropfindResponse(fi.Href, propfind, props)
return internal.NewPropfindResponse(u.String(), propfind, props)
} }
func (b *backend) Put(r *http.Request) error { func (b *backend) Put(r *http.Request) error {

View File

@ -2,3 +2,16 @@
// //
// WebDAV is defined in RFC 4918. // WebDAV is defined in RFC 4918.
package webdav package webdav
import (
"time"
)
// TODO: add ETag, MIMEType to FileInfo
type FileInfo struct {
Href string
Size int64
ModTime time.Time
IsDir bool
}