2
0

pdf_document_properties.js 5.6 KB

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