FIX: Implement WebDAV Move and simplified move file handler
This commit is contained in:
@@ -216,3 +216,60 @@ func (c *WebDAVClient) Delete(ctx context.Context, remotePath string) error {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("webdav delete failed: %d %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Move moves/renames a file using WebDAV MOVE method
|
||||
func (c *WebDAVClient) Move(ctx context.Context, sourcePath, targetPath string) error {
|
||||
if c == nil {
|
||||
return fmt.Errorf("no webdav client configured")
|
||||
}
|
||||
|
||||
sourceRel := strings.TrimLeft(sourcePath, "/")
|
||||
targetRel := strings.TrimLeft(targetPath, "/")
|
||||
|
||||
u := c.basePrefix
|
||||
if u == "/" || u == "" {
|
||||
u = ""
|
||||
}
|
||||
u = strings.TrimRight(u, "/")
|
||||
|
||||
// Build source URL
|
||||
var sourceURL string
|
||||
if u == "" {
|
||||
sourceURL = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(sourceRel))
|
||||
} else {
|
||||
sourceURL = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(sourceRel))
|
||||
}
|
||||
sourceURL = strings.ReplaceAll(sourceURL, "%2F", "/")
|
||||
|
||||
// Build target URL
|
||||
var targetURL string
|
||||
if u == "" {
|
||||
targetURL = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(targetRel))
|
||||
} else {
|
||||
targetURL = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(targetRel))
|
||||
}
|
||||
targetURL = strings.ReplaceAll(targetURL, "%2F", "/")
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "MOVE", sourceURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Destination", targetURL)
|
||||
if c.user != "" {
|
||||
req.SetBasicAuth(c.user, c.pass)
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
return nil
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("webdav move failed: %d %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user