From bf0a7d4fca301b1c4c22e62d62c16edd3138b9ad Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Thu, 12 Sep 2024 12:48:35 -0500 Subject: [PATCH] add file downloading to fsapp --- apps/fsapp.app/GUI.js | 4 ++++ apps/fsapp.app/index.html | 2 +- apps/fsapp.app/operations.js | 28 +++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/apps/fsapp.app/GUI.js b/apps/fsapp.app/GUI.js index e0006731..1f85efa5 100644 --- a/apps/fsapp.app/GUI.js +++ b/apps/fsapp.app/GUI.js @@ -34,6 +34,10 @@ addContextMenuItem("Refresh", function () { reload(); }); +newcontextmenu.addItem("Download", function () { + download(); +}); + appcontextmenu.addItem("Install (Session)", function () { installSession(); }); diff --git a/apps/fsapp.app/index.html b/apps/fsapp.app/index.html index 5b1f365c..6dc16b55 100644 --- a/apps/fsapp.app/index.html +++ b/apps/fsapp.app/index.html @@ -129,8 +129,8 @@ - + diff --git a/apps/fsapp.app/operations.js b/apps/fsapp.app/operations.js index 4f18c4df..5548224f 100644 --- a/apps/fsapp.app/operations.js +++ b/apps/fsapp.app/operations.js @@ -1,3 +1,4 @@ +// this app should be refactored again, (someday?) but im not doing it rn so async function selectAction(selected) { currentlySelected.forEach((row) => { row.classList.remove("selected"); @@ -725,7 +726,8 @@ function reload() { } function upload() { - let fauxput = document.createElement("input"); // fauxput - fake input that isn't shown or ever added to page TODO: think of a better name for this variable + // TODO: think of a better name for this variable + const fauxput = document.createElement("input"); // fauxput - fake input that isn't shown or ever added to page fauxput.type = "file"; fauxput.onchange = async (e) => { const file = await e.target.files[0]; @@ -743,6 +745,30 @@ function upload() { fauxput.click(); } +async function download() { + for (const selected of currentlySelected) { + if (selected.getAttribute("data-type") !== "file") { + return; + } + const filePath = selected.getAttribute("data-path"); + const fileData = await fs.promises.readFile(filePath); + const file = await fs.promises.stat(filePath); + // keeping up the bad names + // TODO: this name is horrible, fix it + const fauxnchor = document.createElement("a"); + fauxnchor.style.display = "none"; + document.body.appendChild(fauxnchor); + const blob = new Blob([fileData], { type: "application/octet-stream" }); + const url = window.URL.createObjectURL(blob); + fauxnchor.href = url; + fauxnchor.download = file.name; + fauxnchor.click(); + + window.URL.revokeObjectURL(url); + fauxnchor.remove(); + } +} + function deleteFile() { currentlySelected.forEach(async (item) => { await sh.rm(