From 1aee8686fafc71e3d48b071d577604c121407b2b Mon Sep 17 00:00:00 2001 From: chark1es Date: Tue, 11 Feb 2025 02:07:40 -0800 Subject: [PATCH] add download button for files --- src/components/dashboard/EventsSection.astro | 152 ++++++++++++++++--- 1 file changed, 127 insertions(+), 25 deletions(-) diff --git a/src/components/dashboard/EventsSection.astro b/src/components/dashboard/EventsSection.astro index 0995229..f82a556 100644 --- a/src/components/dashboard/EventsSection.astro +++ b/src/components/dashboard/EventsSection.astro @@ -139,24 +139,44 @@ import { Icon } from "astro-icon/components";

Event Files

- + + + + Download All + + +
@@ -713,11 +733,15 @@ import { Icon } from "astro-icon/components"; event.files && event.files.length > 0 ? ` - - ` + + ` : ""; })()}
@@ -783,9 +807,13 @@ import { Icon } from "astro-icon/components"; ${ event.files && event.files.length > 0 ? ` - ` : "" @@ -1076,4 +1104,78 @@ import { Icon } from "astro-icon/components"; modal.showModal(); }; + + // Add downloadAllFiles function + declare global { + interface Window { + downloadAllFiles: () => Promise; + [key: string]: any; + } + } + + window.downloadAllFiles = async function () { + const downloadBtn = document.getElementById( + "downloadAllBtn" + ) as HTMLButtonElement; + if (!downloadBtn) return; + + const originalBtnContent = downloadBtn.innerHTML; + + try { + // Show loading state + downloadBtn.innerHTML = + ' Preparing...'; + downloadBtn.disabled = true; + + // Load JSZip dynamically + // @ts-ignore - Dynamically importing JSZip from CDN + const JSZip = ( + await import("https://cdn.jsdelivr.net/npm/jszip@3.10.1/+esm") + ).default; + const zip = new JSZip(); + + // Get current event files + const baseUrl = "https://pocketbase.ieeeucsd.org"; + const collectionId = "events"; + const recordId = currentEventId; + + // Get the current event from the window object + const eventDataId = `event_${currentEventId}`; + const event = window[eventDataId] as Event; + + if (!event || !event.files || event.files.length === 0) { + throw new Error("No files available to download"); + } + + // Download each file and add to zip + const filePromises = event.files.map(async (filename: string) => { + const fileUrl = `${baseUrl}/api/files/${collectionId}/${recordId}/${filename}`; + const response = await fetch(fileUrl); + const blob = await response.blob(); + zip.file(filename, blob); + }); + + await Promise.all(filePromises); + + // Generate and download zip + const zipBlob = await zip.generateAsync({ type: "blob" }); + const downloadUrl = URL.createObjectURL(zipBlob); + const link = document.createElement("a"); + link.href = downloadUrl; + link.download = `${event.event_name}_files.zip`; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + URL.revokeObjectURL(downloadUrl); + + // Reset button state + downloadBtn.innerHTML = originalBtnContent; + downloadBtn.disabled = false; + } catch (error) { + console.error("Failed to download files:", error); + createToast("Failed to download files. Please try again.", "error"); + downloadBtn.innerHTML = originalBtnContent; + downloadBtn.disabled = false; + } + };