From 23bdfd04a271a9ad19338d3c91485952f18cc3f0 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Sat, 10 Feb 2018 15:31:50 +0000 Subject: [PATCH] Magic operation now shows matching ops even if they are not run. --- src/core/FlowControl.js | 23 +++++++++++++---------- src/core/lib/Magic.js | 9 +++++---- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/core/FlowControl.js b/src/core/FlowControl.js index 059cdebc..cc8b68c9 100755 --- a/src/core/FlowControl.js +++ b/src/core/FlowControl.js @@ -277,7 +277,7 @@ const FlowControl = { style='table-layout: fixed;'> Recipe (click to load) - Data snippet + Result snippet Properties `; @@ -290,24 +290,27 @@ const FlowControl = { recipeURL = "recipe=" + Utils.encodeURIFragment(Utils.generatePrettyRecipe(recipeConfig)); const bestLanguage = option.languageScores[0]; - let language = "Unknown", - fileType = "Unknown"; + let language = "", + fileType = "", + matchingOps = "", + validUTF8 = option.isUTF8 ? "Valid UTF8\n" : ""; - if (bestLanguage.probability > 0.00005) { - language = Magic.codeToLanguage(bestLanguage.lang) + " " + - (bestLanguage.probability * 100).toFixed(2) + "%"; + if (bestLanguage.probability > 0.00001) { + language = `Language: ${Magic.codeToLanguage(bestLanguage.lang)} ${(bestLanguage.probability * 100).toFixed(2)}%\n`; } if (option.fileType) { - fileType = `${option.fileType.mime} (${option.fileType.ext})`; + fileType = `File type: ${option.fileType.mime} (${option.fileType.ext})\n`; + } + + if (option.matchingOps.length) { + matchingOps = `Matching ops: ${[...new Set(option.matchingOps.map(op => op.op))].join(", ")}\n`; } output += ` ${Utils.generatePrettyRecipe(option.recipe, true)} ${Utils.escapeHtml(Utils.printable(Utils.truncate(option.data, 99)))} - Language: ${language} -File type: ${fileType} -Valid UTF8: ${option.isUTF8} + ${language}${fileType}${matchingOps}${validUTF8} `; }); diff --git a/src/core/lib/Magic.js b/src/core/lib/Magic.js index b93ad2ec..96969d2e 100644 --- a/src/core/lib/Magic.js +++ b/src/core/lib/Magic.js @@ -174,6 +174,9 @@ class Magic { async speculativeExecution(depth = 0, recipeConfig = []) { if (depth < 0) return []; + // Find any operations that can be run on this data + const matchingOps = this.findMatchingOps(); + let results = []; // Record the properties of the current data @@ -182,12 +185,10 @@ class Magic { data: this.inputStr.slice(0, 100), languageScores: this.detectLanguage(), fileType: this.detectFileType(), - isUTF8: this.isUTF8() + isUTF8: this.isUTF8(), + matchingOps: matchingOps }); - // Find any operations that can be run on this data - const matchingOps = this.findMatchingOps(); - // Execute each of those operations, then recursively call the speculativeExecution() method // on the resulting data, recording the properties of each option. await Promise.all(matchingOps.map(async op => {