From 6237db9ba685c538085ddae536ff1d4d25004a09 Mon Sep 17 00:00:00 2001 From: j433866 Date: Thu, 16 May 2019 09:27:46 +0100 Subject: [PATCH] Use detectFileType to autodetect file extensions in zip --- src/web/OutputWaiter.mjs | 9 +++------ src/web/ZipWorker.mjs | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/web/OutputWaiter.mjs b/src/web/OutputWaiter.mjs index d3cbc8c7..8e21c2d3 100755 --- a/src/web/OutputWaiter.mjs +++ b/src/web/OutputWaiter.mjs @@ -514,12 +514,9 @@ class OutputWaiter { fileName += ".zip"; } - let fileExt = window.prompt("Please enter a file extension for the files: ", ".txt"); + let fileExt = window.prompt("Please enter a file extension for the files, or leave blank to detect automatically.", ""); - if (fileExt === null) { - // Use .dat as the default file extension - fileExt = ".dat"; - } + if (fileExt === null) fileExt = ""; if (this.zipWorker !== null) { this.terminateZipWorker(); @@ -1076,7 +1073,7 @@ class OutputWaiter { sliceToEl = document.getElementById("output-file-slice-to"), sliceFrom = parseInt(sliceFromEl.value, 10), sliceTo = parseInt(sliceToEl.value, 10), - str = Utils.arrayBufferToStr(this.getActive(true).slice(sliceFrom, sliceTo)); + str = Utils.arrayBufferToStr(this.getActive(false).slice(sliceFrom, sliceTo)); outputText.classList.remove("blur"); showFileOverlay.style.display = "block"; diff --git a/src/web/ZipWorker.mjs b/src/web/ZipWorker.mjs index 039b1e32..6af603cc 100644 --- a/src/web/ZipWorker.mjs +++ b/src/web/ZipWorker.mjs @@ -8,6 +8,7 @@ import zip from "zlibjs/bin/zip.min"; import Utils from "../core/Utils"; +import {detectFileType} from "../core/lib/FileType"; const Zlib = zip.Zlib; @@ -48,7 +49,7 @@ self.zipFiles = function(outputs, filename, fileExtension) { for (let i = 0; i < inputNums.length; i++) { const iNum = inputNums[i]; - const name = Utils.strToByteArray(iNum + fileExtension); + let ext = fileExtension; let output; if (outputs[iNum].data === null) { @@ -58,6 +59,18 @@ self.zipFiles = function(outputs, filename, fileExtension) { } else { output = new Uint8Array(outputs[iNum].data.dish.value); } + + if (fileExtension === "") { + // Detect automatically + const types = detectFileType(output); + if (!types.length) { + ext = ".dat"; + } else { + ext = `.${types[0].extension.split(",", 1)[0]}`; + } + } + const name = Utils.strToByteArray(iNum + ext); + zip.addFile(output, {filename: name}); }