|
| 1 | +/* Copyright 2013 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { isPdfFile } from "pdfjs-lib"; |
| 17 | + |
| 18 | +class BaseDownloadManager { |
| 19 | + #openBlobUrls = new WeakMap(); |
| 20 | + |
| 21 | + constructor() { |
| 22 | + if ( |
| 23 | + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && |
| 24 | + this.constructor === BaseDownloadManager |
| 25 | + ) { |
| 26 | + throw new Error("Cannot initialize BaseDownloadManager."); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + _triggerDownload(blobUrl, originalUrl, filename, isAttachment = false) { |
| 31 | + throw new Error("Not implemented: _triggerDownload"); |
| 32 | + } |
| 33 | + |
| 34 | + _getOpenDataUrl(blobUrl, filename, dest = null) { |
| 35 | + throw new Error("Not implemented: _getOpenDataUrl"); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param {Uint8Array} data |
| 40 | + * @param {string} filename |
| 41 | + * @param {string} [contentType] |
| 42 | + */ |
| 43 | + downloadData(data, filename, contentType) { |
| 44 | + const blobUrl = URL.createObjectURL( |
| 45 | + new Blob([data], { type: contentType }) |
| 46 | + ); |
| 47 | + |
| 48 | + this._triggerDownload( |
| 49 | + blobUrl, |
| 50 | + /* originalUrl = */ blobUrl, |
| 51 | + filename, |
| 52 | + /* isAttachment = */ true |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param {Uint8Array} data |
| 58 | + * @param {string} filename |
| 59 | + * @param {string | null} [dest] |
| 60 | + * @returns {boolean} Indicating if the data was opened. |
| 61 | + */ |
| 62 | + openOrDownloadData(data, filename, dest = null) { |
| 63 | + const isPdfData = isPdfFile(filename); |
| 64 | + const contentType = isPdfData ? "application/pdf" : ""; |
| 65 | + |
| 66 | + if (isPdfData) { |
| 67 | + let blobUrl; |
| 68 | + try { |
| 69 | + blobUrl = this.#openBlobUrls.getOrInsertComputed(data, () => |
| 70 | + URL.createObjectURL(new Blob([data], { type: contentType })) |
| 71 | + ); |
| 72 | + const viewerUrl = this._getOpenDataUrl(blobUrl, filename, dest); |
| 73 | + |
| 74 | + window.open(viewerUrl); |
| 75 | + return true; |
| 76 | + } catch (ex) { |
| 77 | + console.error("openOrDownloadData:", ex); |
| 78 | + // Release the `blobUrl`, since opening it failed, and fallback to |
| 79 | + // downloading the PDF file. |
| 80 | + URL.revokeObjectURL(blobUrl); |
| 81 | + this.#openBlobUrls.delete(data); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + this.downloadData(data, filename, contentType); |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param {Uint8Array} data |
| 91 | + * @param {string} url |
| 92 | + * @param {string} filename |
| 93 | + */ |
| 94 | + download(data, url, filename) { |
| 95 | + const blobUrl = data |
| 96 | + ? URL.createObjectURL(new Blob([data], { type: "application/pdf" })) |
| 97 | + : null; |
| 98 | + |
| 99 | + this._triggerDownload(blobUrl, /* originalUrl = */ url, filename); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export { BaseDownloadManager }; |
0 commit comments