download_manager.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DownloadManager = void 0;
  27. var _pdf = require("../pdf");
  28. ;
  29. function download(blobUrl, filename) {
  30. const a = document.createElement("a");
  31. if (!a.click) {
  32. throw new Error('DownloadManager: "a.click()" is not supported.');
  33. }
  34. a.href = blobUrl;
  35. a.target = "_parent";
  36. if ("download" in a) {
  37. a.download = filename;
  38. }
  39. (document.body || document.documentElement).append(a);
  40. a.click();
  41. a.remove();
  42. }
  43. class DownloadManager {
  44. #openBlobUrls = new WeakMap();
  45. downloadUrl(url, filename) {
  46. if (!(0, _pdf.createValidAbsoluteUrl)(url, "http://example.com")) {
  47. console.error(`downloadUrl - not a valid URL: ${url}`);
  48. return;
  49. }
  50. download(url + "#pdfjs.action=download", filename);
  51. }
  52. downloadData(data, filename, contentType) {
  53. const blobUrl = URL.createObjectURL(new Blob([data], {
  54. type: contentType
  55. }));
  56. download(blobUrl, filename);
  57. }
  58. openOrDownloadData(element, data, filename) {
  59. const isPdfData = (0, _pdf.isPdfFile)(filename);
  60. const contentType = isPdfData ? "application/pdf" : "";
  61. if (isPdfData) {
  62. let blobUrl = this.#openBlobUrls.get(element);
  63. if (!blobUrl) {
  64. blobUrl = URL.createObjectURL(new Blob([data], {
  65. type: contentType
  66. }));
  67. this.#openBlobUrls.set(element, blobUrl);
  68. }
  69. let viewerUrl;
  70. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
  71. try {
  72. window.open(viewerUrl);
  73. return true;
  74. } catch (ex) {
  75. console.error(`openOrDownloadData: ${ex}`);
  76. URL.revokeObjectURL(blobUrl);
  77. this.#openBlobUrls.delete(element);
  78. }
  79. }
  80. this.downloadData(data, filename, contentType);
  81. return false;
  82. }
  83. download(blob, url, filename) {
  84. const blobUrl = URL.createObjectURL(blob);
  85. download(blobUrl, filename);
  86. }
  87. }
  88. exports.DownloadManager = DownloadManager;