fix: resolve file viewer auth issues and add text file viewing support

- Fix file viewer not sending JWT auth tokens when loading files
  - inlineViewer.js: already used XHR with auth (images/PDFs worked)
  - fileViewer.js: was setting img.src/iframe.src directly without auth headers,
    now uses fetch with Bearer token and blob URLs
  - ui.js/contextMenus.js/fileRenderer.js/recent.js/favorites.js: replaced all
    window.location.href = /api/files/... (unauthenticated navigation) with
    authenticated viewer or fileOps.downloadFile()

- Add text file viewing support (text/*, application/json, etc.)
  - New createTextViewer() in inlineViewer.js with authenticated fetch
  - New loadTextViewer() in fileViewer.js with authenticated fetch
  - New isViewableFile() helper in ui.js used across all entry points
  - CSS styles for .inline-viewer-text-content and .file-viewer-text-content

- Translate remaining Spanish strings to English in viewer files

Fixes: text files showing 'Token not provided', images failing to load,
and text files not being previewable at all.
This commit is contained in:
Dionisio
2026-02-12 11:16:58 +01:00
parent 6ca1ac4294
commit 3c03caaf60
9 changed files with 313 additions and 62 deletions
+24 -8
View File
@@ -434,6 +434,24 @@ const ui = {
}
},
/**
* Check if a file can be previewed in the viewer
* @param {Object} file - File object with mime_type property
* @returns {boolean}
*/
isViewableFile(file) {
if (!file || !file.mime_type) return false;
if (file.mime_type.startsWith('image/')) return true;
if (file.mime_type === 'application/pdf') return true;
if (file.mime_type.startsWith('text/')) return true;
const textTypes = [
'application/json', 'application/xml', 'application/javascript',
'application/x-sh', 'application/x-yaml', 'application/toml',
'application/x-toml', 'application/sql',
];
return textTypes.includes(file.mime_type);
},
/**
* Show notification
* @param {string} title - Notification title
@@ -957,17 +975,16 @@ const ui = {
}
// Check if it's a viewable file type
if ((file.mime_type && file.mime_type.startsWith('image/')) ||
(file.mime_type && file.mime_type === 'application/pdf')) {
if (this.isViewableFile(file)) {
if (window.inlineViewer) {
window.inlineViewer.openFile(file);
} else if (window.fileViewer) {
window.fileViewer.open(file);
} else {
window.location.href = `/api/files/${file.id}`;
window.fileOps.downloadFile(file.id, file.name);
}
} else {
window.location.href = `/api/files/${file.id}`;
window.fileOps.downloadFile(file.id, file.name);
}
});
@@ -1051,8 +1068,7 @@ const ui = {
}
// Check if it's a viewable file type
if ((file.mime_type && file.mime_type.startsWith('image/')) ||
(file.mime_type && file.mime_type === 'application/pdf')) {
if (this.isViewableFile(file)) {
// Open in the inline viewer
if (window.inlineViewer) {
window.inlineViewer.openFile(file);
@@ -1061,11 +1077,11 @@ const ui = {
window.fileViewer.open(file);
} else {
// No viewer available, download directly
window.location.href = `/api/files/${file.id}`;
window.fileOps.downloadFile(file.id, file.name);
}
} else {
// For other file types, download directly
window.location.href = `/api/files/${file.id}`;
window.fileOps.downloadFile(file.id, file.name);
}
});