webdav: change FileSystem.Create to give implementations more control

This commit is contained in:
Thomas Müller 2024-04-09 12:15:55 +02:00 committed by Simon Ser
parent 3ed9a4f052
commit df447dc627
2 changed files with 17 additions and 13 deletions

View File

@ -114,13 +114,25 @@ 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) (io.WriteCloser, error) { func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser) error {
p, err := fs.localPath(name) p, err := fs.localPath(name)
if err != nil { if err != nil {
return nil, err return err
} }
wc, err := os.Create(p) 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 { 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) 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) (io.WriteCloser, error) Create(ctx context.Context, name string, body io.ReadCloser) 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,18 +194,10 @@ 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 {
wc, err := b.FileSystem.Create(r.Context(), r.URL.Path) err := b.FileSystem.Create(r.Context(), r.URL.Path, r.Body)
if err != nil { if err != nil {
return err 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) w.WriteHeader(http.StatusCreated)
// TODO: Last-Modified, ETag, Content-Type if the request has been copied // TODO: Last-Modified, ETag, Content-Type if the request has been copied