From aad1bc898e6b8cac96610a7815c6fdfeb3671a2d Mon Sep 17 00:00:00 2001 From: d98762625 Date: Wed, 20 Mar 2019 12:38:49 +0000 Subject: [PATCH] update file shim to use detectFileType. --- package-lock.json | 3 ++- package.json | 1 - src/node/File.mjs | 9 ++++++++- tests/node/tests/File.mjs | 16 +++++++++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 45580c8e..a8157dc7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 3da84254..5d5dc72b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/node/File.mjs b/src/node/File.mjs index 0ecee7e5..16ca4453 100644 --- a/src/node/File.mjs +++ b/src/node/File.mjs @@ -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"; + } } /** diff --git a/tests/node/tests/File.mjs b/tests/node/tests/File.mjs index dc9ddffc..6a40ce4f 100644 --- a/tests/node/tests/File.mjs +++ b/tests/node/tests/File.mjs @@ -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"); }), ]);