From 5e047443178937cef6b19885ca4d2707085436ee Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 27 Apr 2022 10:17:41 -0600 Subject: [PATCH] literally zip folders kek --- json/ctx.json | 4 +- package-lock.json | 14 +++++ package.json | 1 + scripts/contextmenu/compress.js | 92 +++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 scripts/contextmenu/compress.js diff --git a/json/ctx.json b/json/ctx.json index 5eb3707..af81f7c 100755 --- a/json/ctx.json +++ b/json/ctx.json @@ -1,6 +1,8 @@ [ [ - {"name": "New Folder", "id": "ctx-new-folder", "onclick": "newfolder"} + {"name": "New Folder", "id": "ctx-new-folder", "onclick": "newfolder"}, + {"name": "Compress", "id": "ctx-compress", "onclick": "compress"}, + {"name": "Decompress", "id": "ctx-decompress", "onclick": "decompress"} ], [ {"name": "Sort by: Name", "id": "ctx-sort-by", "onclick": "changesort"}, diff --git a/package-lock.json b/package-lock.json index 0b67d86..b505e8a 100755 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.7.0", "license": "ISC", "dependencies": { + "adm-zip": "^0.5.9", "chalk": "^4.1.2", "electron-squirrel-startup": "^1.0.0", "generator-code": "^1.6.5" @@ -1993,6 +1994,14 @@ "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", "dev": true }, + "node_modules/adm-zip": { + "version": "0.5.9", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.9.tgz", + "integrity": "sha512-s+3fXLkeeLjZ2kLjCBwQufpI5fuN+kIGBxu6530nVQZGVol0d7Y/M88/xw9HGGUcJjKf8LutN3VPRUBq6N7Ajg==", + "engines": { + "node": ">=6.0" + } + }, "node_modules/agent-base": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", @@ -15218,6 +15227,11 @@ "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", "dev": true }, + "adm-zip": { + "version": "0.5.9", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.9.tgz", + "integrity": "sha512-s+3fXLkeeLjZ2kLjCBwQufpI5fuN+kIGBxu6530nVQZGVol0d7Y/M88/xw9HGGUcJjKf8LutN3VPRUBq6N7Ajg==" + }, "agent-base": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", diff --git a/package.json b/package.json index c297ada..5d069ba 100755 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "author": "WubzyGD", "license": "ISC", "dependencies": { + "adm-zip": "^0.5.9", "chalk": "^4.1.2", "electron-squirrel-startup": "^1.0.0", "generator-code": "^1.6.5" diff --git a/scripts/contextmenu/compress.js b/scripts/contextmenu/compress.js new file mode 100644 index 0000000..3b90e1b --- /dev/null +++ b/scripts/contextmenu/compress.js @@ -0,0 +1,92 @@ +const fs = require('fs'); +const path = require('path'); +const az = require('adm-zip'); +const Mousetrap = require('../dep/mousetrap'); + +const lightRefresh = require('../fileview/lightrefresh'); +const preModal = require('../modal/pre'); +const postModal = require('../modal/post'); +const showError = require('../modal/common/error'); +const clearModals = require('../modal/clearmodals'); +const newToast = require('../toast/createtoast'); +const refresh = require('../fileview/refresh'); + +module.exports = () => { + let zip = new az(); + if (window.kade.modal) {return;} + preModal('compress-folder-modal-container'); + let modalOut = document.createElement('div'); + modalOut.className = 'modal'; + modalOut.id = 'compress-folder-modal-container'; + document.body.appendChild(modalOut); + let modal = document.createElement('div'); + modal.className = 'modal-wrapper'; + modalOut.appendChild(modal); + let title = document.createElement('h2'); + title.innerHTML = 'Compress Folder'; + modal.appendChild(title); + let text = document.createElement('p'); + text.innerHTML = "Please name the zip you'd like to compress to."; + modal.appendChild(text); + let cont = document.createElement('div'); + cont.className = 'button-container'; + modal.appendChild(cont); + let input = document.createElement('input'); + input.placeholder = window.kade.cpath.split(/\\+|\/+/gm).reverse()[0]; + input.id = 'compress-folder-input'; + let lastIn = ''; + input.oninput = () => { + if (!input.value.match(/^[a-zA-Z0-9-_() ]*$/gm)) {input.value = lastIn;} + else {lastIn = input.value;} + }; + cont.appendChild(input); + let conf = document.createElement('button'); + conf.innerHTML = 'Create'; + conf.onclick = () => { + try { + input.value = input.value.trim(); + if (!input.value.endsWith('.zip')) {input.value = `${input.value}.zip`;} + if (!input.value.length) {return;} + if (fs.existsSync(path.join(window.kade.cpath, input.value))) { + if (!input.value.match(/^.+\(\d\)$/gm)) {input.value += ' (1)';} + else { + let tempstr = input.value.split(''); + tempstr[input.value.length - 2] = `${Number(input.value.charAt(input.value.length - 2)) + 1}`; + input.value = tempstr.join(''); + } + return; + } + zip.addLocalFolderPromise(window.kade.cpath).then(() => { + modalOut.remove(); + postModal(modalOut.id); + zip.writeZipPromise(`${window.kade.cpath}/${input.value}`, {overwrite: true}).then(() => { + newToast("Folder compressed", [`The current folder was compressed into "${input.value}" created successfully`, `${window.kade.cpath}/${input.value}`], undefined, false, 5); + }); + }); + } catch { + newToast("Folder not Compressed", "An error caused that folder to not be compressed.", "#b24355", false, 5, () => {showError("Folder Creation", "There was an unknown error while trying to compress that folder. It may be a permissions issue, or the host folder doesn't exist anymore.");}); + clearModals(); + } + postModal(modalOut.id); + }; + cont.appendChild(conf); + input.focus(); + let msm = new Mousetrap(modal); + msm.bind('esc', () => { + lightRefresh(); + modalOut.remove(); + postModal(modalOut.id); + }); + msm.bind('enter', () => {conf.click();}); + let close = document.createElement('a'); + close.className = 'close-button'; + close.onclick = () => { + lightRefresh(); + modalOut.remove(); + postModal(modalOut.id); + }; + let closeWrap = document.createElement('div'); + closeWrap.className = 'close-button-wrapper'; + modal.appendChild(closeWrap); + closeWrap.appendChild(close); +}; \ No newline at end of file