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.
This commit is contained in:
Jonas Jenwald 2021-01-01 23:03:18 +01:00
parent c3e02b3471
commit c4b95d925f

View File

@ -763,6 +763,12 @@ const PDFViewerApplication = {
document.title = title; 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 * @private
*/ */
@ -984,12 +990,9 @@ const PDFViewerApplication = {
downloadManager.downloadUrl(url, filename); downloadManager.downloadUrl(url, filename);
} }
const url = this.baseUrl; const downloadManager = this.downloadManager,
// Use this.url instead of this.baseUrl to perform filename detection based url = this.baseUrl,
// on the reference fragment as ultimate fallback if needed. filename = this._docFilename;
const filename =
this._contentDispositionFilename || getPDFFileNameFromURL(this.url);
const downloadManager = this.downloadManager;
downloadManager.onerror = err => { downloadManager.onerror = err => {
// This error won't really be helpful because it's likely the // This error won't really be helpful because it's likely the
// fallback won't work either (or is already open). // fallback won't work either (or is already open).
@ -1017,12 +1020,9 @@ const PDFViewerApplication = {
return; return;
} }
const url = this.baseUrl; const downloadManager = this.downloadManager,
// Use this.url instead of this.baseUrl to perform filename detection based url = this.baseUrl,
// on the reference fragment as ultimate fallback if needed. filename = this._docFilename;
const filename =
this._contentDispositionFilename || getPDFFileNameFromURL(this.url);
const downloadManager = this.downloadManager;
downloadManager.onerror = err => { downloadManager.onerror = err => {
// This error won't really be helpful because it's likely the // This error won't really be helpful because it's likely the
// fallback won't work either (or is already open). // fallback won't work either (or is already open).
@ -1587,8 +1587,6 @@ const PDFViewerApplication = {
} }
this._contentLength = length; this._contentLength = length;
} }
const filename =
this._contentDispositionFilename || getPDFFileNameFromURL(this.url);
try { try {
await scripting.createSandbox({ await scripting.createSandbox({
@ -1602,7 +1600,7 @@ const PDFViewerApplication = {
...this.documentInfo, ...this.documentInfo,
baseURL: this.baseUrl, baseURL: this.baseUrl,
filesize: this._contentLength, filesize: this._contentLength,
filename, filename: this._docFilename,
metadata: this.metadata, metadata: this.metadata,
numPages: pdfDocument.numPages, numPages: pdfDocument.numPages,
URL: this.url, URL: this.url,