1
0
mirror of synced 2024-11-15 02:37:40 +01:00

update file shim to use detectFileType.

This commit is contained in:
d98762625 2019-03-20 12:38:49 +00:00
parent 4add484d2a
commit aad1bc898e
4 changed files with 25 additions and 4 deletions

3
package-lock.json generated
View File

@ -8709,7 +8709,8 @@
"mime": {
"version": "2.4.0",
"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": {
"version": "1.37.0",

View File

@ -116,7 +116,6 @@
"lodash": "^4.17.11",
"loglevel": "^1.6.1",
"loglevel-message-prefix": "^3.0.0",
"mime": "^2.4.0",
"moment": "^2.23.0",
"moment-timezone": "^0.5.23",
"ngeohash": "^0.6.3",

View File

@ -5,6 +5,8 @@
*/
import mime from "mime";
import { detectFileType } from "../core/lib/FileType";
/**
* FileShim
@ -37,8 +39,13 @@ class File {
this.name = name;
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";
}
}
/**

View File

@ -2,6 +2,7 @@ import assert from "assert";
import it from "../assertionHandler";
import TestRegister from "../../lib/TestRegister";
import File from "../../../src/node/File";
import {zip} from "../../../src/node/index";
TestRegister.addApiTests([
it("File: should exist", () => {
@ -15,6 +16,19 @@ TestRegister.addApiTests([
assert(typeof file.lastModified, "number");
assert(file.lastModifiedDate instanceof Date);
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");
}),
]);