small refactor

This commit is contained in:
gapato 2026-06-08 19:59:55 +02:00
commit fc77b286b7

View file

@ -55,7 +55,7 @@ async function downloadApplicantFilesWithProgress(applicantDetails, token, onPro
throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}`
}
afile.bytes = new Uint8Array(await response.arrayBuffer())
afile.bytes = await response.bytes()
completedDownloads += 1
downloadedBytes += afile.bytes.length
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
@ -120,7 +120,7 @@ async function getApplicants() {
return {
aid,
token,
applicants: jsonData//.slice(0, 5)
applicants: jsonData
}
// return { aid, token, applicants: jsonData }
})
@ -287,24 +287,46 @@ function getFflate() {
}
async function createZipBlob(archiveEntries) {
console.log(archiveEntries)
const { Zip, ZipPassThrough } = getFflate()
return new Promise((resolve, reject) => {
fflate.zip(archiveEntries, { mem: 9, level: 0, consume: true }, (error, data) => {
const chunks = []
const zip = new Zip()
let settled = false
zip.ondata = (error, chunk, final) => {
if (settled) {
return
}
if (error != null) {
settled = true
reject(error)
return
}
resolve(data)
})
// resolve(fflate.zipSync(archiveEntries, { level: 0, consume: true }))
chunks.push(chunk)
if (final) {
settled = true
resolve(new Blob(chunks, { type: "application/zip" }))
}
}
try {
Object.entries(archiveEntries).forEach(([path, bytes]) => {
const file = new ZipPassThrough(path)
zip.add(file)
file.push(bytes, true)
})
zip.end()
} catch (error) {
settled = true
reject(error)
}
})
.then((archiveBytes) => {
console.log("producing blob")
return new Blob([archiveBytes], { type: "application/zip" })
})
}
function downloadBlob(blob, fileName) {
@ -508,7 +530,7 @@ function rip(event) {
archiveFiles.forEach((archiveFile, index) => {
const afile = applicantDetails.application_files[index]
archiveEntries[archiveFile.relativePath] = afile.bytes
archiveEntries[archiveFile.relativePath] = new Uint8Array(afile.bytes)
})
})
@ -528,7 +550,7 @@ function rip(event) {
console.log("Generating zip archive...")
createZipBlob(archiveEntries)
return createZipBlob(archiveEntries)
.then((zipBlob) => {
console.log("Zip archive is ready.")
progressDialog.setStatus("Download ready.")