2
0

file_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.FileSpec = void 0;
  27. var _util = require("../shared/util.js");
  28. var _base_stream = require("./base_stream.js");
  29. var _primitives = require("./primitives.js");
  30. function pickPlatformItem(dict) {
  31. if (dict.has("UF")) {
  32. return dict.get("UF");
  33. } else if (dict.has("F")) {
  34. return dict.get("F");
  35. } else if (dict.has("Unix")) {
  36. return dict.get("Unix");
  37. } else if (dict.has("Mac")) {
  38. return dict.get("Mac");
  39. } else if (dict.has("DOS")) {
  40. return dict.get("DOS");
  41. }
  42. return null;
  43. }
  44. class FileSpec {
  45. constructor(root, xref) {
  46. if (!(root instanceof _primitives.Dict)) {
  47. return;
  48. }
  49. this.xref = xref;
  50. this.root = root;
  51. if (root.has("FS")) {
  52. this.fs = root.get("FS");
  53. }
  54. this.description = root.has("Desc") ? (0, _util.stringToPDFString)(root.get("Desc")) : "";
  55. if (root.has("RF")) {
  56. (0, _util.warn)("Related file specifications are not supported");
  57. }
  58. this.contentAvailable = true;
  59. if (!root.has("EF")) {
  60. this.contentAvailable = false;
  61. (0, _util.warn)("Non-embedded file specifications are not supported");
  62. }
  63. }
  64. get filename() {
  65. if (!this._filename && this.root) {
  66. const filename = pickPlatformItem(this.root) || "unnamed";
  67. this._filename = (0, _util.stringToPDFString)(filename).replace(/\\\\/g, "\\").replace(/\\\//g, "/").replace(/\\/g, "/");
  68. }
  69. return this._filename;
  70. }
  71. get content() {
  72. if (!this.contentAvailable) {
  73. return null;
  74. }
  75. if (!this.contentRef && this.root) {
  76. this.contentRef = pickPlatformItem(this.root.get("EF"));
  77. }
  78. let content = null;
  79. if (this.contentRef) {
  80. const fileObj = this.xref.fetchIfRef(this.contentRef);
  81. if (fileObj instanceof _base_stream.BaseStream) {
  82. content = fileObj.getBytes();
  83. } else {
  84. (0, _util.warn)("Embedded file specification points to non-existing/invalid content");
  85. }
  86. } else {
  87. (0, _util.warn)("Embedded file specification does not have a content");
  88. }
  89. return content;
  90. }
  91. get serializable() {
  92. return {
  93. filename: this.filename,
  94. content: this.content
  95. };
  96. }
  97. }
  98. exports.FileSpec = FileSpec;