diff --git a/fs_local.go b/fs_local.go index 7668612..6caac26 100644 --- a/fs_local.go +++ b/fs_local.go @@ -114,13 +114,25 @@ func (fs LocalFileSystem) ReadDir(ctx context.Context, name string, recursive bo return l, errFromOS(err) } -func (fs LocalFileSystem) Create(ctx context.Context, name string) (io.WriteCloser, error) { +func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser) error { p, err := fs.localPath(name) if err != nil { - return nil, err + return err } wc, err := os.Create(p) - return wc, errFromOS(err) + if err != nil { + return errFromOS(err) + } + defer wc.Close() + + if _, err := io.Copy(wc, body); err != nil { + return err + } + if err := wc.Close(); err != nil { + return err + } + + return nil } func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string) error { diff --git a/server.go b/server.go index 08f3913..16f3310 100644 --- a/server.go +++ b/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) (io.WriteCloser, error) + Create(ctx context.Context, name string, body io.ReadCloser) 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,18 +194,10 @@ func (b *backend) PropPatch(r *http.Request, update *internal.PropertyUpdate) (* } func (b *backend) Put(w http.ResponseWriter, r *http.Request) error { - wc, err := b.FileSystem.Create(r.Context(), r.URL.Path) + err := b.FileSystem.Create(r.Context(), r.URL.Path, r.Body) if err != nil { return err } - defer wc.Close() - - if _, err := io.Copy(wc, r.Body); err != nil { - return err - } - if err := wc.Close(); err != nil { - return err - } w.WriteHeader(http.StatusCreated) // TODO: Last-Modified, ETag, Content-Type if the request has been copied