webdav: respond PUT request with 204/No Content in case the file already existed before putting

This commit is contained in:
Thomas Müller 2024-04-17 15:07:53 +02:00 committed by Simon Ser
parent ffd81465fd
commit ff8598015d
2 changed files with 26 additions and 10 deletions

View File

@ -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
}
return fs.Stat(ctx, name)
fi, err = fs.Stat(ctx, name)
if err != nil {
return nil, false, err
}
return fi, created, err
}
func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string) error {

View File

@ -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)
@ -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
}
@ -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
}