download_manager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
  16. import { createValidAbsoluteUrl, isPdfFile } from "pdfjs-lib";
  17. if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("CHROME || GENERIC")) {
  18. throw new Error(
  19. 'Module "pdfjs-web/download_manager" shall not be used ' +
  20. "outside CHROME and GENERIC builds."
  21. );
  22. }
  23. function download(blobUrl, filename) {
  24. const a = document.createElement("a");
  25. if (!a.click) {
  26. throw new Error('DownloadManager: "a.click()" is not supported.');
  27. }
  28. a.href = blobUrl;
  29. a.target = "_parent";
  30. // Use a.download if available. This increases the likelihood that
  31. // the file is downloaded instead of opened by another PDF plugin.
  32. if ("download" in a) {
  33. a.download = filename;
  34. }
  35. // <a> must be in the document for recent Firefox versions,
  36. // otherwise .click() is ignored.
  37. (document.body || document.documentElement).append(a);
  38. a.click();
  39. a.remove();
  40. }
  41. /**
  42. * @implements {IDownloadManager}
  43. */
  44. class DownloadManager {
  45. #openBlobUrls = new WeakMap();
  46. downloadUrl(url, filename) {
  47. if (!createValidAbsoluteUrl(url, "http://example.com")) {
  48. console.error(`downloadUrl - not a valid URL: ${url}`);
  49. return; // restricted/invalid URL
  50. }
  51. download(url + "#pdfjs.action=download", filename);
  52. }
  53. downloadData(data, filename, contentType) {
  54. const blobUrl = URL.createObjectURL(
  55. new Blob([data], { type: contentType })
  56. );
  57. download(blobUrl, filename);
  58. }
  59. /**
  60. * @returns {boolean} Indicating if the data was opened.
  61. */
  62. openOrDownloadData(element, data, filename) {
  63. const isPdfData = isPdfFile(filename);
  64. const contentType = isPdfData ? "application/pdf" : "";
  65. if (isPdfData) {
  66. let blobUrl = this.#openBlobUrls.get(element);
  67. if (!blobUrl) {
  68. blobUrl = URL.createObjectURL(new Blob([data], { type: contentType }));
  69. this.#openBlobUrls.set(element, blobUrl);
  70. }
  71. let viewerUrl;
  72. if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
  73. // The current URL is the viewer, let's use it and append the file.
  74. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
  75. } else if (PDFJSDev.test("CHROME")) {
  76. // In the Chrome extension, the URL is rewritten using the history API
  77. // in viewer.js, so an absolute URL must be generated.
  78. viewerUrl =
  79. // eslint-disable-next-line no-undef
  80. chrome.runtime.getURL("/content/web/viewer.html") +
  81. "?file=" +
  82. encodeURIComponent(blobUrl + "#" + filename);
  83. }
  84. try {
  85. window.open(viewerUrl);
  86. return true;
  87. } catch (ex) {
  88. console.error(`openOrDownloadData: ${ex}`);
  89. // Release the `blobUrl`, since opening it failed, and fallback to
  90. // downloading the PDF file.
  91. URL.revokeObjectURL(blobUrl);
  92. this.#openBlobUrls.delete(element);
  93. }
  94. }
  95. this.downloadData(data, filename, contentType);
  96. return false;
  97. }
  98. download(blob, url, filename) {
  99. const blobUrl = URL.createObjectURL(blob);
  100. download(blobUrl, filename);
  101. }
  102. }
  103. export { DownloadManager };