Skip to content

Commit

Permalink
webdav: respond PUT request with 204/No Content in case the file alre…
Browse files Browse the repository at this point in the history
…ady existed before putting
  • Loading branch information
DeepDiver1975 authored and emersion committed Apr 17, 2024
1 parent ffd8146 commit ff85980
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
23 changes: 17 additions & 6 deletions fs_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,36 @@ 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, error) {
func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser) (*FileInfo, bool, error) {
p, err := fs.localPath(name)
if err != nil {
return nil, err
return nil, false, err
}
created := false
fi, _ := fs.Stat(ctx, name)
if fi == nil {
created = true
}

wc, err := os.Create(p)
if err != nil {
return nil, errFromOS(err)
return nil, false, errFromOS(err)
}
defer wc.Close()

if _, err := io.Copy(wc, body); err != nil {
return nil, err
return nil, false, err
}
if err := wc.Close(); err != nil {
return nil, err
return nil, false, err
}

fi, err = fs.Stat(ctx, name)
if err != nil {
return nil, false, err
}

return fs.Stat(ctx, name)
return fi, created, err
}

func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string) error {
Expand Down
13 changes: 9 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, error)
Create(ctx context.Context, name string, body io.ReadCloser) (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)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (b *backend) PropPatch(r *http.Request, update *internal.PropertyUpdate) (*
}

func (b *backend) Put(w http.ResponseWriter, r *http.Request) error {
fi, err := b.FileSystem.Create(r.Context(), r.URL.Path, r.Body)
fi, created, err := b.FileSystem.Create(r.Context(), r.URL.Path, r.Body)
if err != nil {
return err
}
Expand All @@ -209,8 +209,13 @@ func (b *backend) Put(w http.ResponseWriter, r *http.Request) error {
if fi.ETag != "" {
w.Header().Set("ETag", internal.ETag(fi.ETag).String())
}
w.WriteHeader(http.StatusCreated)
// TODO: http.StatusNoContent if the resource already existed

if created {
w.WriteHeader(http.StatusCreated)
} else {
w.WriteHeader(http.StatusNoContent)
}

return nil
}

Expand Down

0 comments on commit ff85980

Please sign in to comment.