Skip to content

Commit 0c955aa

Browse files
committed
feat: Add new EJS view templates for file detail, my files, recent files, and upload pages.
1 parent d9fb228 commit 0c955aa

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

src/client/views/pages/detail.ejs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,19 @@
439439
let zoomLevel = 100;
440440
let rotation = 0;
441441
442+
const getAccessToken = () => {
443+
const readCookie = (name) => {
444+
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
445+
if (match) return match[2];
446+
};
447+
return localStorage.getItem("accessToken") || readCookie("accessToken") || "";
448+
};
449+
450+
const getAuthHeaders = () => {
451+
const token = getAccessToken();
452+
return token ? { 'Authorization': `Bearer ${token}` } : {};
453+
};
454+
442455
// File type mappings
443456
const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico', 'tiff', 'tif', 'avif'];
444457
const videoExts = ['mp4', 'webm', 'ogg', 'ogv', 'mov', 'avi', 'mkv'];
@@ -613,7 +626,10 @@
613626
let fileSize = null;
614627
let lastModified = null;
615628
try {
616-
const headRes = await fetch(fileUrl, { method: 'HEAD' });
629+
const headRes = await fetch(fileUrl, {
630+
method: 'HEAD',
631+
headers: getAuthHeaders()
632+
});
617633
if (headRes.ok) {
618634
fileSize = parseInt(headRes.headers.get('content-length') || '0');
619635
lastModified = headRes.headers.get('last-modified');
@@ -668,7 +684,9 @@
668684
document.getElementById('preview-pdf').src = fileUrl;
669685
pdfPreviewContainer.classList.remove('hidden');
670686
} else if (isCode || isText) {
671-
const res = await fetch(fileUrl);
687+
const res = await fetch(fileUrl, {
688+
headers: getAuthHeaders()
689+
});
672690
if (!res.ok) throw new Error('Failed to load file');
673691
const text = await res.text();
674692
document.getElementById('preview-text').textContent = text;

src/client/views/pages/my-file.ejs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@
177177
</div>
178178
179179
<script type="module">
180+
const getAccessToken = () => {
181+
const readCookie = (name) => {
182+
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
183+
if (match) return match[2];
184+
};
185+
return localStorage.getItem("accessToken") || readCookie("accessToken") || "";
186+
};
187+
188+
const getAuthHeaders = () => {
189+
const token = getAccessToken();
190+
return token ? { 'Authorization': `Bearer ${token}` } : {};
191+
};
192+
180193
// Initialize delete action handlers
181194
const initDeleteActions = () => {
182195
document.querySelectorAll('[data-action="delete"]').forEach(btn => {
@@ -194,7 +207,8 @@
194207
if (confirm(`Are you sure you want to delete "${filename}"?`)) {
195208
try {
196209
const res = await fetch(`/api/file/delete/${filename}`, {
197-
method: 'DELETE'
210+
method: 'DELETE',
211+
headers: getAuthHeaders()
198212
});
199213
200214
if (res.ok) {

src/client/views/pages/recent.ejs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@
210210
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
211211
};
212212
213+
const getAccessToken = () => {
214+
const readCookie = (name) => {
215+
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
216+
if (match) return match[2];
217+
};
218+
return localStorage.getItem("accessToken") || readCookie("accessToken") || "";
219+
};
220+
221+
const getAuthHeaders = () => {
222+
const token = getAccessToken();
223+
return token ? { 'Authorization': `Bearer ${token}` } : {};
224+
};
225+
213226
const timeAgo = (isoString) => {
214227
if (!isoString) return '';
215228
const time = new Date(isoString).getTime();
@@ -523,7 +536,10 @@
523536
const filename = btn.getAttribute('data-filename');
524537
if (filename && confirm('Are you sure you want to delete this file?')) {
525538
try {
526-
const res = await fetch(`/api/file/delete/${encodeURIComponent(filename)}`, { method: 'DELETE' });
539+
const res = await fetch(`/api/file/delete/${encodeURIComponent(filename)}`, {
540+
method: 'DELETE',
541+
headers: getAuthHeaders()
542+
});
527543
if (res.ok) {
528544
loadFiles();
529545
} else {
@@ -554,7 +570,9 @@
554570
// Load files
555571
const loadFiles = async () => {
556572
try {
557-
const response = await fetch('/api/database/files');
573+
const response = await fetch('/api/database/files', {
574+
headers: getAuthHeaders()
575+
});
558576
if (!response.ok) throw new Error('Failed to load files');
559577
const data = await response.json();
560578
allFiles = Array.isArray(data?.result) ? data.result : [];

src/client/views/pages/upload.ejs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@
189189
return new Date(isoString).toLocaleDateString();
190190
};
191191
192+
const getAccessToken = () => {
193+
const readCookie = (name) => {
194+
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
195+
if (match) return match[2];
196+
};
197+
return localStorage.getItem("accessToken") || readCookie("accessToken") || "";
198+
};
199+
200+
const getAuthHeaders = () => {
201+
const token = getAccessToken();
202+
return token ? { 'Authorization': `Bearer ${token}` } : {};
203+
};
204+
192205
const renderRecentUploads = (files) => {
193206
if (!recentUploads) return;
194207
if (!files || files.length === 0) {
@@ -214,7 +227,9 @@
214227
const loadRecentUploads = async () => {
215228
if (!recentUploads) return;
216229
try {
217-
const response = await fetch('/api/database/files');
230+
const response = await fetch('/api/database/files', {
231+
headers: getAuthHeaders()
232+
});
218233
if (!response.ok) {
219234
throw new Error('Failed to load uploads');
220235
}
@@ -369,6 +384,12 @@
369384
const xhr = new XMLHttpRequest();
370385
xhr.open('POST', uploadForm.action, true);
371386
387+
// Add Authorization header
388+
const token = getAccessToken();
389+
if (token) {
390+
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
391+
}
392+
372393
if (storagePath && storagePath.value.trim()) {
373394
xhr.setRequestHeader('storage', storagePath.value.trim());
374395
}

0 commit comments

Comments
 (0)