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 { 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
* 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
* collections attributes and related files (/RF)
*/
var FileSpec = (function FileSpecClosure() {
// eslint-disable-next-line no-shadow
function FileSpec(root, xref) {
class FileSpec {
constructor(root, xref) {
if (!root || !isDict(root)) {
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() {
if (!this._filename && this.root) {
var filename = pickPlatformItem(this.root) || "unnamed";
@ -75,7 +73,8 @@ var FileSpec = (function FileSpecClosure() {
.replace(/\\/g, "/");
}
return this._filename;
},
}
get content() {
if (!this.contentAvailable) {
return null;
@ -91,23 +90,21 @@ var FileSpec = (function FileSpecClosure() {
content = fileObj.getBytes();
} else {
warn(
"Embedded file specification points to non-existing/invalid " +
"content"
"Embedded file specification points to non-existing/invalid content"
);
}
} else {
warn("Embedded file specification does not have a content");
}
return content;
},
}
get serializable() {
return {
filename: this.filename,
content: this.content,
};
},
};
return FileSpec;
})();
}
}
export { FileSpec };