2
0

download_manager.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.DownloadManager = undefined;
  20. var _pdfjs = require('pdfjs-web/pdfjs');
  21. var _app = require('pdfjs-web/app');
  22. ;
  23. function download(blobUrl, filename) {
  24. var a = document.createElement('a');
  25. if (a.click) {
  26. a.href = blobUrl;
  27. a.target = '_parent';
  28. if ('download' in a) {
  29. a.download = filename;
  30. }
  31. (document.body || document.documentElement).appendChild(a);
  32. a.click();
  33. a.parentNode.removeChild(a);
  34. } else {
  35. if (window.top === window && blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
  36. var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
  37. blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
  38. }
  39. window.open(blobUrl, '_parent');
  40. }
  41. }
  42. function DownloadManager() {}
  43. DownloadManager.prototype = {
  44. downloadUrl: function DownloadManager_downloadUrl(url, filename) {
  45. if (!(0, _pdfjs.createValidAbsoluteUrl)(url, 'http://example.com')) {
  46. return;
  47. }
  48. download(url + '#pdfjs.action=download', filename);
  49. },
  50. downloadData: function DownloadManager_downloadData(data, filename, contentType) {
  51. if (navigator.msSaveBlob) {
  52. return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
  53. }
  54. var blobUrl = (0, _pdfjs.createObjectURL)(data, contentType, _pdfjs.PDFJS.disableCreateObjectURL);
  55. download(blobUrl, filename);
  56. },
  57. download: function DownloadManager_download(blob, url, filename) {
  58. if (navigator.msSaveBlob) {
  59. if (!navigator.msSaveBlob(blob, filename)) {
  60. this.downloadUrl(url, filename);
  61. }
  62. return;
  63. }
  64. if (_pdfjs.PDFJS.disableCreateObjectURL) {
  65. this.downloadUrl(url, filename);
  66. return;
  67. }
  68. var blobUrl = URL.createObjectURL(blob);
  69. download(blobUrl, filename);
  70. }
  71. };
  72. var GenericExternalServices = Object.create(_app.DefaultExternalServices);
  73. GenericExternalServices.createDownloadManager = function () {
  74. return new DownloadManager();
  75. };
  76. _app.PDFViewerApplication.externalServices = GenericExternalServices;
  77. exports.DownloadManager = DownloadManager;