i forgot what i did
This commit is contained in:
parent
b0cb20d0f4
commit
4e1f3f6720
2 changed files with 60 additions and 13 deletions
|
@ -1106,19 +1106,11 @@ import JSZip from "jszip";
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add downloadAllFiles function
|
// Add downloadAllFiles function
|
||||||
declare global {
|
|
||||||
interface Window {
|
|
||||||
downloadAllFiles: () => Promise<void>;
|
|
||||||
[key: string]: any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.downloadAllFiles = async function () {
|
window.downloadAllFiles = async function () {
|
||||||
const downloadBtn = document.getElementById(
|
const downloadBtn = document.getElementById(
|
||||||
"downloadAllBtn"
|
"downloadAllBtn"
|
||||||
) as HTMLButtonElement;
|
) as HTMLButtonElement;
|
||||||
if (!downloadBtn) return;
|
if (!downloadBtn) return;
|
||||||
|
|
||||||
const originalBtnContent = downloadBtn.innerHTML;
|
const originalBtnContent = downloadBtn.innerHTML;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -1146,6 +1138,9 @@ import JSZip from "jszip";
|
||||||
const filePromises = event.files.map(async (filename: string) => {
|
const filePromises = event.files.map(async (filename: string) => {
|
||||||
const fileUrl = `${baseUrl}/api/files/${collectionId}/${recordId}/${filename}`;
|
const fileUrl = `${baseUrl}/api/files/${collectionId}/${recordId}/${filename}`;
|
||||||
const response = await fetch(fileUrl);
|
const response = await fetch(fileUrl);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to download ${filename}`);
|
||||||
|
}
|
||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
zip.file(filename, blob);
|
zip.file(filename, blob);
|
||||||
});
|
});
|
||||||
|
@ -1163,14 +1158,26 @@ import JSZip from "jszip";
|
||||||
document.body.removeChild(link);
|
document.body.removeChild(link);
|
||||||
URL.revokeObjectURL(downloadUrl);
|
URL.revokeObjectURL(downloadUrl);
|
||||||
|
|
||||||
// Reset button state
|
// Show success message
|
||||||
downloadBtn.innerHTML = originalBtnContent;
|
createToast("Files downloaded successfully!", "success");
|
||||||
downloadBtn.disabled = false;
|
} catch (error: any) {
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to download files:", error);
|
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.innerHTML = originalBtnContent;
|
||||||
downloadBtn.disabled = false;
|
downloadBtn.disabled = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add TypeScript interface for Window
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
downloadAllFiles: () => Promise<void>;
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -227,4 +227,44 @@ export class FileManager {
|
||||||
this.auth.setUpdating(false);
|
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<string[]> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue