Skip to content

Commit

Permalink
webdav: change FileSystem.Create to give implementations more control
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 authored and emersion committed Apr 9, 2024
1 parent 3ed9a4f commit df447dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
18 changes: 15 additions & 3 deletions fs_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 2 additions & 10 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) (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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit df447dc

Please sign in to comment.