file_spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright 2021 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. import { stringToPDFString, warn } from "../shared/util.js";
  16. import { BaseStream } from "./base_stream.js";
  17. import { Dict } from "./primitives.js";
  18. function pickPlatformItem(dict) {
  19. // Look for the filename in this order:
  20. // UF, F, Unix, Mac, DOS
  21. if (dict.has("UF")) {
  22. return dict.get("UF");
  23. } else if (dict.has("F")) {
  24. return dict.get("F");
  25. } else if (dict.has("Unix")) {
  26. return dict.get("Unix");
  27. } else if (dict.has("Mac")) {
  28. return dict.get("Mac");
  29. } else if (dict.has("DOS")) {
  30. return dict.get("DOS");
  31. }
  32. return null;
  33. }
  34. /**
  35. * "A PDF file can refer to the contents of another file by using a File
  36. * Specification (PDF 1.1)", see the spec (7.11) for more details.
  37. * NOTE: Only embedded files are supported (as part of the attachments support)
  38. * TODO: support the 'URL' file system (with caching if !/V), portable
  39. * collections attributes and related files (/RF)
  40. */
  41. class FileSpec {
  42. constructor(root, xref) {
  43. if (!(root instanceof Dict)) {
  44. return;
  45. }
  46. this.xref = xref;
  47. this.root = root;
  48. if (root.has("FS")) {
  49. this.fs = root.get("FS");
  50. }
  51. this.description = root.has("Desc")
  52. ? stringToPDFString(root.get("Desc"))
  53. : "";
  54. if (root.has("RF")) {
  55. warn("Related file specifications are not supported");
  56. }
  57. this.contentAvailable = true;
  58. if (!root.has("EF")) {
  59. this.contentAvailable = false;
  60. 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 = stringToPDFString(filename)
  67. .replace(/\\\\/g, "\\")
  68. .replace(/\\\//g, "/")
  69. .replace(/\\/g, "/");
  70. }
  71. return this._filename;
  72. }
  73. get content() {
  74. if (!this.contentAvailable) {
  75. return null;
  76. }
  77. if (!this.contentRef && this.root) {
  78. this.contentRef = pickPlatformItem(this.root.get("EF"));
  79. }
  80. let content = null;
  81. if (this.contentRef) {
  82. const fileObj = this.xref.fetchIfRef(this.contentRef);
  83. if (fileObj instanceof BaseStream) {
  84. content = fileObj.getBytes();
  85. } else {
  86. warn(
  87. "Embedded file specification points to non-existing/invalid content"
  88. );
  89. }
  90. } else {
  91. warn("Embedded file specification does not have a content");
  92. }
  93. return content;
  94. }
  95. get serializable() {
  96. return {
  97. filename: this.filename,
  98. content: this.content,
  99. };
  100. }
  101. }
  102. export { FileSpec };