mirror of
https://github.com/1f349/go-webdav.git
synced 2025-02-05 22:26:25 +00:00
webdav: add support for If-Match/If-None-Match in FileSystem.Create
This commit is contained in:
parent
93fee5bcf0
commit
9d778f4072
26
fs_local.go
26
fs_local.go
@ -114,15 +114,31 @@ func (fs LocalFileSystem) ReadDir(ctx context.Context, name string, recursive bo
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
created := false
|
||||
fi, _ := fs.Stat(ctx, name)
|
||||
if fi == nil {
|
||||
created = true
|
||||
fi, _ = fs.Stat(ctx, name)
|
||||
created = fi == nil
|
||||
etag := ""
|
||||
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)
|
||||
|
11
server.go
11
server.go
@ -17,7 +17,7 @@ type FileSystem interface {
|
||||
Open(ctx context.Context, name string) (io.ReadCloser, error)
|
||||
Stat(ctx context.Context, name string) (*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
|
||||
Mkdir(ctx context.Context, name string) error
|
||||
Copy(ctx context.Context, name, dest string, options *CopyOptions) (created bool, err error)
|
||||
@ -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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
13
webdav.go
13
webdav.go
@ -19,6 +19,11 @@ type FileInfo struct {
|
||||
ETag string
|
||||
}
|
||||
|
||||
type CreateOptions struct {
|
||||
IfMatch ConditionalMatch
|
||||
IfNoneMatch ConditionalMatch
|
||||
}
|
||||
|
||||
type CopyOptions struct {
|
||||
NoRecursive bool
|
||||
NoOverwrite bool
|
||||
@ -48,3 +53,11 @@ func (val ConditionalMatch) ETag() (string, error) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user