From 0ff7aa78e4f508d22fc315fe1018b6358364ea34 Mon Sep 17 00:00:00 2001 From: Andrew Risse <52644157+andrewrisse@users.noreply.github.com> Date: Fri, 16 Aug 2024 10:19:58 -0600 Subject: [PATCH 1/2] feat(ui): download files --- src/leapfrogai_ui/src/lib/mocks/file-mocks.ts | 4 ++ .../(settings)/file-management/+page.svelte | 37 +++++++++++++++++++ .../file-management/file-management.test.ts | 29 +++++++++++++++ .../tests/file-management.test.ts | 26 +++++++++++++ 4 files changed, 96 insertions(+) diff --git a/src/leapfrogai_ui/src/lib/mocks/file-mocks.ts b/src/leapfrogai_ui/src/lib/mocks/file-mocks.ts index ee7457e37..c25c7eecc 100644 --- a/src/leapfrogai_ui/src/lib/mocks/file-mocks.ts +++ b/src/leapfrogai_ui/src/lib/mocks/file-mocks.ts @@ -64,3 +64,7 @@ export const mockDeleteCheck = (assistantsToReturn: LFAssistant[]) => { }) ); }; + +export const mockDownloadError = (id: string) => { + server.use(http.get(`api/files/${id}`, () => new HttpResponse(null, { status: 500 }))); +}; diff --git a/src/leapfrogai_ui/src/routes/chat/(settings)/file-management/+page.svelte b/src/leapfrogai_ui/src/routes/chat/(settings)/file-management/+page.svelte index b91d43d8f..4d1194296 100644 --- a/src/leapfrogai_ui/src/routes/chat/(settings)/file-management/+page.svelte +++ b/src/leapfrogai_ui/src/routes/chat/(settings)/file-management/+page.svelte @@ -34,6 +34,7 @@ import LFFileUploadBtn from '$components/LFFileUploadBtn.svelte'; import ConfirmFilesDeleteModal from '$components/modals/ConfirmFilesDeleteModal.svelte'; import { allFilesAndPendingUploads } from '$stores/filesStore'; + import { browser } from '$app/environment'; export let data; @@ -190,6 +191,41 @@ submit(); //upload all files }; + const handleDownload = async () => { + let currentFilename; + if (browser) { + try { + for (const id of $filesStore.selectedFileManagementFileIds) { + const res = await fetch(`/api/files/${id}`); + if (!res.ok) { + throw new Error(`Failed to fetch file with id ${id}`); + } + currentFilename = $filesStore.files.find((f) => f.id === id)?.filename; + const blob = await res.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = currentFilename || `file_${id}`; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); + document.body.removeChild(a); + } + toastStore.addToast({ + kind: 'success', + title: `File${$filesStore.selectedFileManagementFileIds.length > 1 ? 's' : ''} Downloaded` + }); + filesStore.setSelectedFileManagementFileIds([]); // deselect all + } catch { + toastStore.addToast({ + kind: 'error', + title: 'Download Failed', + subtitle: currentFilename && `Download of file ${currentFilename} failed.` + }); + } + } + }; + afterNavigate(() => { // Remove files with "uploading" status from store and invalidate the route so files are re-fetched // when the page is loaded again @@ -219,6 +255,7 @@
{#if editMode}
+ {#if deleting}