Browse Source

Fixed the magic bug where it wouldnt recommended operations that resulted in lists of files

n1073645 5 years ago
parent
commit
86db43e6dd

+ 25 - 3
src/core/dishTypes/DishListFile.mjs

@@ -5,7 +5,8 @@
  */
 
 import DishType from "./DishType.mjs";
-import { isNodeEnvironment } from "../Utils.mjs";
+import Utils, { isNodeEnvironment } from "../Utils.mjs";
+import Dish from "../Dish.mjs";
 
 
 /**
@@ -16,13 +17,13 @@ class DishListFile extends DishType {
     /**
      * convert the given value to a ArrayBuffer
      */
-    static toArrayBuffer() {
+    static async toArrayBuffer() {
         DishListFile.checkForValue(this.value);
 
         if (isNodeEnvironment()) {
             this.value = this.value.map(file => Uint8Array.from(file.data));
         }
-        this.value = DishListFile.concatenateTypedArrays(...this.value).buffer;
+        this.value = (this.type === Dish.LIST_FILE ? await DishListFile.concatenateTypedArraysWithTypedElements(...this.value) : await DishListFile.concatenateTypedArrays(...this.value)).buffer;
     }
 
     /**
@@ -33,6 +34,27 @@ class DishListFile extends DishType {
         this.value = [new File(this.value, "unknown")];
     }
 
+    /**
+     * Concatenates a list of typed elements together.
+     *
+     * @param {Uint8Array[]} arrays
+     * @returns {Uint8Array}
+     */
+    static async concatenateTypedArraysWithTypedElements(...arrays) {
+        let totalLength = 0;
+        for (const arr of arrays) {
+            totalLength += arr.size;
+        }
+        const myArray = new Uint8Array(totalLength);
+
+        let offset = 0;
+        for (const arr of arrays) {
+            const data = await Utils.readFile(arr);
+            myArray.set(data, offset);
+            offset += data.length;
+        }
+        return myArray;
+    }
 
     /**
      * Concatenates a list of Uint8Arrays together

+ 1 - 1
src/core/lib/FileSignatures.mjs

@@ -2947,7 +2947,7 @@ export function extractWAV(bytes, offset) {
     stream.moveTo(4);
 
     // Move to file size.
-    stream.moveTo(stream.readInt(4, "le") - 4);
+    stream.moveTo(stream.readInt(4, "le"));
 
     return stream.carve();
 }

+ 1 - 1
src/core/lib/Magic.mjs

@@ -403,7 +403,7 @@ class Magic {
             await recipe.execute(dish);
             // Return an empty buffer if the recipe did not run to completion
             if (recipe.lastRunOp === recipe.opList[recipe.opList.length - 1]) {
-                return dish.get(Dish.ARRAY_BUFFER);
+                return await dish.get(Dish.ARRAY_BUFFER);
             } else {
                 return new ArrayBuffer();
             }

+ 2 - 2
tests/node/tests/NodeDish.mjs

@@ -187,8 +187,8 @@ TestRegister.addApiTests([
         const dish = new Dish([file1, file2], Dish.LIST_FILE);
 
         dish.get(Dish.ARRAY_BUFFER);
-        assert.deepStrictEqual(dish.value, new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b]).buffer);
-        assert.strictEqual(dish.value.byteLength, 11);
+        assert.deepStrictEqual(dish.value, [new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]), new Uint8Array([0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b])]);
+        assert.strictEqual(dish.value.length, 2);
 
         dish.get(Dish.LIST_FILE);
         const dataArray = new Uint8Array(dish.value[0].data);