1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.FileSpec = void 0;
|
28 |
|
29 | var _util = require("../shared/util.js");
|
30 |
|
31 | var _base_stream = require("./base_stream.js");
|
32 |
|
33 | var _primitives = require("./primitives.js");
|
34 |
|
35 | function 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 |
|
51 | class 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 |
|
122 | exports.FileSpec = FileSpec; |
\ | No newline at end of file |