From 64a4f0dc7e60e86a7c1da1dc903497fff71abe2c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 20 Jul 2024 09:41:44 +0200 Subject: [PATCH] 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. --- web/app.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/web/app.js b/web/app.js index 83393b9b2..7a06a9c5c 100644 --- a/web/app.js +++ b/web/app.js @@ -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(() => {