Refactor WebDAV client to expose BaseURL and update avatar upload logic for improved URL handling

This commit is contained in:
Leon Bösche
2026-01-29 10:30:24 +01:00
parent 11daed18d7
commit 688cec90a8
4 changed files with 29 additions and 28 deletions

View File

@@ -4038,7 +4038,7 @@ func uploadUserAvatarHandler(w http.ResponseWriter, r *http.Request, db *databas
// Upload to Nextcloud
client := storage.NewWebDAVClient(cfg)
avatarPath := fmt.Sprintf("remote.php/dav/files/b0esche/avatars/%s", filename)
avatarPath := fmt.Sprintf("avatars/%s", filename)
err = client.Upload(r.Context(), avatarPath, bytes.NewReader(fileBytes), header.Size)
if err != nil {
errors.LogError(r, err, "Failed to upload avatar")
@@ -4049,11 +4049,12 @@ func uploadUserAvatarHandler(w http.ResponseWriter, r *http.Request, db *databas
// Get public URL - for now, construct it manually since Nextcloud doesn't provide direct public URLs
// In a real setup, you'd configure Nextcloud to serve public URLs or use a CDN
publicURL := "/user/avatar"
webdavURL := fmt.Sprintf("%s/%s", client.BaseURL, avatarPath)
// Update user profile with avatar URL
_, err = db.ExecContext(r.Context(),
`UPDATE users SET avatar_url = $1, updated_at = NOW() WHERE id = $2`,
publicURL, userID)
webdavURL, userID)
if err != nil {
errors.LogError(r, err, "Failed to update user avatar")
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
@@ -4112,16 +4113,13 @@ func getUserAvatarHandler(w http.ResponseWriter, r *http.Request, db *database.D
return
}
// Parse the avatar URL to get the remote path
// Assuming avatarURL is like https://storage.b0esche.cloud/remote.php/dav/files/b0esche/avatars/filename
baseURL := cfg.NextcloudURL
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
remotePath := strings.TrimPrefix(*avatarURL, baseURL)
// Download from WebDAV
client := storage.NewWebDAVClient(cfg)
if client == nil {
errors.WriteError(w, errors.CodeInternal, "WebDAV client not configured", http.StatusInternalServerError)
return
}
remotePath := strings.TrimPrefix(*avatarURL, client.BaseURL+"/")
resp, err := client.Download(r.Context(), remotePath, "")
if err != nil {
errors.LogError(r, err, "Failed to download avatar")