Fix document viewer: add dynamic base URL and debug logging for file downloads

This commit is contained in:
Leon Bösche
2026-01-10 05:21:43 +01:00
parent fa6ba846d8
commit f372172898
2 changed files with 32 additions and 4 deletions

View File

@@ -169,9 +169,16 @@ class _DocumentViewerModalState extends State<DocumentViewerModal> {
}
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(

View File

@@ -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/<userID>/<rel>"
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)