Merge pull request #18236 from Snuffleupagus/rm-downloadUrl

Remove the `DownloadManager.downloadUrl` method
This commit is contained in:
Tim van der Meij 2024-06-13 15:44:42 +02:00 committed by GitHub
commit 47791a4c80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 37 deletions

View file

@ -1084,18 +1084,21 @@ const PDFViewerApplication = {
}, },
async download(options = {}) { async download(options = {}) {
const url = this._downloadUrl, let data;
filename = this._docFilename;
try { try {
this._ensureDownloadComplete(); this._ensureDownloadComplete();
const data = await this.pdfDocument.getData(); data = await this.pdfDocument.getData();
this.downloadManager.download(data, url, filename, options);
} catch { } catch {
// When the PDF document isn't ready, or the PDF file is still // When the PDF document isn't ready, or the PDF file is still
// downloading, simply download using the URL. // downloading, simply download using the URL.
this.downloadManager.downloadUrl(url, filename, options);
} }
this.downloadManager.download(
data,
this._downloadUrl,
this._docFilename,
options
);
}, },
async save(options = {}) { async save(options = {}) {
@ -1105,13 +1108,16 @@ const PDFViewerApplication = {
this._saveInProgress = true; this._saveInProgress = true;
await this.pdfScriptingManager.dispatchWillSave(); await this.pdfScriptingManager.dispatchWillSave();
const url = this._downloadUrl,
filename = this._docFilename;
try { try {
this._ensureDownloadComplete(); this._ensureDownloadComplete();
const data = await this.pdfDocument.saveDocument(); const data = await this.pdfDocument.saveDocument();
this.downloadManager.download(data, url, filename, options); this.downloadManager.download(
data,
this._downloadUrl,
this._docFilename,
options
);
} catch (reason) { } catch (reason) {
// When the PDF document isn't ready, or the PDF file is still // When the PDF document isn't ready, or the PDF file is still
// downloading, simply fallback to a "regular" download. // downloading, simply fallback to a "regular" download.

View file

@ -49,14 +49,6 @@ function download(blobUrl, filename) {
class DownloadManager { class DownloadManager {
#openBlobUrls = new WeakMap(); #openBlobUrls = new WeakMap();
downloadUrl(url, filename, _options) {
if (!createValidAbsoluteUrl(url, "http://example.com")) {
console.error(`downloadUrl - not a valid URL: ${url}`);
return; // restricted/invalid URL
}
download(url + "#pdfjs.action=download", filename);
}
downloadData(data, filename, contentType) { downloadData(data, filename, contentType) {
const blobUrl = URL.createObjectURL( const blobUrl = URL.createObjectURL(
new Blob([data], { type: contentType }) new Blob([data], { type: contentType })
@ -114,9 +106,18 @@ class DownloadManager {
} }
download(data, url, filename, _options) { download(data, url, filename, _options) {
const blobUrl = URL.createObjectURL( let blobUrl;
new Blob([data], { type: "application/pdf" }) if (data) {
); blobUrl = URL.createObjectURL(
new Blob([data], { type: "application/pdf" })
);
} else {
if (!createValidAbsoluteUrl(url, "http://example.com")) {
console.error(`download - not a valid URL: ${url}`);
return;
}
blobUrl = url + "#pdfjs.action=download";
}
download(blobUrl, filename); download(blobUrl, filename);
} }
} }

View file

@ -82,14 +82,6 @@ class FirefoxCom {
class DownloadManager { class DownloadManager {
#openBlobUrls = new WeakMap(); #openBlobUrls = new WeakMap();
downloadUrl(url, filename, options = {}) {
FirefoxCom.request("download", {
originalUrl: url,
filename,
options,
});
}
downloadData(data, filename, contentType) { downloadData(data, filename, contentType) {
const blobUrl = URL.createObjectURL( const blobUrl = URL.createObjectURL(
new Blob([data], { type: contentType }) new Blob([data], { type: contentType })
@ -141,9 +133,9 @@ class DownloadManager {
} }
download(data, url, filename, options = {}) { download(data, url, filename, options = {}) {
const blobUrl = URL.createObjectURL( const blobUrl = data
new Blob([data], { type: "application/pdf" }) ? URL.createObjectURL(new Blob([data], { type: "application/pdf" }))
); : null;
FirefoxCom.request("download", { FirefoxCom.request("download", {
blobUrl, blobUrl,

View file

@ -137,13 +137,6 @@ class IRenderableView {
* @interface * @interface
*/ */
class IDownloadManager { class IDownloadManager {
/**
* @param {string} url
* @param {string} filename
* @param {Object} [options]
*/
downloadUrl(url, filename, options) {}
/** /**
* @param {Uint8Array} data * @param {Uint8Array} data
* @param {string} filename * @param {string} filename