Skip to content

Commit e94c613

Browse files
committed
Fixed Downloading files in all cases
1 parent 6724e80 commit e94c613

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

js/common/file_dialog.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,14 @@ class FileDialog extends GenericModal {
548548
input.click();
549549
}
550550

551-
// Currently only files are downloadable, but it would be nice to eventually download zipped folders
552551
async _handleDownloadButton() {
553-
// TODO: Implement a way to download multiple files at once into a zip file
554-
555-
await this._download(this._getSelectedFilesInfo());
552+
await this._showBusy(this._download(this._getSelectedFilesInfo()));
556553
}
557554

558555
async _download(files) {
559556
if (!this._canDownload()) return;
560557

561-
let folder, blob, filename;
562-
563-
// If we only have 1 item and it is a file, we can download it directly
564-
// Otherwise, we need to zip the files and download the zip keeping the structure intact
558+
let blob, filename;
565559

566560
// Function to read the file contents as a blob
567561
let getBlob = async (path) => {
@@ -576,36 +570,43 @@ class FileDialog extends GenericModal {
576570
}
577571
};
578572

573+
let addFileContentsToZip = async (zip, folder, location) => {
574+
let contents = await getBlob(folder + location);
575+
// Get the filename only from the path
576+
zip.file(location, contents);
577+
};
578+
579579
if (files.length == 1 && files[0].filetype != "folder") {
580+
// Single File Selected
580581
filename = files[0].filename;
581-
blob = await this._showBusy(getBlob(this._currentPath + filename));
582+
blob = await getBlob(this._currentPath + filename);
582583
} else {
583584
// We either have more than 1 item selected or we have a folder selected or we have no file selected and want to download the current folder
584585
// If we have nothing selected, we will download the current folder
585-
folder = this._currentPath;
586+
filename = `${getParentFolderName()}.zip`;
586587
if (files.length == 0) {
587-
files.push({filename: getParentFolderName(), filetype: "folder", path: this._currentPath});
588-
}
588+
// No Files Selected, so get everything in current folder
589+
const filesInFolder = await this._fileHelper.listDir(this._currentPath);
589590

590-
if (files.length == 1) {
591-
filename = files[0].filename;
592-
folder += filename + "/";
593-
filename = `${filename}.zip`;
594-
} else {
595-
filename = `${getParentFolderName()}.zip`;
591+
// Add all files in current folder to files array
592+
for (let fileObj of filesInFolder) {
593+
if (this._hidePaths.has(this._currentPath + fileObj.path)) continue;
594+
files.push({filename: fileObj.path, filetype: fileObj.isDir ? "folder" : "file", path: this._currentPath});
595+
}
596+
} else if (files.length == 1) {
597+
// Single Folder Selected
598+
filename = `${files[0].filename}.zip`;
596599
}
597600

598601
let zip = new JSZip();
599602
for (let item of files) {
600603
if (item.filetype == "folder") {
601-
let containedFiles = await this._fileHelper.findContainedFiles(folder + item + "/", true);
604+
let containedFiles = await this._fileHelper.findContainedFiles(item.path + item.filename + "/", true);
602605
for (let location of containedFiles) {
603-
let contents = await this._showBusy(getBlob(folder + location));
604-
zip.file(location, contents);
606+
await addFileContentsToZip(zip, item.path, item.filename + "/" + location);
605607
}
606608
} else {
607-
let contents = await this._showBusy(getBlob(folder + item.filename));
608-
zip.file(item.filename, contents);
609+
await addFileContentsToZip(zip, item.path, item.filename);
609610
}
610611
}
611612
blob = await zip.generateAsync({type: "blob"});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"dependencies": {
1616
"@adafruit/ble-file-transfer-js": "adafruit/ble-file-transfer-js#1.0.2",
17-
"@adafruit/circuitpython-repl-js": "adafruit/circuitpython-repl-js#3.2.2",
17+
"@adafruit/circuitpython-repl-js": "adafruit/circuitpython-repl-js#3.2.3",
1818
"@codemirror/lang-python": "^6.1.6",
1919
"@fortawesome/fontawesome-free": "^6.6.0",
2020
"@xterm/addon-fit": "^0.10.0",

0 commit comments

Comments
 (0)