Use detectFileType to autodetect file extensions in zip

This commit is contained in:
j433866 2019-05-16 09:27:46 +01:00
parent 0cff7bdd04
commit 6237db9ba6
2 changed files with 17 additions and 7 deletions

View File

@ -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";

View File

@ -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});
}