add anti-stupid mechanism

This commit is contained in:
ayunami2000 2022-07-26 13:41:18 -04:00
parent 29ba21f841
commit 2407d81179

View File

@ -69,6 +69,7 @@
const downloadLink = document.querySelector('a'); const downloadLink = document.querySelector('a');
const progressBar = document.querySelector('progress'); const progressBar = document.querySelector('progress');
const fileElems = document.querySelectorAll('input[type=file]'); const fileElems = document.querySelectorAll('input[type=file]');
function concatTypedArrays(a, b) { function concatTypedArrays(a, b) {
const c = new (a.constructor)(a.length + b.length); const c = new (a.constructor)(a.length + b.length);
c.set(a, 0); c.set(a, 0);
@ -76,6 +77,20 @@
return c; return c;
} }
// https://stackoverflow.com/a/68703218/6917520
function prefix(words) {
// check border cases size 1 array and empty first word)
if (!words[0] || words.length == 1) return words[0] || "";
let i = 0;
// while all words have the same character at position i, increment i
while(words[0][i] && words.every(w => w[i] === words[0][i]))
i++;
// prefix is the substring from the beginning to the last successfully checked i
return words[0].substr(0, i);
}
const textEncoder = new TextEncoder(); const textEncoder = new TextEncoder();
function generateIntArray(num) { function generateIntArray(num) {
return Uint8Array.of((num >>> 24) & 0xFF, (num >>> 16) & 0xFF, (num >>> 8) & 0xFF, num & 0xFF); return Uint8Array.of((num >>> 24) & 0xFF, (num >>> 16) & 0xFF, (num >>> 8) & 0xFF, num & 0xFF);
@ -99,11 +114,12 @@
fileElems.forEach(elem => elem.disabled = true); fileElems.forEach(elem => elem.disabled = true);
if (fileElem.files.length == 1) { if (fileElem.files.length == 1) {
const zipFile = fileElem.files[0]; const zipFile = fileElem.files[0];
var reader = new FileReader(); const reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
const zip = new JSZip(); const zip = new JSZip();
zip.loadAsync(e.target.result).then(function(zip) { zip.loadAsync(e.target.result).then(function(zip) {
progressBar.max = Object.keys(zip.files).length; progressBar.max = Object.keys(zip.files).length;
const stripFolder = prefix(Object.keys(zip.files));
let i = 0; let i = 0;
for (let fileName in zip.files) { for (let fileName in zip.files) {
if (fileName.endsWith('/')) { if (fileName.endsWith('/')) {
@ -114,7 +130,7 @@
continue; continue;
} }
zip.files[fileName].async('uint8array').then(function(data) { zip.files[fileName].async('uint8array').then(function(data) {
processFile(fileName, data); processFile(stripFolder.length == 0 ? fileName : fileName.slice(fileName.indexOf(stripFolder) + stripFolder.length), data);
progressBar.value = i++; progressBar.value = i++;
if (i == progressBar.max) { if (i == progressBar.max) {
wrapItUp(fileElem); wrapItUp(fileElem);
@ -126,11 +142,13 @@
reader.readAsArrayBuffer(zipFile); reader.readAsArrayBuffer(zipFile);
} else if (fileElem.files.length > 1) { } else if (fileElem.files.length > 1) {
progressBar.max = fileElem.files.length; progressBar.max = fileElem.files.length;
const stripFolder = prefix(Object.values(fileElem.files).map(file => file.webkitRelativePath));
let i = 0; let i = 0;
for (let file of fileElem.files) { for (let file of fileElem.files) {
var reader = new FileReader(); const fileName = file.webkitRelativePath;
const reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
processFile(file.webkitRelativePath.slice(file.webkitRelativePath.indexOf('/') + 1), new Uint8Array(e.target.result)); processFile(stripFolder.length == 0 ? fileName : fileName.slice(fileName.indexOf(stripFolder) + stripFolder.length), new Uint8Array(e.target.result));
progressBar.value = i++; progressBar.value = i++;
if (i == progressBar.max) { if (i == progressBar.max) {
wrapItUp(fileElem); wrapItUp(fileElem);