pdf_document_properties.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. var uiUtils = require('./ui_utils.js');
  17. var overlayManager = require('./overlay_manager.js');
  18. var getPDFFileNameFromURL = uiUtils.getPDFFileNameFromURL;
  19. var mozL10n = uiUtils.mozL10n;
  20. var OverlayManager = overlayManager.OverlayManager;
  21. var PDFDocumentProperties = function PDFDocumentPropertiesClosure() {
  22. function PDFDocumentProperties(options) {
  23. this.fields = options.fields;
  24. this.overlayName = options.overlayName;
  25. this.container = options.container;
  26. this.rawFileSize = 0;
  27. this.url = null;
  28. this.pdfDocument = null;
  29. if (options.closeButton) {
  30. options.closeButton.addEventListener('click', this.close.bind(this));
  31. }
  32. this.dataAvailablePromise = new Promise(function (resolve) {
  33. this.resolveDataAvailable = resolve;
  34. }.bind(this));
  35. OverlayManager.register(this.overlayName, this.container, this.close.bind(this));
  36. }
  37. PDFDocumentProperties.prototype = {
  38. open: function PDFDocumentProperties_open() {
  39. Promise.all([
  40. OverlayManager.open(this.overlayName),
  41. this.dataAvailablePromise
  42. ]).then(function () {
  43. this._getProperties();
  44. }.bind(this));
  45. },
  46. close: function PDFDocumentProperties_close() {
  47. OverlayManager.close(this.overlayName);
  48. },
  49. setFileSize: function PDFDocumentProperties_setFileSize(fileSize) {
  50. if (fileSize > 0) {
  51. this.rawFileSize = fileSize;
  52. }
  53. },
  54. setDocumentAndUrl: function PDFDocumentProperties_setDocumentAndUrl(pdfDocument, url) {
  55. this.pdfDocument = pdfDocument;
  56. this.url = url;
  57. this.resolveDataAvailable();
  58. },
  59. _getProperties: function PDFDocumentProperties_getProperties() {
  60. if (!OverlayManager.active) {
  61. return;
  62. }
  63. this.pdfDocument.getDownloadInfo().then(function (data) {
  64. if (data.length === this.rawFileSize) {
  65. return;
  66. }
  67. this.setFileSize(data.length);
  68. this._updateUI(this.fields['fileSize'], this._parseFileSize());
  69. }.bind(this));
  70. this.pdfDocument.getMetadata().then(function (data) {
  71. var content = {
  72. 'fileName': getPDFFileNameFromURL(this.url),
  73. 'fileSize': this._parseFileSize(),
  74. 'title': data.info.Title,
  75. 'author': data.info.Author,
  76. 'subject': data.info.Subject,
  77. 'keywords': data.info.Keywords,
  78. 'creationDate': this._parseDate(data.info.CreationDate),
  79. 'modificationDate': this._parseDate(data.info.ModDate),
  80. 'creator': data.info.Creator,
  81. 'producer': data.info.Producer,
  82. 'version': data.info.PDFFormatVersion,
  83. 'pageCount': this.pdfDocument.numPages
  84. };
  85. for (var identifier in content) {
  86. this._updateUI(this.fields[identifier], content[identifier]);
  87. }
  88. }.bind(this));
  89. },
  90. _updateUI: function PDFDocumentProperties_updateUI(field, content) {
  91. if (field && content !== undefined && content !== '') {
  92. field.textContent = content;
  93. }
  94. },
  95. _parseFileSize: function PDFDocumentProperties_parseFileSize() {
  96. var fileSize = this.rawFileSize, kb = fileSize / 1024;
  97. if (!kb) {
  98. return;
  99. } else if (kb < 1024) {
  100. return mozL10n.get('document_properties_kb', {
  101. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  102. size_b: fileSize.toLocaleString()
  103. }, '{{size_kb}} KB ({{size_b}} bytes)');
  104. }
  105. return mozL10n.get('document_properties_mb', {
  106. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  107. size_b: fileSize.toLocaleString()
  108. }, '{{size_mb}} MB ({{size_b}} bytes)');
  109. },
  110. _parseDate: function PDFDocumentProperties_parseDate(inputDate) {
  111. var dateToParse = inputDate;
  112. if (dateToParse === undefined) {
  113. return '';
  114. }
  115. if (dateToParse.substring(0, 2) === 'D:') {
  116. dateToParse = dateToParse.substring(2);
  117. }
  118. var year = parseInt(dateToParse.substring(0, 4), 10);
  119. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  120. var day = parseInt(dateToParse.substring(6, 8), 10);
  121. var hours = parseInt(dateToParse.substring(8, 10), 10);
  122. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  123. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  124. var utRel = dateToParse.substring(14, 15);
  125. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  126. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  127. if (utRel === '-') {
  128. hours += offsetHours;
  129. minutes += offsetMinutes;
  130. } else if (utRel === '+') {
  131. hours -= offsetHours;
  132. minutes -= offsetMinutes;
  133. }
  134. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  135. var dateString = date.toLocaleDateString();
  136. var timeString = date.toLocaleTimeString();
  137. return mozL10n.get('document_properties_date_string', {
  138. date: dateString,
  139. time: timeString
  140. }, '{{date}}, {{time}}');
  141. }
  142. };
  143. return PDFDocumentProperties;
  144. }();
  145. exports.PDFDocumentProperties = PDFDocumentProperties;