diff --git a/content.js b/content.js index 63338a1..51bf310 100644 --- a/content.js +++ b/content.js @@ -55,9 +55,9 @@ async function downloadApplicantFilesWithProgress(applicantDetails, token, onPro throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}` } - afile.blob = await response.blob() + afile.bytes = await response.bytes() completedDownloads += 1 - downloadedBytes += afile.blob.size + downloadedBytes += afile.bytes.length onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) })) @@ -120,7 +120,7 @@ async function getApplicants() { return { aid, token, - applicants: jsonData + applicants: jsonData//.slice(0, 5) } // return { aid, token, applicants: jsonData } }) @@ -253,7 +253,7 @@ function createApplicantsCsv(applicantDetailsList) { rows.push(values.join(",")) }) - return rows.join("\n") + return fflate.strToU8(rows.join("\n")) } // Copy the standalone viewer HTML into the generated archive. @@ -264,7 +264,7 @@ async function getArchiveViewerHtmlSource() { throw new Error("Failed to load archive viewer HTML") } - return response.text() + return response.bytes() } // Bundle the viewer's fflate runtime so the exported archive can be browsed offline. @@ -275,7 +275,7 @@ async function getArchiveViewerFflateSource() { throw new Error("Failed to load archive viewer dependency") } - return response.text() + return response.bytes() } function getFflate() { @@ -286,37 +286,12 @@ function getFflate() { return fflate } -async function createZipBlob(entries, onProgress = () => { }) { - const { strToU8, zip } = getFflate() - const archiveEntries = {} - let processedEntries = 0 +async function createZipBlob(archiveEntries) { - for (const entry of entries) { - if (entry.data instanceof Blob) { - archiveEntries[entry.path] = new Uint8Array(await entry.data.arrayBuffer()) - } else if (entry.data instanceof Uint8Array) { - archiveEntries[entry.path] = entry.data - } else { - archiveEntries[entry.path] = strToU8(String(entry.data ?? "")) - } + console.log(archiveEntries) - entry.data = null - processedEntries += 1 - onProgress({ - phase: "preparing", - processedEntries, - totalEntries: entries.length - }) - } - - onProgress({ - phase: "generating", - processedEntries, - totalEntries: entries.length - }) - - const archiveBytes = await new Promise((resolve, reject) => { - zip(archiveEntries, { level: 0, consume: true }, (error, data) => { + return new Promise((resolve, reject) => { + fflate.zip(archiveEntries, { level: 0, consume: true }, (error, data) => { if (error != null) { reject(error) return @@ -324,9 +299,12 @@ async function createZipBlob(entries, onProgress = () => { }) { resolve(data) }) + // resolve(fflate.zipSync(archiveEntries, { level: 0, consume: true })) }) - - return new Blob([archiveBytes], { type: "application/zip" }) + .then((archiveBytes) => { + console.log("producing blob") + return new Blob([archiveBytes], { type: "application/zip" }) + }) } function downloadBlob(blob, fileName) { @@ -515,8 +493,8 @@ function rip(event) { console.log("Preparing zip archive...") progressDialog.setStatus("Preparing zip archive...") - const archiveEntries = [] const successfulApplicants = [] + const archiveEntries = {} applicantDetailsResults.forEach((result) => { if (result.status !== "fulfilled") { @@ -530,10 +508,7 @@ function rip(event) { archiveFiles.forEach((archiveFile, index) => { const afile = applicantDetails.application_files[index] - archiveEntries.push({ - path: archiveFile.relativePath, - data: afile.blob - }) + archiveEntries[archiveFile.relativePath] = afile.bytes }) }) @@ -542,40 +517,24 @@ function rip(event) { getArchiveViewerFflateSource() ]) - console.log("Creating applicants.csv") - archiveEntries.push({ - path: "applicants.csv", - data: createApplicantsCsv(successfulApplicants) - }) + console.log("Creating applications.csv") + archiveEntries["applications.csv"] = createApplicantsCsv(successfulApplicants) console.log("Creating index.html") - archiveEntries.push({ - path: "viewer.html", - data: viewerHtmlSource - }) + archiveEntries["viewer.html"] = viewerHtmlSource console.log("Adding viewer Javascript to archive") - archiveEntries.push({ - path: "fflate.min.js", - data: viewerFflateSource - }) + archiveEntries["fflate.min.js"] = viewerFflateSource console.log("Generating zip archive...") - const zipBlob = await createZipBlob(archiveEntries, ({ phase, processedEntries, totalEntries }) => { - if (phase === "preparing") { - if (processedEntries === totalEntries || processedEntries === 1 || processedEntries % 10 === 0) { - progressDialog.setStatus(`Preparing zip archive (${processedEntries}/${totalEntries})...`) - } - return - } + createZipBlob(archiveEntries) + .then((zipBlob) => { + console.log("Zip archive is ready.") + progressDialog.setStatus("Download ready.") + downloadBlob(zipBlob, `procedure_${aid}.zip`) + btn.textContent = "done" + }) - progressDialog.setStatus("Generating zip archive in background... Firefox should stay responsive.") - }) - - console.log("Zip archive is ready.") - progressDialog.setStatus("Download ready.") - downloadBlob(zipBlob, `applications_${aid}.zip`) - btn.textContent = "done" }) .catch((error) => { console.error(error)