From c479c2243894d7218462775d0bb865e4ba3c7b6d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 24 Mar 2026 05:38:45 -0500 Subject: [PATCH] refac --- src/lib/apis/terminal/index.ts | 23 ++--------------------- src/lib/components/chat/FileNav.svelte | 13 ++++++------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/lib/apis/terminal/index.ts b/src/lib/apis/terminal/index.ts index 828931045..23567baf8 100644 --- a/src/lib/apis/terminal/index.ts +++ b/src/lib/apis/terminal/index.ts @@ -120,31 +120,12 @@ export const downloadFileBlob = async ( return { blob, filename }; }; -export const downloadDirectoryArchive = async ( - baseUrl: string, - apiKey: string, - path: string -): Promise<{ blob: Blob; filename: string } | null> => { - const url = `${baseUrl.replace(/\/$/, '')}/files/download?path=${encodeURIComponent(path)}`; - const res = await fetch(url, { - headers: { Authorization: `Bearer ${apiKey}` } - }).catch(() => null); - - if (!res || !res.ok) return null; - - const disposition = res.headers.get('content-disposition') ?? ''; - const match = disposition.match(/filename="?([^"]+)"?/); - const filename = match?.[1] ?? `${path.split('/').filter(Boolean).pop() ?? 'download'}.zip`; - const blob = await res.blob(); - return { blob, filename }; -}; - -export const downloadMultipleAsZip = async ( +export const archiveFromTerminal = async ( baseUrl: string, apiKey: string, paths: string[] ): Promise<{ blob: Blob; filename: string } | null> => { - const url = `${baseUrl.replace(/\/$/, '')}/files/download`; + const url = `${baseUrl.replace(/\/$/, '')}/files/archive`; const res = await fetch(url, { method: 'POST', headers: { diff --git a/src/lib/components/chat/FileNav.svelte b/src/lib/components/chat/FileNav.svelte index c41038393..8f0efda47 100644 --- a/src/lib/components/chat/FileNav.svelte +++ b/src/lib/components/chat/FileNav.svelte @@ -19,8 +19,7 @@ listFiles, readFile, downloadFileBlob, - downloadDirectoryArchive, - downloadMultipleAsZip, + archiveFromTerminal, uploadToTerminal, createDirectory, deleteEntry, @@ -408,7 +407,7 @@ // Directories end with '/' — download as ZIP archive const isDir = path.endsWith('/'); const result = isDir - ? await downloadDirectoryArchive(terminal.url, terminal.key, path.replace(/\/$/, '')) + ? await archiveFromTerminal(terminal.url, terminal.key, [path.replace(/\/$/, '')]) : await downloadFileBlob(terminal.url, terminal.key, path); if (!result) return; const url = URL.createObjectURL(result.blob); @@ -653,14 +652,14 @@ const paths = [...selectedEntries].map((p) => p.replace(/\/$/, '')); if (paths.length === 0) return; - // Single item — use the regular downloadFile path - if (paths.length === 1) { + // Single file (not dir) — use the regular downloadFile path + if (paths.length === 1 && !([...selectedEntries][0]).endsWith('/')) { await downloadFile([...selectedEntries][0]); return; } - // Multiple items — bundle into a single ZIP - const result = await downloadMultipleAsZip(terminal.url, terminal.key, paths); + // Archive everything into a single ZIP + const result = await archiveFromTerminal(terminal.url, terminal.key, paths); if (!result) return; const url = URL.createObjectURL(result.blob); const a = document.createElement('a');