2023-05-05 00:19:05 +02:00
|
|
|
async function downloadFilesWithCallback(processFileCallback) {
|
2024-02-16 22:49:06 +01:00
|
|
|
const fileInput = document.querySelector('input[type="file"]');
|
|
|
|
const files = fileInput.files;
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const zipThreshold = 4;
|
|
|
|
const zipFiles = files.length > zipThreshold;
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
let jszip = null;
|
|
|
|
if (zipFiles) {
|
|
|
|
jszip = new JSZip();
|
|
|
|
}
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const promises = Array.from(files).map(async (file) => {
|
|
|
|
const { processedData, fileName } = await processFileCallback(file);
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
if (zipFiles) {
|
|
|
|
jszip.file(fileName, processedData);
|
|
|
|
} else {
|
|
|
|
const url = URL.createObjectURL(processedData);
|
|
|
|
const downloadOption = localStorage.getItem("downloadOption");
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
if (downloadOption === "sameWindow") {
|
|
|
|
window.location.href = url;
|
|
|
|
} else if (downloadOption === "newWindow") {
|
|
|
|
window.open(url, "_blank");
|
|
|
|
} else {
|
|
|
|
const downloadLink = document.createElement("a");
|
|
|
|
downloadLink.href = url;
|
|
|
|
downloadLink.download = fileName;
|
|
|
|
downloadLink.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
await Promise.all(promises);
|
2023-05-05 00:19:05 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
if (zipFiles) {
|
|
|
|
const content = await jszip.generateAsync({ type: "blob" });
|
|
|
|
const url = URL.createObjectURL(content);
|
|
|
|
const a = document.createElement("a");
|
|
|
|
a.href = url;
|
|
|
|
a.download = "files.zip";
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
a.remove();
|
|
|
|
}
|
2023-05-05 00:19:05 +02:00
|
|
|
}
|