feat: update WebDAV download handler to support range requests and improve response handling

This commit is contained in:
Leon Bösche
2026-01-11 14:38:03 +01:00
parent b2e5eef66f
commit 5ef5623c8d
2 changed files with 46 additions and 19 deletions

View File

@@ -124,9 +124,9 @@ func (c *WebDAVClient) Upload(ctx context.Context, remotePath string, r io.Reade
}
// Download retrieves a file from the remotePath using HTTP GET (WebDAV).
func (c *WebDAVClient) Download(ctx context.Context, remotePath string) (io.ReadCloser, int64, error) {
func (c *WebDAVClient) Download(ctx context.Context, remotePath string, rangeHeader string) (*http.Response, error) {
if c == nil {
return nil, 0, fmt.Errorf("no webdav client configured")
return nil, fmt.Errorf("no webdav client configured")
}
rel := strings.TrimLeft(remotePath, "/")
@@ -146,24 +146,27 @@ func (c *WebDAVClient) Download(ctx context.Context, remotePath string) (io.Read
req, err := http.NewRequestWithContext(ctx, "GET", full, nil)
if err != nil {
return nil, 0, err
return nil, err
}
if c.user != "" {
req.SetBasicAuth(c.user, c.pass)
}
if rangeHeader != "" {
req.Header.Set("Range", rangeHeader)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, 0, err
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return resp.Body, resp.ContentLength, nil
return resp, nil
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return nil, 0, fmt.Errorf("webdav download failed: %d %s", resp.StatusCode, string(body))
resp.Body.Close()
return nil, fmt.Errorf("webdav download failed: %d %s", resp.StatusCode, string(body))
}
// Delete removes a file or collection from the remotePath using HTTP DELETE (WebDAV).