Skip to content

Commit

Permalink
add file downloading to fsapp
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Sep 12, 2024
1 parent 761e607 commit bf0a7d4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apps/fsapp.app/GUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ addContextMenuItem("Refresh", function () {
reload();
});

newcontextmenu.addItem("Download", function () {
download();
});

appcontextmenu.addItem("Install (Session)", function () {
installSession();
});
Expand Down
2 changes: 1 addition & 1 deletion apps/fsapp.app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@
<!-- in any sane case this wouldn't be required. This isn't a sane case, Buffer doesn't work without importing this -->
<script src="/libs/filer/filer.min.js"></script>
<script src="/libs/dreamland/all.js"></script>
<script src="GUI.js"></script>
<script src="index.mjs" type="module"></script>
<script src="GUI.js"></script>
<script src="operations.js"></script>
</head>
</html>
28 changes: 27 additions & 1 deletion apps/fsapp.app/operations.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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];
Expand All @@ -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(
Expand Down

0 comments on commit bf0a7d4

Please sign in to comment.