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,24 +64,6 @@ var FileSpec = (function FileSpecClosure() {
} }
} }
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;
}
FileSpec.prototype = {
get filename() { get filename() {
if (!this._filename && this.root) { if (!this._filename && this.root) {
var filename = pickPlatformItem(this.root) || "unnamed"; var filename = pickPlatformItem(this.root) || "unnamed";
@ -75,7 +73,8 @@ var FileSpec = (function FileSpecClosure() {
.replace(/\\/g, "/"); .replace(/\\/g, "/");
} }
return this._filename; return this._filename;
}, }
get content() { get content() {
if (!this.contentAvailable) { if (!this.contentAvailable) {
return null; return null;
@ -91,23 +90,21 @@ var FileSpec = (function FileSpecClosure() {
content = fileObj.getBytes(); content = fileObj.getBytes();
} else { } else {
warn( warn(
"Embedded file specification points to non-existing/invalid " + "Embedded file specification points to non-existing/invalid content"
"content"
); );
} }
} else { } else {
warn("Embedded file specification does not have a content"); warn("Embedded file specification does not have a content");
} }
return content; return content;
}, }
get serializable() { get serializable() {
return { return {
filename: this.filename, filename: this.filename,
content: this.content, content: this.content,
}; };
}, }
}; }
return FileSpec;
})();
export { FileSpec }; export { FileSpec };