download_manager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 _app_options = require("./app_options.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. console.error(`downloadUrl - not a valid URL: ${url}`);
  51. return;
  52. }
  53. download(url + "#pdfjs.action=download", filename);
  54. }
  55. downloadData(data, filename, contentType) {
  56. const blobUrl = (0, _pdf.createObjectURL)(data, contentType, _app_options.compatibilityParams.disableCreateObjectURL);
  57. download(blobUrl, filename);
  58. }
  59. openOrDownloadData(element, data, filename) {
  60. const isPdfData = (0, _pdf.isPdfFile)(filename);
  61. const contentType = isPdfData ? "application/pdf" : "";
  62. if (isPdfData && !_app_options.compatibilityParams.disableCreateObjectURL) {
  63. let blobUrl = this._openBlobUrls.get(element);
  64. if (!blobUrl) {
  65. blobUrl = URL.createObjectURL(new Blob([data], {
  66. type: contentType
  67. }));
  68. this._openBlobUrls.set(element, blobUrl);
  69. }
  70. let viewerUrl;
  71. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
  72. try {
  73. window.open(viewerUrl);
  74. return true;
  75. } catch (ex) {
  76. console.error(`openOrDownloadData: ${ex}`);
  77. URL.revokeObjectURL(blobUrl);
  78. this._openBlobUrls.delete(element);
  79. }
  80. }
  81. this.downloadData(data, filename, contentType);
  82. return false;
  83. }
  84. download(blob, url, filename, sourceEventType = "download") {
  85. if (_app_options.compatibilityParams.disableCreateObjectURL) {
  86. this.downloadUrl(url, filename);
  87. return;
  88. }
  89. const blobUrl = URL.createObjectURL(blob);
  90. download(blobUrl, filename);
  91. }
  92. }
  93. exports.DownloadManager = DownloadManager;