Refactor WebDAV client to expose BaseURL and update avatar upload logic for improved URL handling
This commit is contained in:
@@ -69,7 +69,7 @@ func NewUserWebDAVClient(nextcloudBaseURL, username, password string) *WebDAVCli
|
||||
fullURL := fmt.Sprintf("%s/remote.php/dav/files/%s", baseURL, username)
|
||||
|
||||
return &WebDAVClient{
|
||||
baseURL: fullURL,
|
||||
BaseURL: fullURL,
|
||||
user: username,
|
||||
pass: password,
|
||||
basePrefix: "/",
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
type WebDAVClient struct {
|
||||
baseURL string
|
||||
BaseURL string
|
||||
user string
|
||||
pass string
|
||||
basePrefix string
|
||||
@@ -29,17 +29,20 @@ func NewWebDAVClient(cfg *config.Config) *WebDAVClient {
|
||||
return nil
|
||||
}
|
||||
u := strings.TrimRight(cfg.NextcloudURL, "/")
|
||||
if !strings.Contains(u, "/remote.php") {
|
||||
u += "/remote.php/dav/files/" + cfg.NextcloudUser
|
||||
}
|
||||
base := cfg.NextcloudBase
|
||||
if base == "" {
|
||||
base = "/"
|
||||
}
|
||||
log.Printf("[WEBDAV] Initializing WebDAV client - URL: %s, User: %s, BasePath: %s\n", u, cfg.NextcloudUser, base)
|
||||
return &WebDAVClient{
|
||||
baseURL: u,
|
||||
BaseURL: u,
|
||||
user: cfg.NextcloudUser,
|
||||
pass: cfg.NextcloudPass,
|
||||
basePrefix: strings.TrimRight(base, "/"),
|
||||
httpClient: &http.Client{Timeout: 10 * time.Minute},
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,14 +60,14 @@ func (c *WebDAVClient) ensureParent(ctx context.Context, remotePath string) erro
|
||||
cur = path.Join(cur, p)
|
||||
var mkurl string
|
||||
if cur == "" || cur == "/" {
|
||||
mkurl = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(p))
|
||||
mkurl = fmt.Sprintf("%s/%s", c.BaseURL, url.PathEscape(p))
|
||||
} else {
|
||||
// Ensure there's a "/" between baseURL and cur
|
||||
sep := ""
|
||||
if !strings.HasSuffix(c.baseURL, "/") && !strings.HasPrefix(cur, "/") {
|
||||
if !strings.HasSuffix(c.BaseURL, "/") && !strings.HasPrefix(cur, "/") {
|
||||
sep = "/"
|
||||
}
|
||||
mkurl = fmt.Sprintf("%s%s%s", c.baseURL, sep, strings.TrimPrefix(cur, "/"))
|
||||
mkurl = fmt.Sprintf("%s%s%s", c.BaseURL, sep, strings.TrimPrefix(cur, "/"))
|
||||
}
|
||||
req, _ := http.NewRequestWithContext(ctx, "MKCOL", mkurl, nil)
|
||||
if c.user != "" {
|
||||
@@ -103,13 +106,13 @@ func (c *WebDAVClient) Upload(ctx context.Context, remotePath string, r io.Reade
|
||||
|
||||
var full string
|
||||
if u == "" {
|
||||
full = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(rel))
|
||||
full = fmt.Sprintf("%s/%s", c.BaseURL, url.PathEscape(rel))
|
||||
} else {
|
||||
full = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(rel))
|
||||
full = fmt.Sprintf("%s%s/%s", c.BaseURL, u, url.PathEscape(rel))
|
||||
}
|
||||
full = strings.ReplaceAll(full, "%2F", "/")
|
||||
|
||||
fmt.Printf("[WEBDAV-UPLOAD] BaseURL: %s, BasePrefix: %s, RemotePath: %s, Full URL: %s\n", c.baseURL, c.basePrefix, remotePath, full)
|
||||
fmt.Printf("[WEBDAV-UPLOAD] BaseURL: %s, BasePrefix: %s, RemotePath: %s, Full URL: %s\n", c.BaseURL, c.basePrefix, remotePath, full)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "PUT", full, r)
|
||||
if err != nil {
|
||||
@@ -150,9 +153,9 @@ func (c *WebDAVClient) Download(ctx context.Context, remotePath string, rangeHea
|
||||
|
||||
var full string
|
||||
if u == "" {
|
||||
full = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(rel))
|
||||
full = fmt.Sprintf("%s/%s", c.BaseURL, url.PathEscape(rel))
|
||||
} else {
|
||||
full = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(rel))
|
||||
full = fmt.Sprintf("%s%s/%s", c.BaseURL, u, url.PathEscape(rel))
|
||||
}
|
||||
full = strings.ReplaceAll(full, "%2F", "/")
|
||||
|
||||
@@ -196,9 +199,9 @@ func (c *WebDAVClient) Delete(ctx context.Context, remotePath string) error {
|
||||
|
||||
var full string
|
||||
if u == "" {
|
||||
full = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(rel))
|
||||
full = fmt.Sprintf("%s/%s", c.BaseURL, url.PathEscape(rel))
|
||||
} else {
|
||||
full = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(rel))
|
||||
full = fmt.Sprintf("%s%s/%s", c.BaseURL, u, url.PathEscape(rel))
|
||||
}
|
||||
full = strings.ReplaceAll(full, "%2F", "/")
|
||||
|
||||
@@ -247,18 +250,18 @@ func (c *WebDAVClient) Move(ctx context.Context, sourcePath, targetPath string)
|
||||
// Build source URL
|
||||
var sourceURL string
|
||||
if u == "" {
|
||||
sourceURL = fmt.Sprintf("%s/%s", c.baseURL, url.PathEscape(sourceRel))
|
||||
sourceURL = fmt.Sprintf("%s/%s", c.BaseURL, url.PathEscape(sourceRel))
|
||||
} else {
|
||||
sourceURL = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(sourceRel))
|
||||
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))
|
||||
targetURL = fmt.Sprintf("%s/%s", c.BaseURL, url.PathEscape(targetRel))
|
||||
} else {
|
||||
targetURL = fmt.Sprintf("%s%s/%s", c.baseURL, u, url.PathEscape(targetRel))
|
||||
targetURL = fmt.Sprintf("%s%s/%s", c.BaseURL, u, url.PathEscape(targetRel))
|
||||
}
|
||||
targetURL = strings.ReplaceAll(targetURL, "%2F", "/")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user