small refactor
This commit is contained in:
parent
5b2cffbd00
commit
fc77b286b7
1 changed files with 36 additions and 14 deletions
48
content.js
48
content.js
|
|
@ -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,23 +287,45 @@ 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)
|
||||
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)
|
||||
})
|
||||
// resolve(fflate.zipSync(archiveEntries, { level: 0, consume: true }))
|
||||
})
|
||||
.then((archiveBytes) => {
|
||||
console.log("producing blob")
|
||||
return new Blob([archiveBytes], { type: "application/zip" })
|
||||
|
||||
zip.end()
|
||||
} catch (error) {
|
||||
settled = true
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -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.")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue