@@ -14,13 +14,19 @@ import osBridge from './osBridge';
1414import * as mica from './mica' ;
1515// import { FFBoxService } from './service/FFBoxService';
1616
17+ interface DownloadMap {
18+ item ?: Electron . DownloadItem ;
19+ finalFileBaseName ?: string ;
20+ dir ?: string ; // 批量下载前指定文件夹,这样每个文件下载时就不弹窗
21+ fileTime ?: { accessTime : number , createTime : number , modifyTime : number } ;
22+ }
1723
1824class ElectronApp {
1925 mainWindow : BrowserWindow | null = null ;
2026 // electronStore: ElectronStore;
2127 service : ProcessInstance | null = null ;
2228 blockWindowClose = true ;
23- downloadMap : Map < string , { item ?: Electron . DownloadItem , finalFileBaseName ?: string , fileTime ?: { accessTime : number , createTime : number , modifyTime : number } } > = new Map ( ) ;
29+ downloadMap : Map < string , DownloadMap > = new Map ( ) ;
2430
2531 constructor ( ) {
2632 this . mountAppEvents ( ) ;
@@ -149,8 +155,11 @@ class ElectronApp {
149155 if ( ! map || map ?. item ) return ;
150156 map . item = item ;
151157
152- item . setSaveDialogOptions ( { defaultPath : map . finalFileBaseName } ) ;
153- // item.setSavePath(folderpath + `\\${item.getFilename()}`); // 设置文件存放位置
158+ if ( map . dir ) {
159+ item . setSavePath ( path . join ( map . dir , map . finalFileBaseName ) ) ;
160+ } else {
161+ item . setSaveDialogOptions ( { defaultPath : map . finalFileBaseName } ) ;
162+ }
154163 mainWindow . webContents . send ( 'downloadStatusChange' , { url : url , status : 'started' } ) ;
155164 item . on ( 'updated' , ( event , state ) => {
156165 if ( state === 'interrupted' ) {
@@ -435,6 +444,19 @@ class ElectronApp {
435444 this . mainWindow ! . webContents . downloadURL ( params . url ) ;
436445 } ) ;
437446
447+ ipcMain . on ( 'downloadFiles' , async ( _event , params : { sessionId : string ; files : { url : string ; finalFileBaseName ?: string ; fileTime ?: any } [ ] } ) => {
448+ const result = await dialog . showOpenDialog ( this . mainWindow , {
449+ title : `指定 ${ params . files . length } 个下载文件的保存文件夹` ,
450+ properties : [ 'openDirectory' , 'createDirectory' ]
451+ } ) ;
452+ if ( ! result . canceled ) {
453+ for ( const file of params . files ) {
454+ this . downloadMap . set ( file . url , { finalFileBaseName : file . finalFileBaseName , fileTime : file . fileTime , dir : result . filePaths [ 0 ] } ) ;
455+ this . mainWindow ! . webContents . downloadURL ( file . url ) ;
456+ }
457+ }
458+ } ) ;
459+
438460 // 启动一个 ffboxService,这个 ffboxService 目前钦定监听 localhost:33269,而 serviceBridge 会连接此 service
439461 ipcMain . handle ( 'startService' , ( ) => this . createService ( ) ) ;
440462
0 commit comments