Compare commits

..

8 Commits

Author SHA1 Message Date
6f60a899bf
Add error returning for missing error output paths
Co-Authored-By: Captain ALM <captainalm@captainalm.com>
2025-01-26 18:49:07 +00:00
d28f08a32d
Change this too 2025-01-26 18:49:07 +00:00
f5b508b766
Respond with 200 OK instead of 204 No Content 2025-01-26 18:49:06 +00:00
63f15c0ec6
AddCurrentUserPrivilegeSet to find caldav 2025-01-26 18:49:06 +00:00
906087cd59
Add CurrentUserPrivilegeSet to find carddav 2025-01-26 18:49:00 +00:00
Simon Ser
3cc7466ac9 internal: add PropFindValue
NewPropFindResponse uses callbacks to lazily build the response.
However, some props are static: they don't require any processing
to generate. Add a small helper to reduce boilerplate a bit.
2025-01-13 23:00:32 +01:00
Simon Ser
9d778f4072 webdav: add support for If-Match/If-None-Match in FileSystem.Create 2024-12-09 22:31:59 +01:00
Simon Ser
93fee5bcf0 webdav: don't leave a partially uploaded file behind on error 2024-12-09 09:19:16 +01:00
6 changed files with 154 additions and 132 deletions

View File

@ -465,12 +465,10 @@ func (b *backend) propFindRoot(ctx context.Context, propfind *internal.PropFind)
} }
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
@ -486,15 +484,13 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
} }
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
calendarHomeSetName: func(*internal.RawXMLValue) (interface{}, error) { calendarHomeSetName: internal.PropFindValue(&calendarHomeSet{
return &calendarHomeSet{Href: internal.Href{Path: homeSetPath}}, nil Href: internal.Href{Path: homeSetPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
@ -511,12 +507,10 @@ func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFi
// TODO anything else to return here? // TODO anything else to return here?
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(homeSetPath, propfind, props) return internal.NewPropFindResponse(homeSetPath, propfind, props)
} }
@ -530,19 +524,15 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, calendarName)),
return internal.NewResourceType(internal.CollectionName, calendarName), nil calendarDescriptionName: internal.PropFindValue(&calendarDescription{
}, Description: cal.Description,
calendarDescriptionName: func(*internal.RawXMLValue) (interface{}, error) { }),
return &calendarDescription{Description: cal.Description}, nil supportedCalendarDataName: internal.PropFindValue(&supportedCalendarData{
},
supportedCalendarDataName: func(*internal.RawXMLValue) (interface{}, error) {
return &supportedCalendarData{
Types: []calendarDataType{ Types: []calendarDataType{
{ContentType: ical.MIMEType, Version: "2.0"}, {ContentType: ical.MIMEType, Version: "2.0"},
}, },
}, nil }),
},
supportedCalendarComponentSetName: func(*internal.RawXMLValue) (interface{}, error) { supportedCalendarComponentSetName: func(*internal.RawXMLValue) (interface{}, error) {
components := []comp{} components := []comp{}
if cal.SupportedComponentSet != nil { if cal.SupportedComponentSet != nil {
@ -559,19 +549,19 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
} }
if cal.Name != "" { if cal.Name != "" {
props[internal.DisplayNameName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.DisplayNameName] = internal.PropFindValue(&internal.DisplayName{
return &internal.DisplayName{Name: cal.Name}, nil Name: cal.Name,
} })
} }
if cal.Description != "" { if cal.Description != "" {
props[calendarDescriptionName] = func(*internal.RawXMLValue) (interface{}, error) { props[calendarDescriptionName] = internal.PropFindValue(&calendarDescription{
return &calendarDescription{Description: cal.Description}, nil Description: cal.Description,
} })
} }
if cal.MaxResourceSize > 0 { if cal.MaxResourceSize > 0 {
props[maxResourceSizeName] = func(*internal.RawXMLValue) (interface{}, error) { props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
return &maxResourceSize{Size: cal.MaxResourceSize}, nil Size: cal.MaxResourceSize,
} })
} }
props[internal.CurrentUserPrivilegeSetName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.CurrentUserPrivilegeSetName] = func(*internal.RawXMLValue) (interface{}, error) {
return &internal.CurrentUserPrivilegeSet{Privilege: internal.NewAllPrivileges()}, nil return &internal.CurrentUserPrivilegeSet{Privilege: internal.NewAllPrivileges()}, nil
@ -615,9 +605,9 @@ func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.GetContentTypeName: internal.PropFindValue(&internal.GetContentType{
return &internal.GetContentType{Type: ical.MIMEType}, nil Type: ical.MIMEType,
}, }),
// TODO: calendar-data can only be used in REPORT requests // TODO: calendar-data can only be used in REPORT requests
calendarDataName: func(*internal.RawXMLValue) (interface{}, error) { calendarDataName: func(*internal.RawXMLValue) (interface{}, error) {
var buf bytes.Buffer var buf bytes.Buffer
@ -630,20 +620,20 @@ func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal
} }
if co.ContentLength > 0 { if co.ContentLength > 0 {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = internal.PropFindValue(&internal.GetContentLength{
return &internal.GetContentLength{Length: co.ContentLength}, nil Length: co.ContentLength,
} })
} }
if !co.ModTime.IsZero() { if !co.ModTime.IsZero() {
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = internal.PropFindValue(&internal.GetLastModified{
return &internal.GetLastModified{LastModified: internal.Time(co.ModTime)}, nil LastModified: internal.Time(co.ModTime),
} })
} }
if co.ETag != "" { if co.ETag != "" {
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetETagName] = internal.PropFindValue(&internal.GetETag{
return &internal.GetETag{ETag: internal.ETag(co.ETag)}, nil ETag: internal.ETag(co.ETag),
} })
} }
return internal.NewPropFindResponse(co.Path, propfind, props) return internal.NewPropFindResponse(co.Path, propfind, props)

View File

@ -431,12 +431,10 @@ func (b *backend) propFindRoot(ctx context.Context, propfind *internal.PropFind)
} }
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
@ -446,30 +444,24 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
if err != nil { if err != nil {
return nil, err return nil, err
} }
props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
Href: internal.Href{Path: principalPath},
}),
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx) homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil
},
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
return &addressbookHomeSet{Href: internal.Href{Path: homeSetPath}}, nil return &addressbookHomeSet{Href: internal.Href{Path: homeSetPath}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFind) (*internal.Response, error) { func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFind) (*internal.Response, error) {
principalPath, err := b.Backend.CurrentUserPrincipal(ctx)
if err != nil {
return nil, err
}
homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx) homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -478,11 +470,13 @@ func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFi
// TODO anything else to return here? // TODO anything else to return here?
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
principalPath, err := b.Backend.CurrentUserPrincipal(ctx)
if err != nil {
return nil, err
}
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(homeSetPath, propfind, props) return internal.NewPropFindResponse(homeSetPath, propfind, props)
} }
@ -496,33 +490,29 @@ func (b *backend) propFindAddressBook(ctx context.Context, propfind *internal.Pr
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, addressBookName)),
return internal.NewResourceType(internal.CollectionName, addressBookName), nil supportedAddressDataName: internal.PropFindValue(&supportedAddressData{
},
supportedAddressDataName: func(*internal.RawXMLValue) (interface{}, error) {
return &supportedAddressData{
Types: []addressDataType{ Types: []addressDataType{
{ContentType: vcard.MIMEType, Version: "3.0"}, {ContentType: vcard.MIMEType, Version: "3.0"},
{ContentType: vcard.MIMEType, Version: "4.0"}, {ContentType: vcard.MIMEType, Version: "4.0"},
}, },
}, nil }),
},
} }
if ab.Name != "" { if ab.Name != "" {
props[internal.DisplayNameName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.DisplayNameName] = internal.PropFindValue(&internal.DisplayName{
return &internal.DisplayName{Name: ab.Name}, nil Name: ab.Name,
} })
} }
if ab.Description != "" { if ab.Description != "" {
props[addressBookDescriptionName] = func(*internal.RawXMLValue) (interface{}, error) { props[addressBookDescriptionName] = internal.PropFindValue(&addressbookDescription{
return &addressbookDescription{Description: ab.Description}, nil Description: ab.Description,
} })
} }
if ab.MaxResourceSize > 0 { if ab.MaxResourceSize > 0 {
props[maxResourceSizeName] = func(*internal.RawXMLValue) (interface{}, error) { props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
return &maxResourceSize{Size: ab.MaxResourceSize}, nil Size: ab.MaxResourceSize,
} })
} }
props[internal.CurrentUserPrivilegeSetName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.CurrentUserPrivilegeSetName] = func(*internal.RawXMLValue) (interface{}, error) {
return &internal.CurrentUserPrivilegeSet{Privilege: internal.NewAllPrivileges()}, nil return &internal.CurrentUserPrivilegeSet{Privilege: internal.NewAllPrivileges()}, nil
@ -564,9 +554,9 @@ func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.GetContentTypeName: internal.PropFindValue(&internal.GetContentType{
return &internal.GetContentType{Type: vcard.MIMEType}, nil Type: vcard.MIMEType,
}, }),
// TODO: address-data can only be used in REPORT requests // TODO: address-data can only be used in REPORT requests
addressDataName: func(*internal.RawXMLValue) (interface{}, error) { addressDataName: func(*internal.RawXMLValue) (interface{}, error) {
var buf bytes.Buffer var buf bytes.Buffer
@ -579,20 +569,20 @@ func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.
} }
if ao.ContentLength > 0 { if ao.ContentLength > 0 {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = internal.PropFindValue(&internal.GetContentLength{
return &internal.GetContentLength{Length: ao.ContentLength}, nil Length: ao.ContentLength,
} })
} }
if !ao.ModTime.IsZero() { if !ao.ModTime.IsZero() {
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = internal.PropFindValue(&internal.GetLastModified{
return &internal.GetLastModified{LastModified: internal.Time(ao.ModTime)}, nil LastModified: internal.Time(ao.ModTime),
} })
} }
if ao.ETag != "" { if ao.ETag != "" {
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetETagName] = internal.PropFindValue(&internal.GetETag{
return &internal.GetETag{ETag: internal.ETag(ao.ETag)}, nil ETag: internal.ETag(ao.ETag),
} })
} }
return internal.NewPropFindResponse(ao.Path, propfind, props) return internal.NewPropFindResponse(ao.Path, propfind, props)

View File

@ -114,15 +114,31 @@ func (fs LocalFileSystem) ReadDir(ctx context.Context, name string, recursive bo
return l, errFromOS(err) return l, errFromOS(err)
} }
func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser) (*FileInfo, bool, error) { func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fi *FileInfo, created bool, err error) {
p, err := fs.localPath(name) p, err := fs.localPath(name)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
created := false fi, _ = fs.Stat(ctx, name)
fi, _ := fs.Stat(ctx, name) created = fi == nil
if fi == nil { etag := ""
created = true if fi != nil {
etag = fi.ETag
}
if opts.IfMatch.IsSet() {
if ok, err := opts.IfMatch.MatchETag(etag); err != nil {
return nil, false, NewHTTPError(http.StatusBadRequest, err)
} else if !ok {
return nil, false, NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-Match condition failed"))
}
}
if opts.IfNoneMatch.IsSet() {
if ok, err := opts.IfNoneMatch.MatchETag(etag); err != nil {
return nil, false, NewHTTPError(http.StatusBadRequest, err)
} else if ok {
return nil, false, NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-None-Match condition failed"))
}
} }
wc, err := os.Create(p) wc, err := os.Create(p)
@ -132,9 +148,11 @@ func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadC
defer wc.Close() defer wc.Close()
if _, err := io.Copy(wc, body); err != nil { if _, err := io.Copy(wc, body); err != nil {
os.Remove(p)
return nil, false, err return nil, false, err
} }
if err := wc.Close(); err != nil { if err := wc.Close(); err != nil {
os.Remove(p)
return nil, false, err return nil, false, err
} }

View File

@ -162,13 +162,17 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) error {
type PropFindFunc func(raw *RawXMLValue) (interface{}, error) type PropFindFunc func(raw *RawXMLValue) (interface{}, error)
func PropFindValue(value interface{}) PropFindFunc {
return func(raw *RawXMLValue) (interface{}, error) {
return value, nil
}
}
func NewPropFindResponse(path string, propfind *PropFind, props map[xml.Name]PropFindFunc) (*Response, error) { func NewPropFindResponse(path string, propfind *PropFind, props map[xml.Name]PropFindFunc) (*Response, error) {
resp := &Response{Hrefs: []Href{Href{Path: path}}} resp := &Response{Hrefs: []Href{Href{Path: path}}}
if _, ok := props[ResourceTypeName]; !ok { if _, ok := props[ResourceTypeName]; !ok {
props[ResourceTypeName] = func(*RawXMLValue) (interface{}, error) { props[ResourceTypeName] = PropFindValue(NewResourceType())
return NewResourceType(), nil
}
} }
if propfind.PropName != nil { if propfind.PropName != nil {

View File

@ -17,7 +17,7 @@ type FileSystem interface {
Open(ctx context.Context, name string) (io.ReadCloser, error) Open(ctx context.Context, name string) (io.ReadCloser, error)
Stat(ctx context.Context, name string) (*FileInfo, error) Stat(ctx context.Context, name string) (*FileInfo, error)
ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error) ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error)
Create(ctx context.Context, name string, body io.ReadCloser) (fileInfo *FileInfo, created bool, err error) Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fileInfo *FileInfo, created bool, err error)
RemoveAll(ctx context.Context, name string) error RemoveAll(ctx context.Context, name string) error
Mkdir(ctx context.Context, name string) error Mkdir(ctx context.Context, name string) error
Copy(ctx context.Context, name, dest string, options *CopyOptions) (created bool, err error) Copy(ctx context.Context, name, dest string, options *CopyOptions) (created bool, err error)
@ -162,26 +162,26 @@ func (b *backend) propFindFile(propfind *internal.PropFind, fi *FileInfo) (*inte
} }
if !fi.IsDir { if !fi.IsDir {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = internal.PropFindValue(&internal.GetContentLength{
return &internal.GetContentLength{Length: fi.Size}, nil Length: fi.Size,
} })
if !fi.ModTime.IsZero() { if !fi.ModTime.IsZero() {
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = internal.PropFindValue(&internal.GetLastModified{
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime)}, nil LastModified: internal.Time(fi.ModTime),
} })
} }
if fi.MIMEType != "" { if fi.MIMEType != "" {
props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentTypeName] = internal.PropFindValue(&internal.GetContentType{
return &internal.GetContentType{Type: fi.MIMEType}, nil Type: fi.MIMEType,
} })
} }
if fi.ETag != "" { if fi.ETag != "" {
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetETagName] = internal.PropFindValue(&internal.GetETag{
return &internal.GetETag{ETag: internal.ETag(fi.ETag)}, nil ETag: internal.ETag(fi.ETag),
} })
} }
} }
@ -194,7 +194,14 @@ func (b *backend) PropPatch(r *http.Request, update *internal.PropertyUpdate) (*
} }
func (b *backend) Put(w http.ResponseWriter, r *http.Request) error { func (b *backend) Put(w http.ResponseWriter, r *http.Request) error {
fi, created, err := b.FileSystem.Create(r.Context(), r.URL.Path, r.Body) ifNoneMatch := ConditionalMatch(r.Header.Get("If-None-Match"))
ifMatch := ConditionalMatch(r.Header.Get("If-Match"))
opts := CreateOptions{
IfNoneMatch: ifNoneMatch,
IfMatch: ifMatch,
}
fi, created, err := b.FileSystem.Create(r.Context(), r.URL.Path, r.Body, &opts)
if err != nil { if err != nil {
return err return err
} }

View File

@ -19,6 +19,11 @@ type FileInfo struct {
ETag string ETag string
} }
type CreateOptions struct {
IfMatch ConditionalMatch
IfNoneMatch ConditionalMatch
}
type CopyOptions struct { type CopyOptions struct {
NoRecursive bool NoRecursive bool
NoOverwrite bool NoOverwrite bool
@ -48,3 +53,11 @@ func (val ConditionalMatch) ETag() (string, error) {
} }
return string(e), nil return string(e), nil
} }
func (val ConditionalMatch) MatchETag(etag string) (bool, error) {
if val.IsWildcard() {
return true, nil
}
t, err := val.ETag()
return t == etag, err
}