UNPKG

3.07 kBJavaScriptView Raw
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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.FileSpec = void 0;
28
29var _util = require("../shared/util.js");
30
31var _base_stream = require("./base_stream.js");
32
33var _primitives = require("./primitives.js");
34
35function pickPlatformItem(dict) {
36 if (dict.has("UF")) {
37 return dict.get("UF");
38 } else if (dict.has("F")) {
39 return dict.get("F");
40 } else if (dict.has("Unix")) {
41 return dict.get("Unix");
42 } else if (dict.has("Mac")) {
43 return dict.get("Mac");
44 } else if (dict.has("DOS")) {
45 return dict.get("DOS");
46 }
47
48 return null;
49}
50
51class FileSpec {
52 constructor(root, xref) {
53 if (!(root instanceof _primitives.Dict)) {
54 return;
55 }
56
57 this.xref = xref;
58 this.root = root;
59
60 if (root.has("FS")) {
61 this.fs = root.get("FS");
62 }
63
64 this.description = root.has("Desc") ? (0, _util.stringToPDFString)(root.get("Desc")) : "";
65
66 if (root.has("RF")) {
67 (0, _util.warn)("Related file specifications are not supported");
68 }
69
70 this.contentAvailable = true;
71
72 if (!root.has("EF")) {
73 this.contentAvailable = false;
74 (0, _util.warn)("Non-embedded file specifications are not supported");
75 }
76 }
77
78 get filename() {
79 if (!this._filename && this.root) {
80 const filename = pickPlatformItem(this.root) || "unnamed";
81 this._filename = (0, _util.stringToPDFString)(filename).replace(/\\\\/g, "\\").replace(/\\\//g, "/").replace(/\\/g, "/");
82 }
83
84 return this._filename;
85 }
86
87 get content() {
88 if (!this.contentAvailable) {
89 return null;
90 }
91
92 if (!this.contentRef && this.root) {
93 this.contentRef = pickPlatformItem(this.root.get("EF"));
94 }
95
96 let content = null;
97
98 if (this.contentRef) {
99 const fileObj = this.xref.fetchIfRef(this.contentRef);
100
101 if (fileObj instanceof _base_stream.BaseStream) {
102 content = fileObj.getBytes();
103 } else {
104 (0, _util.warn)("Embedded file specification points to non-existing/invalid content");
105 }
106 } else {
107 (0, _util.warn)("Embedded file specification does not have a content");
108 }
109
110 return content;
111 }
112
113 get serializable() {
114 return {
115 filename: this.filename,
116 content: this.content
117 };
118 }
119
120}
121
122exports.FileSpec = FileSpec;
\No newline at end of file