webdav: add support for more props

This commit is contained in:
Simon Ser 2020-01-15 19:08:38 +01:00
parent d83efedfb5
commit 040c38f1b6
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 65 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
) )
// TODO: cache parsed value // TODO: cache parsed value
@ -229,3 +230,31 @@ func (t *ResourceType) Is(name xml.Name) bool {
} }
var CollectionName = xml.Name{"DAV:", "collection"} var CollectionName = xml.Name{"DAV:", "collection"}
// https://tools.ietf.org/html/rfc4918#section-15.4
type GetContentLength struct {
XMLName xml.Name `xml:"DAV: getcontentlength"`
Length int64 `xml:",chardata"`
}
// https://tools.ietf.org/html/rfc4918#section-15.5
type GetContentType struct {
XMLName xml.Name `xml:"DAV: getcontenttype"`
Type string `xml:",chardata"`
}
type Date string
func NewDate(t time.Time) Date {
return Date(t.Format(time.RFC1123Z))
}
func (d Date) Time() (time.Time, error) {
return http.ParseTime(string(d))
}
// https://tools.ietf.org/html/rfc4918#section-15.7
type GetLastModified struct {
XMLName xml.Name `xml:"DAV: getlastmodified"`
LastModified Date `xml:",chardata"`
}

View File

@ -21,7 +21,12 @@ func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
} }
func (err *HTTPError) Error() string { func (err *HTTPError) Error() string {
return fmt.Sprintf("%v %v: %v", err.Code, http.StatusText(err.Code), err.Err) s := fmt.Sprintf("%v %v", err.Code, http.StatusText(err.Code))
if err.Err != nil {
return fmt.Sprintf("%v: %v", s, err.Err)
} else {
return s
}
} }
type File interface { type File interface {
@ -179,7 +184,12 @@ func (h *Handler) propfindFile(propfind *internal.Propfind, name string, fi os.F
f, ok := liveProps[xmlName] f, ok := liveProps[xmlName]
if ok { if ok {
if v, err := f(h, name, fi); err != nil { if v, err := f(h, name, fi); err != nil {
code = http.StatusInternalServerError // TODO: better error handling // TODO: don't throw away error message here
if httpErr, ok := err.(*HTTPError); ok {
code = httpErr.Code
} else {
code = http.StatusInternalServerError
}
} else { } else {
code = http.StatusOK code = http.StatusOK
val = v val = v
@ -207,4 +217,28 @@ var liveProps = map[xml.Name]PropfindFunc{
} }
return internal.NewResourceType(types...), nil return internal.NewResourceType(types...), nil
}, },
{"DAV:", "getcontentlength"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) {
if fi.IsDir() {
return nil, &HTTPError{Code: http.StatusNotFound}
}
return &internal.GetContentLength{Length: fi.Size()}, nil
},
{"DAV:", "getcontenttype"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) {
if fi.IsDir() {
return nil, &HTTPError{Code: http.StatusNotFound}
}
t := mime.TypeByExtension(path.Ext(name))
if t == "" {
// TODO: use http.DetectContentType
return nil, &HTTPError{Code: http.StatusNotFound}
}
return &internal.GetContentType{Type: t}, nil
},
{"DAV:", "getlastmodified"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) {
if fi.IsDir() {
return nil, &HTTPError{Code: http.StatusNotFound}
}
return &internal.GetLastModified{LastModified: internal.NewDate(fi.ModTime())}, nil
},
// TODO: getetag
} }