Convert the FileSpec to a "normal" class

This commit is contained in:
Jonas Jenwald 2021-04-13 18:25:52 +02:00
parent e02d17da93
commit 22a066e657

View File

@ -17,6 +17,23 @@
import { isDict, isStream } from "./primitives.js"; import { isDict, isStream } from "./primitives.js";
import { stringToPDFString, warn } from "../shared/util.js"; import { stringToPDFString, warn } from "../shared/util.js";
function pickPlatformItem(dict) {
// Look for the filename in this order:
// UF, F, Unix, Mac, DOS
if (dict.has("UF")) {
return dict.get("UF");
} else if (dict.has("F")) {
return dict.get("F");
} else if (dict.has("Unix")) {
return dict.get("Unix");
} else if (dict.has("Mac")) {
return dict.get("Mac");
} else if (dict.has("DOS")) {
return dict.get("DOS");
}
return null;
}
/** /**
* "A PDF file can refer to the contents of another file by using a File * "A PDF file can refer to the contents of another file by using a File
* Specification (PDF 1.1)", see the spec (7.11) for more details. * Specification (PDF 1.1)", see the spec (7.11) for more details.
@ -24,9 +41,8 @@ import { stringToPDFString, warn } from "../shared/util.js";
* TODO: support the 'URL' file system (with caching if !/V), portable * TODO: support the 'URL' file system (with caching if !/V), portable
* collections attributes and related files (/RF) * collections attributes and related files (/RF)
*/ */
var FileSpec = (function FileSpecClosure() { class FileSpec {
// eslint-disable-next-line no-shadow constructor(root, xref) {
function FileSpec(root, xref) {
if (!root || !isDict(root)) { if (!root || !isDict(root)) {
return; return;
} }
@ -48,66 +64,47 @@ var FileSpec = (function FileSpecClosure() {
} }
} }
function pickPlatformItem(dict) { get filename() {
// Look for the filename in this order: if (!this._filename && this.root) {
// UF, F, Unix, Mac, DOS var filename = pickPlatformItem(this.root) || "unnamed";
if (dict.has("UF")) { this._filename = stringToPDFString(filename)
return dict.get("UF"); .replace(/\\\\/g, "\\")
} else if (dict.has("F")) { .replace(/\\\//g, "/")
return dict.get("F"); .replace(/\\/g, "/");
} else if (dict.has("Unix")) {
return dict.get("Unix");
} else if (dict.has("Mac")) {
return dict.get("Mac");
} else if (dict.has("DOS")) {
return dict.get("DOS");
} }
return null; return this._filename;
} }
FileSpec.prototype = { get content() {
get filename() { if (!this.contentAvailable) {
if (!this._filename && this.root) { return null;
var filename = pickPlatformItem(this.root) || "unnamed"; }
this._filename = stringToPDFString(filename) if (!this.contentRef && this.root) {
.replace(/\\\\/g, "\\") this.contentRef = pickPlatformItem(this.root.get("EF"));
.replace(/\\\//g, "/") }
.replace(/\\/g, "/"); var content = null;
} if (this.contentRef) {
return this._filename; var xref = this.xref;
}, var fileObj = xref.fetchIfRef(this.contentRef);
get content() { if (fileObj && isStream(fileObj)) {
if (!this.contentAvailable) { content = fileObj.getBytes();
return null;
}
if (!this.contentRef && this.root) {
this.contentRef = pickPlatformItem(this.root.get("EF"));
}
var content = null;
if (this.contentRef) {
var xref = this.xref;
var fileObj = xref.fetchIfRef(this.contentRef);
if (fileObj && isStream(fileObj)) {
content = fileObj.getBytes();
} else {
warn(
"Embedded file specification points to non-existing/invalid " +
"content"
);
}
} else { } else {
warn("Embedded file specification does not have a content"); warn(
"Embedded file specification points to non-existing/invalid content"
);
} }
return content; } else {
}, warn("Embedded file specification does not have a content");
get serializable() { }
return { return content;
filename: this.filename, }
content: this.content,
}; get serializable() {
}, return {
}; filename: this.filename,
return FileSpec; content: this.content,
})(); };
}
}
export { FileSpec }; export { FileSpec };