From 4e1f3f67202bae05b7774cfbd1df0e60b319d203 Mon Sep 17 00:00:00 2001 From: chark1es Date: Thu, 13 Feb 2025 00:26:26 -0800 Subject: [PATCH] i forgot what i did --- src/components/dashboard/EventsSection.astro | 33 +++++++++------- src/components/pocketbase/FileManager.ts | 40 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/components/dashboard/EventsSection.astro b/src/components/dashboard/EventsSection.astro index ae2d6c9..4f2affd 100644 --- a/src/components/dashboard/EventsSection.astro +++ b/src/components/dashboard/EventsSection.astro @@ -1106,19 +1106,11 @@ import JSZip from "jszip"; }; // 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 { @@ -1146,6 +1138,9 @@ import JSZip from "jszip"; const filePromises = event.files.map(async (filename: string) => { const fileUrl = `${baseUrl}/api/files/${collectionId}/${recordId}/${filename}`; const response = await fetch(fileUrl); + if (!response.ok) { + throw new Error(`Failed to download ${filename}`); + } const blob = await response.blob(); zip.file(filename, blob); }); @@ -1163,14 +1158,26 @@ import JSZip from "jszip"; document.body.removeChild(link); URL.revokeObjectURL(downloadUrl); - // Reset button state - downloadBtn.innerHTML = originalBtnContent; - downloadBtn.disabled = false; - } catch (error) { + // Show success message + createToast("Files downloaded successfully!", "success"); + } catch (error: any) { console.error("Failed to download files:", error); - createToast("Failed to download files. Please try again.", "error"); + createToast( + error?.message || "Failed to download files. Please try again.", + "error" + ); + } finally { + // Reset button state downloadBtn.innerHTML = originalBtnContent; downloadBtn.disabled = false; } }; + + // Add TypeScript interface for Window + declare global { + interface Window { + downloadAllFiles: () => Promise; + [key: string]: any; + } + } diff --git a/src/components/pocketbase/FileManager.ts b/src/components/pocketbase/FileManager.ts index e3249be..2602b67 100644 --- a/src/components/pocketbase/FileManager.ts +++ b/src/components/pocketbase/FileManager.ts @@ -227,4 +227,44 @@ export class FileManager { this.auth.setUpdating(false); } } + + /** + * Get multiple files from a record + * @param collectionName The name of the collection + * @param recordId The ID of the record containing the files + * @param field The field name containing the files + * @returns Array of file URLs + */ + public async getFiles( + collectionName: string, + recordId: string, + field: string + ): Promise { + if (!this.auth.isAuthenticated()) { + throw new Error("User must be authenticated to get files"); + } + + try { + this.auth.setUpdating(true); + const pb = this.auth.getPocketBase(); + + // Get the record to retrieve the filenames + const record = await pb.collection(collectionName).getOne(recordId); + + // Get the filenames from the specified field + const filenames = record[field] || []; + + // Convert filenames to URLs + const fileUrls = filenames.map((filename: string) => + this.getFileUrl(collectionName, recordId, filename) + ); + + return fileUrls; + } catch (err) { + console.error(`Failed to get files from ${collectionName}:`, err); + throw err; + } finally { + this.auth.setUpdating(false); + } + } } \ No newline at end of file