mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 23:28:06 +02:00
Move the disableCreateObjectURL
option from the global PDFJS
object and into getDocument
instead
This commit is contained in:
parent
05c05bdef5
commit
1d03ad0060
11 changed files with 54 additions and 36 deletions
|
@ -55,7 +55,7 @@ const DefaultExternalServices = {
|
|||
initPassiveLoading(callbacks) {},
|
||||
fallback(data, callback) {},
|
||||
reportTelemetry(data) {},
|
||||
createDownloadManager() {
|
||||
createDownloadManager(options) {
|
||||
throw new Error('Not implemented: createDownloadManager');
|
||||
},
|
||||
createPreferences() {
|
||||
|
@ -357,7 +357,9 @@ let PDFViewerApplication = {
|
|||
});
|
||||
this.pdfLinkService = pdfLinkService;
|
||||
|
||||
let downloadManager = this.externalServices.createDownloadManager();
|
||||
let downloadManager = this.externalServices.createDownloadManager({
|
||||
disableCreateObjectURL: AppOptions.get('disableCreateObjectURL'),
|
||||
});
|
||||
this.downloadManager = downloadManager;
|
||||
|
||||
let container = appConfig.mainContainer;
|
||||
|
@ -1873,7 +1875,7 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
|
|||
webViewerFileInputChange = function webViewerFileInputChange(evt) {
|
||||
let file = evt.fileInput.files[0];
|
||||
|
||||
if (!PDFJS.disableCreateObjectURL && URL.createObjectURL) {
|
||||
if (URL.createObjectURL && !AppOptions.get('disableCreateObjectURL')) {
|
||||
PDFViewerApplication.open(URL.createObjectURL(file));
|
||||
} else {
|
||||
// Read the local file into a Uint8Array.
|
||||
|
|
|
@ -145,6 +145,11 @@ const defaultOptions = {
|
|||
value: false,
|
||||
kind: OptionKind.API,
|
||||
},
|
||||
disableCreateObjectURL: {
|
||||
/** @type {boolean} */
|
||||
value: apiCompatibilityParams.disableCreateObjectURL || false,
|
||||
kind: OptionKind.API,
|
||||
},
|
||||
disableFontFace: {
|
||||
/** @type {boolean} */
|
||||
value: false,
|
||||
|
|
|
@ -363,8 +363,8 @@ ChromeExternalServices.initPassiveLoading = function(callbacks) {
|
|||
callbacks.onOpenWithURL(url, length, originalURL);
|
||||
});
|
||||
};
|
||||
ChromeExternalServices.createDownloadManager = function() {
|
||||
return new DownloadManager();
|
||||
ChromeExternalServices.createDownloadManager = function(options) {
|
||||
return new DownloadManager(options);
|
||||
};
|
||||
ChromeExternalServices.createPreferences = function() {
|
||||
return new ChromePreferences();
|
||||
|
|
|
@ -13,13 +13,18 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { createObjectURL, createValidAbsoluteUrl, PDFJS } from 'pdfjs-lib';
|
||||
import {
|
||||
apiCompatibilityParams, createObjectURL, createValidAbsoluteUrl
|
||||
} from 'pdfjs-lib';
|
||||
|
||||
if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('CHROME || GENERIC')) {
|
||||
throw new Error('Module "pdfjs-web/download_manager" shall not be used ' +
|
||||
'outside CHROME and GENERIC builds.');
|
||||
}
|
||||
|
||||
const DISABLE_CREATE_OBJECT_URL =
|
||||
apiCompatibilityParams.disableCreateObjectURL || false;
|
||||
|
||||
function download(blobUrl, filename) {
|
||||
let a = document.createElement('a');
|
||||
if (!a.click) {
|
||||
|
@ -40,6 +45,10 @@ function download(blobUrl, filename) {
|
|||
}
|
||||
|
||||
class DownloadManager {
|
||||
constructor({ disableCreateObjectURL = DISABLE_CREATE_OBJECT_URL, }) {
|
||||
this.disableCreateObjectURL = disableCreateObjectURL;
|
||||
}
|
||||
|
||||
downloadUrl(url, filename) {
|
||||
if (!createValidAbsoluteUrl(url, 'http://example.com')) {
|
||||
return; // restricted/invalid URL
|
||||
|
@ -53,7 +62,7 @@ class DownloadManager {
|
|||
filename);
|
||||
}
|
||||
let blobUrl = createObjectURL(data, contentType,
|
||||
PDFJS.disableCreateObjectURL);
|
||||
this.disableCreateObjectURL);
|
||||
download(blobUrl, filename);
|
||||
}
|
||||
|
||||
|
@ -66,7 +75,7 @@ class DownloadManager {
|
|||
return;
|
||||
}
|
||||
|
||||
if (PDFJS.disableCreateObjectURL) {
|
||||
if (this.disableCreateObjectURL) {
|
||||
// URL.createObjectURL is not supported
|
||||
this.downloadUrl(url, filename);
|
||||
return;
|
||||
|
|
|
@ -84,6 +84,10 @@ let FirefoxCom = (function FirefoxComClosure() {
|
|||
})();
|
||||
|
||||
class DownloadManager {
|
||||
constructor(options) {
|
||||
this.disableCreateObjectURL = false;
|
||||
}
|
||||
|
||||
downloadUrl(url, filename) {
|
||||
FirefoxCom.request('download', {
|
||||
originalUrl: url,
|
||||
|
@ -92,7 +96,7 @@ class DownloadManager {
|
|||
}
|
||||
|
||||
downloadData(data, filename, contentType) {
|
||||
let blobUrl = createObjectURL(data, contentType, false);
|
||||
let blobUrl = createObjectURL(data, contentType);
|
||||
|
||||
FirefoxCom.request('download', {
|
||||
blobUrl,
|
||||
|
@ -256,8 +260,8 @@ PDFViewerApplication.externalServices = {
|
|||
FirefoxCom.request('reportTelemetry', JSON.stringify(data));
|
||||
},
|
||||
|
||||
createDownloadManager() {
|
||||
return new DownloadManager();
|
||||
createDownloadManager(options) {
|
||||
return new DownloadManager(options);
|
||||
},
|
||||
|
||||
createPreferences() {
|
||||
|
|
|
@ -42,8 +42,8 @@ class GenericPreferences extends BasePreferences {
|
|||
}
|
||||
|
||||
let GenericExternalServices = Object.create(DefaultExternalServices);
|
||||
GenericExternalServices.createDownloadManager = function() {
|
||||
return new DownloadManager();
|
||||
GenericExternalServices.createDownloadManager = function(options) {
|
||||
return new DownloadManager(options);
|
||||
};
|
||||
GenericExternalServices.createPreferences = function() {
|
||||
return new GenericPreferences();
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
createObjectURL, createPromiseCapability, getFilenameFromUrl, PDFJS,
|
||||
createObjectURL, createPromiseCapability, getFilenameFromUrl,
|
||||
removeNullCharacters
|
||||
} from 'pdfjs-lib';
|
||||
|
||||
|
@ -74,9 +74,9 @@ class PDFAttachmentViewer {
|
|||
* @private
|
||||
*/
|
||||
_bindPdfLink(button, content, filename) {
|
||||
if (PDFJS.disableCreateObjectURL) {
|
||||
throw new Error('bindPdfLink: ' +
|
||||
'Unsupported "PDFJS.disableCreateObjectURL" value.');
|
||||
if (this.downloadManager.disableCreateObjectURL) {
|
||||
throw new Error(
|
||||
'bindPdfLink: Unsupported "disableCreateObjectURL" value.');
|
||||
}
|
||||
let blobUrl;
|
||||
button.onclick = function() {
|
||||
|
@ -141,7 +141,8 @@ class PDFAttachmentViewer {
|
|||
div.className = 'attachmentsItem';
|
||||
let button = document.createElement('button');
|
||||
button.textContent = filename;
|
||||
if (/\.pdf$/i.test(filename) && !PDFJS.disableCreateObjectURL) {
|
||||
if (/\.pdf$/i.test(filename) &&
|
||||
!this.downloadManager.disableCreateObjectURL) {
|
||||
this._bindPdfLink(button, item.content, filename);
|
||||
} else {
|
||||
this._bindLink(button, item.content, filename);
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
|
||||
import { CSS_UNITS, NullL10n } from './ui_utils';
|
||||
import { PDFPrintServiceFactory, PDFViewerApplication } from './app';
|
||||
import { PDFJS } from 'pdfjs-lib';
|
||||
|
||||
let activeService = null;
|
||||
let overlayManager = null;
|
||||
|
@ -62,6 +61,8 @@ function PDFPrintService(pdfDocument, pagesOverview, printContainer, l10n) {
|
|||
this.pagesOverview = pagesOverview;
|
||||
this.printContainer = printContainer;
|
||||
this.l10n = l10n || NullL10n;
|
||||
this.disableCreateObjectURL =
|
||||
pdfDocument.loadingParams['disableCreateObjectURL'];
|
||||
this.currentPage = -1;
|
||||
// The temporary canvas where renderPage paints one page at a time.
|
||||
this.scratchCanvas = document.createElement('canvas');
|
||||
|
@ -153,7 +154,7 @@ PDFPrintService.prototype = {
|
|||
img.style.height = printItem.height;
|
||||
|
||||
let scratchCanvas = this.scratchCanvas;
|
||||
if (('toBlob' in scratchCanvas) && !PDFJS.disableCreateObjectURL) {
|
||||
if (('toBlob' in scratchCanvas) && !this.disableCreateObjectURL) {
|
||||
scratchCanvas.toBlob(function(blob) {
|
||||
img.src = URL.createObjectURL(blob);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue