Merge branch 'v9' of github.com:gchq/CyberChef into node-lib

This commit is contained in:
d98762625 2019-07-05 11:07:31 +01:00
commit 368f508b17
122 changed files with 24317 additions and 4174 deletions

View file

@ -8,6 +8,7 @@
import Utils, { isNodeEnvironment } from "./Utils";
import DishError from "./errors/DishError";
import BigNumber from "bignumber.js";
import { detectFileType } from "./lib/FileType";
import log from "loglevel";
import {
@ -199,6 +200,54 @@ class Dish {
return clone.get(type, notUTF8);
}
/**
* Detects the MIME type of the current dish
* @returns {string}
*/
async detectDishType() {
const data = new Uint8Array(this.value.slice(0, 2048)),
types = detectFileType(data);
if (!types.length || !types[0].mime || !types[0].mime === "text/plain") {
return null;
} else {
return types[0].mime;
}
}
/**
* Returns the title of the data up to the specified length
*
* @param {number} maxLength - The maximum title length
* @returns {string}
*/
async getTitle(maxLength) {
let title = "";
let cloned;
switch (this.type) {
case Dish.FILE:
title = this.value.name;
break;
case Dish.LIST_FILE:
title = `${this.value.length} file(s)`;
break;
case Dish.ARRAY_BUFFER:
case Dish.BYTE_ARRAY:
title = await this.detectDishType();
if (title !== null) break;
// fall through if no mime type was detected
default:
cloned = this.clone();
cloned.value = cloned.value.slice(0, 256);
title = await cloned.get(Dish.STRING);
}
return title.slice(0, maxLength);
}
/**
* Validates that the value is the type that has been specified.
* May have to disable parts of BYTE_ARRAY validation if it effects performance.