small refactor
This commit is contained in:
parent
5b2cffbd00
commit
fc77b286b7
1 changed files with 36 additions and 14 deletions
50
content.js
50
content.js
|
|
@ -55,7 +55,7 @@ async function downloadApplicantFilesWithProgress(applicantDetails, token, onPro
|
||||||
throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}`
|
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
|
completedDownloads += 1
|
||||||
downloadedBytes += afile.bytes.length
|
downloadedBytes += afile.bytes.length
|
||||||
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
|
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
|
||||||
|
|
@ -120,7 +120,7 @@ async function getApplicants() {
|
||||||
return {
|
return {
|
||||||
aid,
|
aid,
|
||||||
token,
|
token,
|
||||||
applicants: jsonData//.slice(0, 5)
|
applicants: jsonData
|
||||||
}
|
}
|
||||||
// return { aid, token, applicants: jsonData }
|
// return { aid, token, applicants: jsonData }
|
||||||
})
|
})
|
||||||
|
|
@ -287,24 +287,46 @@ function getFflate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createZipBlob(archiveEntries) {
|
async function createZipBlob(archiveEntries) {
|
||||||
|
const { Zip, ZipPassThrough } = getFflate()
|
||||||
console.log(archiveEntries)
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
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) {
|
if (error != null) {
|
||||||
|
settled = true
|
||||||
reject(error)
|
reject(error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(data)
|
chunks.push(chunk)
|
||||||
})
|
|
||||||
// resolve(fflate.zipSync(archiveEntries, { level: 0, consume: true }))
|
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) {
|
function downloadBlob(blob, fileName) {
|
||||||
|
|
@ -508,7 +530,7 @@ function rip(event) {
|
||||||
|
|
||||||
archiveFiles.forEach((archiveFile, index) => {
|
archiveFiles.forEach((archiveFile, index) => {
|
||||||
const afile = applicantDetails.application_files[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...")
|
console.log("Generating zip archive...")
|
||||||
|
|
||||||
createZipBlob(archiveEntries)
|
return createZipBlob(archiveEntries)
|
||||||
.then((zipBlob) => {
|
.then((zipBlob) => {
|
||||||
console.log("Zip archive is ready.")
|
console.log("Zip archive is ready.")
|
||||||
progressDialog.setStatus("Download ready.")
|
progressDialog.setStatus("Download ready.")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue