add download button for files

This commit is contained in:
chark1es 2025-02-11 02:07:40 -08:00
parent 14d038d066
commit 1aee8686fa

View file

@ -139,24 +139,44 @@ import { Icon } from "astro-icon/components";
<div class="flex items-center gap-3">
<h3 class="font-bold text-lg" id="modalTitle">Event Files</h3>
</div>
<button
class="btn btn-circle btn-ghost"
onclick="eventDetailsModal.close()"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
<div class="flex gap-2">
<button
id="downloadAllBtn"
class="btn btn-primary btn-sm gap-2"
onclick="window.downloadAllFiles()"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
Download All
</button>
<button
class="btn btn-circle btn-ghost"
onclick="eventDetailsModal.close()"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
</div>
<div id="filesContent" class="space-y-4">
@ -713,11 +733,15 @@ import { Icon } from "astro-icon/components";
event.files &&
event.files.length > 0
? `
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-ghost btn-xs gap-1">
<Icon name="heroicons:folder-open" class="w-4 h-4" />
Files (${event.files.length})
</button>
`
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-sm btn-primary w-[90px] inline-flex items-center justify-center">
<div class="flex items-center gap-1">
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M5 19a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4l2 2h4a2 2 0 0 1 2 2v1M5 19h14a2 2 0 0 0 2-2v-5a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2z" />
</svg>
<span>Files</span>
</div>
</button>
`
: "";
})()}
</div>
@ -783,9 +807,13 @@ import { Icon } from "astro-icon/components";
${
event.files && event.files.length > 0
? `
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-ghost btn-xs gap-1">
<Icon name="heroicons:folder-open" class="w-4 h-4" />
Files (${event.files.length})
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-sm btn-primary w-[90px] inline-flex items-center justify-center">
<div class="flex items-center gap-1">
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M5 19a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4l2 2h4a2 2 0 0 1 2 2v1M5 19h14a2 2 0 0 0 2-2v-5a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2z" />
</svg>
<span>Files</span>
</div>
</button>
`
: ""
@ -1076,4 +1104,78 @@ import { Icon } from "astro-icon/components";
modal.showModal();
};
// Add downloadAllFiles function
declare global {
interface Window {
downloadAllFiles: () => Promise<void>;
[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 =
'<span class="loading loading-spinner loading-xs"></span> 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;
}
};
</script>