2
0

file_spec.js 3.0 KB

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