mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 15:26:16 -04:00
update file shim to use detectFileType.
This commit is contained in:
parent
4add484d2a
commit
aad1bc898e
4 changed files with 25 additions and 4 deletions
3
package-lock.json
generated
3
package-lock.json
generated
|
@ -8709,7 +8709,8 @@
|
||||||
"mime": {
|
"mime": {
|
||||||
"version": "2.4.0",
|
"version": "2.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/mime/-/mime-2.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/mime/-/mime-2.4.0.tgz",
|
||||||
"integrity": "sha512-ikBcWwyqXQSHKtciCcctu9YfPbFYZ4+gbHEmE0Q8jzcTYQg5dHCr3g2wwAZjPoJfQVXZq6KXAjpXOTf5/cjT7w=="
|
"integrity": "sha512-ikBcWwyqXQSHKtciCcctu9YfPbFYZ4+gbHEmE0Q8jzcTYQg5dHCr3g2wwAZjPoJfQVXZq6KXAjpXOTf5/cjT7w==",
|
||||||
|
"dev": true
|
||||||
},
|
},
|
||||||
"mime-db": {
|
"mime-db": {
|
||||||
"version": "1.37.0",
|
"version": "1.37.0",
|
||||||
|
|
|
@ -116,7 +116,6 @@
|
||||||
"lodash": "^4.17.11",
|
"lodash": "^4.17.11",
|
||||||
"loglevel": "^1.6.1",
|
"loglevel": "^1.6.1",
|
||||||
"loglevel-message-prefix": "^3.0.0",
|
"loglevel-message-prefix": "^3.0.0",
|
||||||
"mime": "^2.4.0",
|
|
||||||
"moment": "^2.23.0",
|
"moment": "^2.23.0",
|
||||||
"moment-timezone": "^0.5.23",
|
"moment-timezone": "^0.5.23",
|
||||||
"ngeohash": "^0.6.3",
|
"ngeohash": "^0.6.3",
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import mime from "mime";
|
import mime from "mime";
|
||||||
|
import { detectFileType } from "../core/lib/FileType";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FileShim
|
* FileShim
|
||||||
|
@ -37,8 +39,13 @@ class File {
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.lastModified = stats.lastModified || Date.now();
|
this.lastModified = stats.lastModified || Date.now();
|
||||||
this.type = stats.type || mime.getType(this.name);
|
|
||||||
|
|
||||||
|
const types = detectFileType(this.data);
|
||||||
|
if (types.length) {
|
||||||
|
this.type = types[0].mime;
|
||||||
|
} else {
|
||||||
|
this.type = "application/unknown";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,6 +2,7 @@ import assert from "assert";
|
||||||
import it from "../assertionHandler";
|
import it from "../assertionHandler";
|
||||||
import TestRegister from "../../lib/TestRegister";
|
import TestRegister from "../../lib/TestRegister";
|
||||||
import File from "../../../src/node/File";
|
import File from "../../../src/node/File";
|
||||||
|
import {zip} from "../../../src/node/index";
|
||||||
|
|
||||||
TestRegister.addApiTests([
|
TestRegister.addApiTests([
|
||||||
it("File: should exist", () => {
|
it("File: should exist", () => {
|
||||||
|
@ -15,6 +16,19 @@ TestRegister.addApiTests([
|
||||||
assert(typeof file.lastModified, "number");
|
assert(typeof file.lastModified, "number");
|
||||||
assert(file.lastModifiedDate instanceof Date);
|
assert(file.lastModifiedDate instanceof Date);
|
||||||
assert.equal(file.size, uint8Array.length);
|
assert.equal(file.size, uint8Array.length);
|
||||||
assert.equal(file.type, "text/plain");
|
assert.equal(file.type, "application/unknown");
|
||||||
|
}),
|
||||||
|
|
||||||
|
it("File: Should determine the type of a file", () => {
|
||||||
|
const zipped = zip("hello");
|
||||||
|
const file = new File([zipped.value]);
|
||||||
|
assert(file);
|
||||||
|
assert.strictEqual(file.type, "application/zip");
|
||||||
|
}),
|
||||||
|
|
||||||
|
it("File: unknown type should have a type of application/unknown", () => {
|
||||||
|
const uint8Array = new Uint8Array(Buffer.from("hello"));
|
||||||
|
const file = new File([uint8Array], "sample.txt");
|
||||||
|
assert.strictEqual(file.type, "application/unknown");
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue