Fix document viewer: add dynamic base URL and debug logging for file downloads
This commit is contained in:
@@ -169,9 +169,16 @@ class _DocumentViewerModalState extends State<DocumentViewerModal> {
|
|||||||
}
|
}
|
||||||
if (state is DocumentViewerReady) {
|
if (state is DocumentViewerReady) {
|
||||||
if (state.caps.isPdf) {
|
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(
|
return SfPdfViewer.network(
|
||||||
state.viewUrl.toString(),
|
state.viewUrl.toString(),
|
||||||
headers: {'Authorization': 'Bearer ${state.token}'},
|
headers: {'Authorization': 'Bearer ${state.token}'},
|
||||||
|
onDocumentLoadFailed: (details) {
|
||||||
|
print('PDF load failed: ${details.error}');
|
||||||
|
print('Description: ${details.description}');
|
||||||
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return Container(
|
return Container(
|
||||||
|
|||||||
@@ -365,8 +365,16 @@ func viewerHandler(w http.ResponseWriter, r *http.Request, db *database.DB, audi
|
|||||||
// Log activity
|
// Log activity
|
||||||
db.LogActivity(r.Context(), userID, orgID, &fileId, "view_file", map[string]interface{}{})
|
db.LogActivity(r.Context(), userID, orgID, &fileId, "view_file", map[string]interface{}{})
|
||||||
|
|
||||||
// Build download URL with proper URL encoding
|
// Build download URL with proper URL encoding using the request's scheme and host
|
||||||
downloadPath := fmt.Sprintf("https://go.b0esche.cloud/orgs/%s/files/download?path=%s", orgID.String(), url.QueryEscape(file.Path))
|
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
|
// Determine if it's a PDF based on file extension
|
||||||
isPdf := strings.HasSuffix(strings.ToLower(file.Name), ".pdf")
|
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
|
// Optionally log activity without org id
|
||||||
db.LogActivity(r.Context(), userID, uuid.Nil, &fileId, "view_user_file", map[string]interface{}{})
|
db.LogActivity(r.Context(), userID, uuid.Nil, &fileId, "view_user_file", map[string]interface{}{})
|
||||||
|
|
||||||
// Build download URL with proper URL encoding
|
// Build download URL with proper URL encoding using the request's scheme and host
|
||||||
downloadPath := fmt.Sprintf("https://go.b0esche.cloud/user/files/download?path=%s", url.QueryEscape(file.Path))
|
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
|
// Determine if it's a PDF based on file extension
|
||||||
isPdf := strings.HasSuffix(strings.ToLower(file.Name), ".pdf")
|
isPdf := strings.HasSuffix(strings.ToLower(file.Name), ".pdf")
|
||||||
@@ -1455,11 +1471,15 @@ func downloadUserFileHandler(w http.ResponseWriter, r *http.Request, db *databas
|
|||||||
return
|
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
|
// Try to download from Nextcloud first
|
||||||
if storageClient != nil {
|
if storageClient != nil {
|
||||||
rel := strings.TrimPrefix(filePath, "/")
|
rel := strings.TrimPrefix(filePath, "/")
|
||||||
// Keep remote user workspace path consistent with uploads: "/users/<userID>/<rel>"
|
// Keep remote user workspace path consistent with uploads: "/users/<userID>/<rel>"
|
||||||
remotePath := path.Join("/users", userID.String(), 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)
|
reader, size, err := storageClient.Download(r.Context(), remotePath)
|
||||||
if err == nil {
|
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()))
|
baseDir := filepath.Clean(filepath.Join("/tmp/uploads/users", userID.String()))
|
||||||
rel := strings.TrimPrefix(filePath, "/")
|
rel := strings.TrimPrefix(filePath, "/")
|
||||||
localPath := filepath.Join(baseDir, rel)
|
localPath := filepath.Join(baseDir, rel)
|
||||||
|
fmt.Printf("[DEBUG] Trying local path: %s\n", localPath)
|
||||||
// Prevent path traversal escaping the baseDir
|
// Prevent path traversal escaping the baseDir
|
||||||
if !strings.HasPrefix(localPath, baseDir+string(os.PathSeparator)) && localPath != baseDir {
|
if !strings.HasPrefix(localPath, baseDir+string(os.PathSeparator)) && localPath != baseDir {
|
||||||
errors.WriteError(w, errors.CodeInvalidArgument, "Invalid path", http.StatusBadRequest)
|
errors.WriteError(w, errors.CodeInvalidArgument, "Invalid path", http.StatusBadRequest)
|
||||||
|
|||||||
Reference in New Issue
Block a user