download_manager.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. const DISABLE_CREATE_OBJECT_URL = _viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL || false;
  31. function download(blobUrl, filename) {
  32. const a = document.createElement("a");
  33. if (!a.click) {
  34. throw new Error('DownloadManager: "a.click()" is not supported.');
  35. }
  36. a.href = blobUrl;
  37. a.target = "_parent";
  38. if ("download" in a) {
  39. a.download = filename;
  40. }
  41. (document.body || document.documentElement).appendChild(a);
  42. a.click();
  43. a.remove();
  44. }
  45. class DownloadManager {
  46. constructor({
  47. disableCreateObjectURL = DISABLE_CREATE_OBJECT_URL
  48. }) {
  49. this.disableCreateObjectURL = disableCreateObjectURL;
  50. }
  51. downloadUrl(url, filename) {
  52. if (!(0, _pdf.createValidAbsoluteUrl)(url, "http://example.com")) {
  53. return;
  54. }
  55. download(url + "#pdfjs.action=download", filename);
  56. }
  57. downloadData(data, filename, contentType) {
  58. if (navigator.msSaveBlob) {
  59. navigator.msSaveBlob(new Blob([data], {
  60. type: contentType
  61. }), filename);
  62. return;
  63. }
  64. const blobUrl = (0, _pdf.createObjectURL)(data, contentType, this.disableCreateObjectURL);
  65. download(blobUrl, filename);
  66. }
  67. download(blob, url, filename) {
  68. if (navigator.msSaveBlob) {
  69. if (!navigator.msSaveBlob(blob, filename)) {
  70. this.downloadUrl(url, filename);
  71. }
  72. return;
  73. }
  74. if (this.disableCreateObjectURL) {
  75. this.downloadUrl(url, filename);
  76. return;
  77. }
  78. const blobUrl = URL.createObjectURL(blob);
  79. download(blobUrl, filename);
  80. }
  81. }
  82. exports.DownloadManager = DownloadManager;