webdav: add Client.MoveAll

This commit is contained in:
Simon Ser 2020-01-22 10:15:15 +01:00
parent d30d4d2932
commit 489be203a1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 29 additions and 3 deletions

View File

@ -186,3 +186,21 @@ func (c *Client) Mkdir(name string) error {
_, err = c.ic.Do(req) _, err = c.ic.Do(req)
return err return err
} }
func (c *Client) MoveAll(name, dest string, overwrite bool) error {
req, err := c.ic.NewRequest("MOVE", name, nil)
if err != nil {
return err
}
dest, err = c.ic.ResolveHref(dest)
if err != nil {
return err
}
req.Header.Set("Destination", dest)
req.Header.Set("Overwrite", internal.FormatOverwrite(overwrite))
_, err = c.ic.Do(req)
return err
}

View File

@ -33,10 +33,10 @@ func (c *Client) SetBasicAuth(username, password string) {
c.password = password c.password = password
} }
func (c *Client) NewRequest(method string, href string, body io.Reader) (*http.Request, error) { func (c *Client) ResolveHref(href string) (string, error) {
hrefURL, err := url.Parse(href) hrefURL, err := url.Parse(href)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse request href %q: %v", href, err) return "", fmt.Errorf("webdav: failed to parse href %q: %v", href, err)
} }
u := url.URL{ u := url.URL{
@ -46,7 +46,15 @@ func (c *Client) NewRequest(method string, href string, body io.Reader) (*http.R
Path: path.Join(c.endpoint.Path, hrefURL.Path), Path: path.Join(c.endpoint.Path, hrefURL.Path),
RawQuery: hrefURL.RawQuery, RawQuery: hrefURL.RawQuery,
} }
return http.NewRequest(method, u.String(), body) return u.String(), nil
}
func (c *Client) NewRequest(method string, href string, body io.Reader) (*http.Request, error) {
href, err := c.ResolveHref(href)
if err != nil {
return nil, err
}
return http.NewRequest(method, href, body)
} }
func (c *Client) NewXMLRequest(method string, href string, v interface{}) (*http.Request, error) { func (c *Client) NewXMLRequest(method string, href string, v interface{}) (*http.Request, error) {