From f372172898db412c9a71898690a3f2f445db84a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6sche?= Date: Sat, 10 Jan 2026 05:21:43 +0100 Subject: [PATCH] Fix document viewer: add dynamic base URL and debug logging for file downloads --- b0esche_cloud/lib/pages/document_viewer.dart | 7 +++++ go_cloud/internal/http/routes.go | 29 +++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/b0esche_cloud/lib/pages/document_viewer.dart b/b0esche_cloud/lib/pages/document_viewer.dart index 8c7de15..a0b2388 100644 --- a/b0esche_cloud/lib/pages/document_viewer.dart +++ b/b0esche_cloud/lib/pages/document_viewer.dart @@ -169,9 +169,16 @@ class _DocumentViewerModalState extends State { } if (state is DocumentViewerReady) { if (state.caps.isPdf) { + // Log the URL being used for debugging + print('Loading PDF from: ${state.viewUrl}'); + print('Using token: ${state.token.substring(0, 20)}...'); return SfPdfViewer.network( state.viewUrl.toString(), headers: {'Authorization': 'Bearer ${state.token}'}, + onDocumentLoadFailed: (details) { + print('PDF load failed: ${details.error}'); + print('Description: ${details.description}'); + }, ); } else { return Container( diff --git a/go_cloud/internal/http/routes.go b/go_cloud/internal/http/routes.go index a9abdf0..f1aaf55 100644 --- a/go_cloud/internal/http/routes.go +++ b/go_cloud/internal/http/routes.go @@ -365,8 +365,16 @@ func viewerHandler(w http.ResponseWriter, r *http.Request, db *database.DB, audi // Log activity db.LogActivity(r.Context(), userID, orgID, &fileId, "view_file", map[string]interface{}{}) - // Build download URL with proper URL encoding - downloadPath := fmt.Sprintf("https://go.b0esche.cloud/orgs/%s/files/download?path=%s", orgID.String(), url.QueryEscape(file.Path)) + // Build download URL with proper URL encoding using the request's scheme and host + scheme := "https" + if r.TLS == nil { + scheme = "http" + } + host := r.Host + if host == "" { + host = "go.b0esche.cloud" + } + downloadPath := fmt.Sprintf("%s://%s/orgs/%s/files/download?path=%s", scheme, host, orgID.String(), url.QueryEscape(file.Path)) // Determine if it's a PDF based on file extension isPdf := strings.HasSuffix(strings.ToLower(file.Name), ".pdf") @@ -421,8 +429,16 @@ func userViewerHandler(w http.ResponseWriter, r *http.Request, db *database.DB, // Optionally log activity without org id db.LogActivity(r.Context(), userID, uuid.Nil, &fileId, "view_user_file", map[string]interface{}{}) - // Build download URL with proper URL encoding - downloadPath := fmt.Sprintf("https://go.b0esche.cloud/user/files/download?path=%s", url.QueryEscape(file.Path)) + // Build download URL with proper URL encoding using the request's scheme and host + scheme := "https" + if r.TLS == nil { + scheme = "http" + } + host := r.Host + if host == "" { + host = "go.b0esche.cloud" + } + downloadPath := fmt.Sprintf("%s://%s/user/files/download?path=%s", scheme, host, url.QueryEscape(file.Path)) // Determine if it's a PDF based on file extension isPdf := strings.HasSuffix(strings.ToLower(file.Name), ".pdf") @@ -1455,11 +1471,15 @@ func downloadUserFileHandler(w http.ResponseWriter, r *http.Request, db *databas return } + // Log the requested file path for debugging + fmt.Printf("[DEBUG] Download request - User: %s, Path: %s\n", userID.String(), filePath) + // Try to download from Nextcloud first if storageClient != nil { rel := strings.TrimPrefix(filePath, "/") // Keep remote user workspace path consistent with uploads: "/users//" remotePath := path.Join("/users", userID.String(), rel) + fmt.Printf("[DEBUG] Trying WebDAV path: %s\n", remotePath) reader, size, err := storageClient.Download(r.Context(), remotePath) if err == nil { @@ -1494,6 +1514,7 @@ func downloadUserFileHandler(w http.ResponseWriter, r *http.Request, db *databas baseDir := filepath.Clean(filepath.Join("/tmp/uploads/users", userID.String())) rel := strings.TrimPrefix(filePath, "/") localPath := filepath.Join(baseDir, rel) + fmt.Printf("[DEBUG] Trying local path: %s\n", localPath) // Prevent path traversal escaping the baseDir if !strings.HasPrefix(localPath, baseDir+string(os.PathSeparator)) && localPath != baseDir { errors.WriteError(w, errors.CodeInvalidArgument, "Invalid path", http.StatusBadRequest)