Przeglądaj źródła

update file shim to use detectFileType.

d98762625 6 lat temu
rodzic
commit
aad1bc898e
4 zmienionych plików z 25 dodań i 4 usunięć
  1. 2 1
      package-lock.json
  2. 0 1
      package.json
  3. 8 1
      src/node/File.mjs
  4. 15 1
      tests/node/tests/File.mjs

+ 2 - 1
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",

+ 0 - 1
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",

+ 8 - 1
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";
+        }
     }
 
     /**

+ 15 - 1
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");
     }),
 ]);