From c4b95d925fb741bbe4cd1fd9808ce40470bcce4d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 1 Jan 2021 23:03:18 +0100 Subject: [PATCH] Add a new helper method, on `PDFViewerApplication`, to determine the document filename Currently this code is duplicated no less than three times in the `web/app.js` file, and by introducing a helper method we can avoid unnecessary repetition. --- web/app.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/web/app.js b/web/app.js index ace56420e..2166e8f21 100644 --- a/web/app.js +++ b/web/app.js @@ -763,6 +763,12 @@ const PDFViewerApplication = { document.title = title; }, + get _docFilename() { + // Use `this.url` instead of `this.baseUrl` to perform filename detection + // based on the reference fragment as ultimate fallback if needed. + return this._contentDispositionFilename || getPDFFileNameFromURL(this.url); + }, + /** * @private */ @@ -984,12 +990,9 @@ const PDFViewerApplication = { downloadManager.downloadUrl(url, filename); } - const url = this.baseUrl; - // Use this.url instead of this.baseUrl to perform filename detection based - // on the reference fragment as ultimate fallback if needed. - const filename = - this._contentDispositionFilename || getPDFFileNameFromURL(this.url); - const downloadManager = this.downloadManager; + const downloadManager = this.downloadManager, + url = this.baseUrl, + filename = this._docFilename; downloadManager.onerror = err => { // This error won't really be helpful because it's likely the // fallback won't work either (or is already open). @@ -1017,12 +1020,9 @@ const PDFViewerApplication = { return; } - const url = this.baseUrl; - // Use this.url instead of this.baseUrl to perform filename detection based - // on the reference fragment as ultimate fallback if needed. - const filename = - this._contentDispositionFilename || getPDFFileNameFromURL(this.url); - const downloadManager = this.downloadManager; + const downloadManager = this.downloadManager, + url = this.baseUrl, + filename = this._docFilename; downloadManager.onerror = err => { // This error won't really be helpful because it's likely the // fallback won't work either (or is already open). @@ -1587,8 +1587,6 @@ const PDFViewerApplication = { } this._contentLength = length; } - const filename = - this._contentDispositionFilename || getPDFFileNameFromURL(this.url); try { await scripting.createSandbox({ @@ -1602,7 +1600,7 @@ const PDFViewerApplication = { ...this.documentInfo, baseURL: this.baseUrl, filesize: this._contentLength, - filename, + filename: this._docFilename, metadata: this.metadata, numPages: pdfDocument.numPages, URL: this.url,