Merge pull request #11845 from Snuffleupagus/less-createObjectURL

Use the native `URL.createObjectURL` method more in the `web/` folder
This commit is contained in:
Tim van der Meij 2020-04-24 23:12:37 +02:00 committed by GitHub
commit 7363308b97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 15 deletions

View File

@ -14,8 +14,8 @@
*/
import "../extensions/firefox/tools/l10n.js";
import { createObjectURL, PDFDataRangeTransport, shadow } from "pdfjs-lib";
import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
import { PDFDataRangeTransport, shadow } from "pdfjs-lib";
import { BasePreferences } from "./preferences.js";
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
@ -100,14 +100,23 @@ class DownloadManager {
}
downloadData(data, filename, contentType) {
const blobUrl = createObjectURL(data, contentType);
const blobUrl = URL.createObjectURL(
new Blob([data], { type: contentType })
);
const onResponse = err => {
URL.revokeObjectURL(blobUrl);
};
FirefoxCom.request("download", {
blobUrl,
originalUrl: blobUrl,
filename,
isAttachment: true,
});
FirefoxCom.request(
"download",
{
blobUrl,
originalUrl: blobUrl,
filename,
isAttachment: true,
},
onResponse
);
}
download(blob, url, filename) {

View File

@ -14,7 +14,6 @@
*/
import {
createObjectURL,
createPromiseCapability,
getFilenameFromUrl,
removeNullCharacters,
@ -84,14 +83,19 @@ class PDFAttachmentViewer {
);
}
let blobUrl;
button.onclick = function () {
button.onclick = () => {
if (!blobUrl) {
blobUrl = createObjectURL(content, "application/pdf");
blobUrl = URL.createObjectURL(
new Blob([content], { type: "application/pdf" })
);
}
let viewerUrl;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// The current URL is the viewer, let's use it and append the file.
viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
} else if (PDFJSDev.test("MOZCENTRAL")) {
// Let Firefox's content handler catch the URL and display the PDF.
viewerUrl = blobUrl + "?" + encodeURIComponent(filename);
} else if (PDFJSDev.test("CHROME")) {
// In the Chrome extension, the URL is rewritten using the history API
// in viewer.js, so an absolute URL must be generated.
@ -100,11 +104,17 @@ class PDFAttachmentViewer {
chrome.runtime.getURL("/content/web/viewer.html") +
"?file=" +
encodeURIComponent(blobUrl + "#" + filename);
} else if (PDFJSDev.test("MOZCENTRAL")) {
// Let Firefox's content handler catch the URL and display the PDF.
viewerUrl = blobUrl + "?" + encodeURIComponent(filename);
}
window.open(viewerUrl);
try {
window.open(viewerUrl);
} catch (ex) {
console.error(`_bindPdfLink: ${ex}`);
// Release the `blobUrl`, since opening it failed...
URL.revokeObjectURL(blobUrl);
blobUrl = null;
// ... and fallback to downloading the PDF file.
this.downloadManager.downloadData(content, filename, "application/pdf");
}
return false;
};
}