@@ -548,20 +548,14 @@ class FileDialog extends GenericModal {
548
548
input . click ( ) ;
549
549
}
550
550
551
- // Currently only files are downloadable, but it would be nice to eventually download zipped folders
552
551
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 ( ) ) ) ;
556
553
}
557
554
558
555
async _download ( files ) {
559
556
if ( ! this . _canDownload ( ) ) return ;
560
557
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 ;
565
559
566
560
// Function to read the file contents as a blob
567
561
let getBlob = async ( path ) => {
@@ -576,36 +570,43 @@ class FileDialog extends GenericModal {
576
570
}
577
571
} ;
578
572
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
+
579
579
if ( files . length == 1 && files [ 0 ] . filetype != "folder" ) {
580
+ // Single File Selected
580
581
filename = files [ 0 ] . filename ;
581
- blob = await this . _showBusy ( getBlob ( this . _currentPath + filename ) ) ;
582
+ blob = await getBlob ( this . _currentPath + filename ) ;
582
583
} else {
583
584
// 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
584
585
// If we have nothing selected, we will download the current folder
585
- folder = this . _currentPath ;
586
+ filename = ` ${ getParentFolderName ( ) } .zip` ;
586
587
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 ) ;
589
590
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` ;
596
599
}
597
600
598
601
let zip = new JSZip ( ) ;
599
602
for ( let item of files ) {
600
603
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 ) ;
602
605
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 ) ;
605
607
}
606
608
} 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 ) ;
609
610
}
610
611
}
611
612
blob = await zip . generateAsync ( { type : "blob" } ) ;
0 commit comments