mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 08:14:15 +00:00
webdav: respond PUT request with 204/No Content in case the file already existed before putting
This commit is contained in:
parent
ffd81465fd
commit
ff8598015d
23
fs_local.go
23
fs_local.go
@ -114,25 +114,36 @@ 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, error) {
|
func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser) (*FileInfo, bool, error) {
|
||||||
p, err := fs.localPath(name)
|
p, err := fs.localPath(name)
|
||||||
if err != nil {
|
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)
|
wc, err := os.Create(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errFromOS(err)
|
return nil, false, errFromOS(err)
|
||||||
}
|
}
|
||||||
defer wc.Close()
|
defer wc.Close()
|
||||||
|
|
||||||
if _, err := io.Copy(wc, body); err != nil {
|
if _, err := io.Copy(wc, body); err != nil {
|
||||||
return nil, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
if err := wc.Close(); err != nil {
|
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 {
|
func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string) error {
|
||||||
|
11
server.go
11
server.go
@ -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, error)
|
Create(ctx context.Context, name string, body io.ReadCloser) (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)
|
||||||
@ -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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -209,8 +209,13 @@ func (b *backend) Put(w http.ResponseWriter, r *http.Request) error {
|
|||||||
if fi.ETag != "" {
|
if fi.ETag != "" {
|
||||||
w.Header().Set("ETag", internal.ETag(fi.ETag).String())
|
w.Header().Set("ETag", internal.ETag(fi.ETag).String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if created {
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
// TODO: http.StatusNoContent if the resource already existed
|
} else {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user