From 1ff48ab86dffbd6daac4922c41f053e7337b70a9 Mon Sep 17 00:00:00 2001 From: "Gan, Jimmy" Date: Thu, 9 Apr 2026 22:55:44 +0800 Subject: [PATCH] fix: add authentication to file downloads --- dashboard/frontend/src/lib/api.js | 22 ++++++++++++++++++++++ dashboard/frontend/src/routes/Files.svelte | 8 ++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/dashboard/frontend/src/lib/api.js b/dashboard/frontend/src/lib/api.js index 66e3ed8..faba7bc 100644 --- a/dashboard/frontend/src/lib/api.js +++ b/dashboard/frontend/src/lib/api.js @@ -197,3 +197,25 @@ export function getInfoEngineItem(id) { export function put(path, data) { return request(path, { method: "PUT", json: data }); } + +export async function download(path, filename) { + const headers = {}; + if (token) headers["Authorization"] = `Bearer ${token}`; + + const r = await fetch(`${BASE}/files/download?path=${encodeURIComponent(path)}`, { + method: "GET", + headers, + }); + + if (!r.ok) throw new Error(`HTTP ${r.status}`); + + const blob = await r.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = filename; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); + document.body.removeChild(a); +} diff --git a/dashboard/frontend/src/routes/Files.svelte b/dashboard/frontend/src/routes/Files.svelte index 0b646a4..fbeb3af 100644 --- a/dashboard/frontend/src/routes/Files.svelte +++ b/dashboard/frontend/src/routes/Files.svelte @@ -1,6 +1,6 @@