1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Avoid downloading the document twice in PDFViewerApplication.download

The old implementation in `PDFViewerApplication.download` means that if the `getDocument`-call hasn't yet downloaded the *entire* PDF document it will be re-downloaded. This seems generally undesirable since:
 - In some (probably rare) cases a URL may not be valid an arbitrary number of times, which means that the download may fail.
 - It will lead to wasted resources, since we'll end up fetching the same PDF document *twice* in that case (once via the `getDocument`-call and once to allow the user to save it).

Hence this patch suggests that we change this very old code to instead always call the `PDFDocumentProxy.getData` method, since that'll trigger immediate downloading of the remaining document via the existing `getDocument`-call.

Finally, the patch removes the `PDFViewerApplication.downloadComplete` property since it's now unused.
This commit is contained in:
Jonas Jenwald 2024-07-20 09:41:44 +02:00
parent 006242489d
commit 64a4f0dc7e

View file

@ -151,7 +151,6 @@ const PDFViewerApplication = {
/** @type {AnnotationEditorParams} */
annotationEditorParams: null,
isInitialViewSet: false,
downloadComplete: false,
isViewerEmbedded: window.parent !== window,
url: "",
baseUrl: "",
@ -942,7 +941,6 @@ const PDFViewerApplication = {
this.pdfLinkService.externalLinkEnabled = true;
this.store = null;
this.isInitialViewSet = false;
this.downloadComplete = false;
this.url = "";
this.baseUrl = "";
this._downloadUrl = "";
@ -1072,12 +1070,9 @@ const PDFViewerApplication = {
async download(options = {}) {
let data;
try {
if (this.downloadComplete) {
data = await this.pdfDocument.getData();
}
data = await this.pdfDocument.getData();
} catch {
// When the PDF document isn't ready, or the PDF file is still
// downloading, simply download using the URL.
// When the PDF document isn't ready, simply download using the URL.
}
this.downloadManager.download(
data,
@ -1216,7 +1211,6 @@ const PDFViewerApplication = {
pdfDocument.getDownloadInfo().then(({ length }) => {
this._contentLength = length; // Ensure that the correct length is used.
this.downloadComplete = true;
this.loadingBar?.hide();
firstPagePromise.then(() => {