download_manager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. var _viewer_compatibility = require("./viewer_compatibility.js");
  29. ;
  30. function download(blobUrl, filename) {
  31. const a = document.createElement("a");
  32. if (!a.click) {
  33. throw new Error('DownloadManager: "a.click()" is not supported.');
  34. }
  35. a.href = blobUrl;
  36. a.target = "_parent";
  37. if ("download" in a) {
  38. a.download = filename;
  39. }
  40. (document.body || document.documentElement).appendChild(a);
  41. a.click();
  42. a.remove();
  43. }
  44. class DownloadManager {
  45. constructor() {
  46. this._openBlobUrls = new WeakMap();
  47. }
  48. downloadUrl(url, filename) {
  49. if (!(0, _pdf.createValidAbsoluteUrl)(url, "http://example.com")) {
  50. return;
  51. }
  52. download(url + "#pdfjs.action=download", filename);
  53. }
  54. downloadData(data, filename, contentType) {
  55. const blobUrl = (0, _pdf.createObjectURL)(data, contentType, _viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL);
  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 && !_viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL) {
  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, sourceEventType = "download") {
  84. if (_viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL) {
  85. this.downloadUrl(url, filename);
  86. return;
  87. }
  88. const blobUrl = URL.createObjectURL(blob);
  89. download(blobUrl, filename);
  90. }
  91. }
  92. exports.DownloadManager = DownloadManager;