diff --git a/src/core/config/scripts/generateConfig.mjs b/src/core/config/scripts/generateConfig.mjs index 492a000c..5f1805f1 100644 --- a/src/core/config/scripts/generateConfig.mjs +++ b/src/core/config/scripts/generateConfig.mjs @@ -53,6 +53,10 @@ for (const opObj in Ops) { if ("inRegexes" in op.checks) { operationConfig[op.name].inputRegexes = op.checks.inRegexes; } + + if ("mimeCheck" in op.checks) { + operationConfig[op.name].mimeCheck = op.checks.mimeCheck; + } } if (!(op.module in modules)) diff --git a/src/core/lib/Magic.mjs b/src/core/lib/Magic.mjs index ebb6b461..c4b7574a 100644 --- a/src/core/lib/Magic.mjs +++ b/src/core/lib/Magic.mjs @@ -5,6 +5,7 @@ import Dish from "../Dish.mjs"; import {detectFileType} from "./FileType.mjs"; import chiSquared from "chi-squared"; import potentialOps from "./Test.mjs"; +import { isImage } from "./FileType.mjs"; /** * A class for detecting encodings, file types and byte frequencies and @@ -244,6 +245,23 @@ class Magic { return results; } + /** + * Checks the mime value of the data compared to the specified type. + * + * @param {ArrayBuffer} data + * @param {string} type + * @returns {boolean} + */ + async checkMime (data, type) { + switch (type) { + case "Image": + if (isImage(data)) + return false; + break; + } + return true; + } + /** * Uses the checks to validate the input/output of potential operations. * @@ -276,6 +294,11 @@ class Magic { return; } + const mime = OperationConfig[op.op].mimeCheck; + if (mime) + if (await this.checkMime(output, mime.type)) + return; + // If the recipe returned an empty buffer, do not continue if (_buffersEqual(output, new ArrayBuffer())) { return; diff --git a/src/core/lib/MagicObject.mjs b/src/core/lib/MagicObject.mjs index 587f29c3..c4b28cd6 100644 --- a/src/core/lib/MagicObject.mjs +++ b/src/core/lib/MagicObject.mjs @@ -5,10 +5,12 @@ class magicObject { /** * @param inRegexes * @param outRegexes + * @param mimeCheck */ - constructor (inRegexes = null, outRegexes = null) { + constructor (inRegexes = null, outRegexes = null, mimeCheck = null) { this.inRegexes = inRegexes; this.outRegexes = outRegexes; + this.mimeCheck = mimeCheck; } } export default magicObject; diff --git a/src/core/operations/ObjectIdentifierToHex.mjs b/src/core/operations/ObjectIdentifierToHex.mjs index f372dd98..0faf72e2 100644 --- a/src/core/operations/ObjectIdentifierToHex.mjs +++ b/src/core/operations/ObjectIdentifierToHex.mjs @@ -28,7 +28,7 @@ class ObjectIdentifierToHex extends Operation { this.args = []; this.checks = new magicObject([ { - match: "^\\s*([0-9]{1,3}\\.?)+\\s*$", + match: "^\\s*([0-9]{1,3}\\.)+[0-9]{1,3}\\s*$", flags: "", magic: true, args: [] diff --git a/src/core/operations/RenderImage.mjs b/src/core/operations/RenderImage.mjs index 4d6d30e4..106a8185 100644 --- a/src/core/operations/RenderImage.mjs +++ b/src/core/operations/RenderImage.mjs @@ -44,7 +44,12 @@ class RenderImage extends Operation { args: ["Raw"], useful: true } - ]); + ], + null, + { + type: "Image", + } + ); } /**