2
0

download_manager.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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).appendChild(a);
  40. a.click();
  41. a.remove();
  42. }
  43. class DownloadManager {
  44. constructor() {
  45. this._openBlobUrls = new WeakMap();
  46. }
  47. downloadUrl(url, filename) {
  48. if (!(0, _pdf.createValidAbsoluteUrl)(url, "http://example.com")) {
  49. console.error(`downloadUrl - not a valid URL: ${url}`);
  50. return;
  51. }
  52. download(url + "#pdfjs.action=download", filename);
  53. }
  54. downloadData(data, filename, contentType) {
  55. const blobUrl = URL.createObjectURL(new Blob([data], {
  56. type: contentType
  57. }));
  58. download(blobUrl, filename);
  59. }
  60. openOrDownloadData(element, data, filename) {
  61. const isPdfData = (0, _pdf.isPdfFile)(filename);
  62. const contentType = isPdfData ? "application/pdf" : "";
  63. if (isPdfData) {
  64. let blobUrl = this._openBlobUrls.get(element);
  65. if (!blobUrl) {
  66. blobUrl = URL.createObjectURL(new Blob([data], {
  67. type: contentType
  68. }));
  69. this._openBlobUrls.set(element, blobUrl);
  70. }
  71. let viewerUrl;
  72. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
  73. try {
  74. window.open(viewerUrl);
  75. return true;
  76. } catch (ex) {
  77. console.error(`openOrDownloadData: ${ex}`);
  78. URL.revokeObjectURL(blobUrl);
  79. this._openBlobUrls.delete(element);
  80. }
  81. }
  82. this.downloadData(data, filename, contentType);
  83. return false;
  84. }
  85. download(blob, url, filename, sourceEventType = "download") {
  86. const blobUrl = URL.createObjectURL(blob);
  87. download(blobUrl, filename);
  88. }
  89. }
  90. exports.DownloadManager = DownloadManager;