mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 00:04:19 +00:00
webdav: introduce MoveOptions
This commit is contained in:
parent
790ebfc5f8
commit
b821d8c1ea
@ -279,14 +279,18 @@ func (c *Client) Copy(ctx context.Context, name, dest string, options *CopyOptio
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move moves a file.
|
// Move moves a file.
|
||||||
func (c *Client) Move(ctx context.Context, name, dest string, overwrite bool) error {
|
func (c *Client) Move(ctx context.Context, name, dest string, options *MoveOptions) error {
|
||||||
|
if options == nil {
|
||||||
|
options = new(MoveOptions)
|
||||||
|
}
|
||||||
|
|
||||||
req, err := c.ic.NewRequest("MOVE", name, nil)
|
req, err := c.ic.NewRequest("MOVE", name, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Destination", c.ic.ResolveHref(dest).String())
|
req.Header.Set("Destination", c.ic.ResolveHref(dest).String())
|
||||||
req.Header.Set("Overwrite", internal.FormatOverwrite(overwrite))
|
req.Header.Set("Overwrite", internal.FormatOverwrite(!options.NoOverwrite))
|
||||||
|
|
||||||
resp, err := c.ic.Do(req.WithContext(ctx))
|
resp, err := c.ic.Do(req.WithContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -214,7 +214,7 @@ func (fs LocalFileSystem) Copy(ctx context.Context, src, dst string, options *Co
|
|||||||
return created, nil
|
return created, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs LocalFileSystem) Move(ctx context.Context, src, dst string, overwrite bool) (created bool, err error) {
|
func (fs LocalFileSystem) Move(ctx context.Context, src, dst string, options *MoveOptions) (created bool, err error) {
|
||||||
srcPath, err := fs.localPath(src)
|
srcPath, err := fs.localPath(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@ -230,7 +230,7 @@ func (fs LocalFileSystem) Move(ctx context.Context, src, dst string, overwrite b
|
|||||||
}
|
}
|
||||||
created = true
|
created = true
|
||||||
} else {
|
} else {
|
||||||
if !overwrite {
|
if options.NoOverwrite {
|
||||||
return false, os.ErrExist
|
return false, os.ErrExist
|
||||||
}
|
}
|
||||||
if err := os.RemoveAll(dstPath); err != nil {
|
if err := os.RemoveAll(dstPath); err != nil {
|
||||||
|
@ -21,7 +21,7 @@ type FileSystem interface {
|
|||||||
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)
|
||||||
Move(ctx context.Context, name, dest string, overwrite bool) (created bool, err error)
|
Move(ctx context.Context, name, dest string, options *MoveOptions) (created bool, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler handles WebDAV HTTP requests. It can be used to create a WebDAV
|
// Handler handles WebDAV HTTP requests. It can be used to create a WebDAV
|
||||||
@ -243,7 +243,10 @@ func (b *backend) Copy(r *http.Request, dest *internal.Href, recursive, overwrit
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *backend) Move(r *http.Request, dest *internal.Href, overwrite bool) (created bool, err error) {
|
func (b *backend) Move(r *http.Request, dest *internal.Href, overwrite bool) (created bool, err error) {
|
||||||
created, err = b.FileSystem.Move(r.Context(), r.URL.Path, dest.Path, overwrite)
|
options := MoveOptions{
|
||||||
|
NoOverwrite: !overwrite,
|
||||||
|
}
|
||||||
|
created, err = b.FileSystem.Move(r.Context(), r.URL.Path, dest.Path, &options)
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
return false, &internal.HTTPError{http.StatusPreconditionFailed, err}
|
return false, &internal.HTTPError{http.StatusPreconditionFailed, err}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user