Files
FEDEO/spaces/pages/downloadFolder.vue
flfeders 5503c572f1 Added Logo
Added Document Download
Added zipjs
2023-12-11 12:04:32 +01:00

90 lines
1.9 KiB
Vue

<script setup>
import { BlobReader, BlobWriter, ZipWriter } from "@zip.js/zip.js";
const downloadFolder = async (folder) => {
const supabaseClient = useSupabaseClient();
const bucket = "documents";
// Get a list of all the files in the path /my-bucket/images
/*const { data: files, error } = await supabaseClient.storage
.from(bucket)
.list(folder);
if (error) {
throw error;
}*/
let files = [
"1/Eingang/Rechnung_VRB170A0249604_2023-12-06.pdf"
]
console.log(files)
// If there are no files in the folder, throw an error
if (!files || !files.length) {
throw new Error("No files to download");
}
const promises = [];
// Download each file in the folder
files.forEach((file) => {
promises.push(
supabaseClient.storage.from(bucket).download(`${file}`)
);
});
// Wait for all the files to download
const response = await Promise.allSettled(promises);
// Map the response to an array of objects containing the file name and blob
const downloadedFiles = response.map((result, index) => {
if (result.status === "fulfilled") {
return {
name: files[index],
blob: result.value.data,
};
}
});
// Create a new zip file
const zipFileWriter = new BlobWriter("application/zip");
const zipWriter = new ZipWriter(zipFileWriter, { bufferedWrite: true });
// Add each file to the zip file
downloadedFiles.forEach((downloadedFile) => {
if (downloadedFile) {
zipWriter.add(downloadedFile.name, new BlobReader(downloadedFile.blob));
}
});
// Download the zip file
const url = URL.createObjectURL(await zipWriter.close());
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "documents.zip");
document.body.appendChild(link);
link.click();
}
</script>
<template>
<div>
<UButton
@click="downloadFolder('1/Eingang')"
>
Download
</UButton>
</div>
</template>
<style scoped>
</style>