pdf_document_properties.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.PDFDocumentProperties = undefined;
  20. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _ui_utils = require('./ui_utils');
  22. var _pdfjs = require('./pdfjs');
  23. var _overlay_manager = require('./overlay_manager');
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. var PDFDocumentProperties = function () {
  26. function PDFDocumentProperties(options) {
  27. _classCallCheck(this, PDFDocumentProperties);
  28. this.overlayName = options.overlayName;
  29. this.fields = options.fields;
  30. this.container = options.container;
  31. this.rawFileSize = 0;
  32. this.url = null;
  33. this.pdfDocument = null;
  34. if (options.closeButton) {
  35. options.closeButton.addEventListener('click', this.close.bind(this));
  36. }
  37. this._dataAvailableCapability = (0, _pdfjs.createPromiseCapability)();
  38. _overlay_manager.OverlayManager.register(this.overlayName, this.container, this.close.bind(this));
  39. }
  40. _createClass(PDFDocumentProperties, [{
  41. key: 'open',
  42. value: function open() {
  43. var _this = this;
  44. Promise.all([_overlay_manager.OverlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(function () {
  45. _this._getProperties();
  46. });
  47. }
  48. }, {
  49. key: 'close',
  50. value: function close() {
  51. _overlay_manager.OverlayManager.close(this.overlayName);
  52. }
  53. }, {
  54. key: 'setFileSize',
  55. value: function setFileSize(fileSize) {
  56. if (fileSize > 0) {
  57. this.rawFileSize = fileSize;
  58. }
  59. }
  60. }, {
  61. key: 'setDocumentAndUrl',
  62. value: function setDocumentAndUrl(pdfDocument, url) {
  63. this.pdfDocument = pdfDocument;
  64. this.url = url;
  65. this._dataAvailableCapability.resolve();
  66. }
  67. }, {
  68. key: '_getProperties',
  69. value: function _getProperties() {
  70. var _this2 = this;
  71. if (!_overlay_manager.OverlayManager.active) {
  72. return;
  73. }
  74. this.pdfDocument.getDownloadInfo().then(function (data) {
  75. if (data.length === _this2.rawFileSize) {
  76. return;
  77. }
  78. _this2.setFileSize(data.length);
  79. _this2._updateUI(_this2.fields['fileSize'], _this2._parseFileSize());
  80. });
  81. this.pdfDocument.getMetadata().then(function (data) {
  82. var content = {
  83. 'fileName': (0, _ui_utils.getPDFFileNameFromURL)(_this2.url),
  84. 'fileSize': _this2._parseFileSize(),
  85. 'title': data.info.Title,
  86. 'author': data.info.Author,
  87. 'subject': data.info.Subject,
  88. 'keywords': data.info.Keywords,
  89. 'creationDate': _this2._parseDate(data.info.CreationDate),
  90. 'modificationDate': _this2._parseDate(data.info.ModDate),
  91. 'creator': data.info.Creator,
  92. 'producer': data.info.Producer,
  93. 'version': data.info.PDFFormatVersion,
  94. 'pageCount': _this2.pdfDocument.numPages
  95. };
  96. for (var identifier in content) {
  97. _this2._updateUI(_this2.fields[identifier], content[identifier]);
  98. }
  99. });
  100. }
  101. }, {
  102. key: '_updateUI',
  103. value: function _updateUI(field, content) {
  104. if (field && content !== undefined && content !== '') {
  105. field.textContent = content;
  106. }
  107. }
  108. }, {
  109. key: '_parseFileSize',
  110. value: function _parseFileSize() {
  111. var fileSize = this.rawFileSize,
  112. kb = fileSize / 1024;
  113. if (!kb) {
  114. return;
  115. } else if (kb < 1024) {
  116. return _ui_utils.mozL10n.get('document_properties_kb', {
  117. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  118. size_b: fileSize.toLocaleString()
  119. }, '{{size_kb}} KB ({{size_b}} bytes)');
  120. }
  121. return _ui_utils.mozL10n.get('document_properties_mb', {
  122. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  123. size_b: fileSize.toLocaleString()
  124. }, '{{size_mb}} MB ({{size_b}} bytes)');
  125. }
  126. }, {
  127. key: '_parseDate',
  128. value: function _parseDate(inputDate) {
  129. var dateToParse = inputDate;
  130. if (dateToParse === undefined) {
  131. return '';
  132. }
  133. if (dateToParse.substring(0, 2) === 'D:') {
  134. dateToParse = dateToParse.substring(2);
  135. }
  136. var year = parseInt(dateToParse.substring(0, 4), 10);
  137. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  138. var day = parseInt(dateToParse.substring(6, 8), 10);
  139. var hours = parseInt(dateToParse.substring(8, 10), 10);
  140. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  141. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  142. var utRel = dateToParse.substring(14, 15);
  143. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  144. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  145. if (utRel === '-') {
  146. hours += offsetHours;
  147. minutes += offsetMinutes;
  148. } else if (utRel === '+') {
  149. hours -= offsetHours;
  150. minutes -= offsetMinutes;
  151. }
  152. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  153. var dateString = date.toLocaleDateString();
  154. var timeString = date.toLocaleTimeString();
  155. return _ui_utils.mozL10n.get('document_properties_date_string', {
  156. date: dateString,
  157. time: timeString
  158. }, '{{date}}, {{time}}');
  159. }
  160. }]);
  161. return PDFDocumentProperties;
  162. }();
  163. exports.PDFDocumentProperties = PDFDocumentProperties;