@@ -63,25 +63,29 @@ function updateProgress(callerWindow) {
6363 setProgress . call ( callerWindow , progress ) ;
6464}
6565
66+ async function fileDownloadPath ( callerWindow , options , prompt = true ) {
67+ let filePath = path . join ( app . getPath ( 'downloads' ) , options . defaultPath ) ;
68+ // prompt is true when the user has set the preference to prompt for download location
69+ if ( prompt ) {
70+ const result = await dialog . showSaveDialog ( callerWindow , {
71+ title : 'Save File' ,
72+ ...options ,
73+ } ) ;
74+
75+ if ( result . canceled ) {
76+ return ;
77+ }
78+ filePath = result . filePath ;
79+ }
80+ return filePath ;
81+ }
82+
6683export function setupDownloader ( ) {
6784 // Listen for the renderer's request to show the open dialog
6885 ipcMain . handle ( 'get-download-path' , async ( event , options , prompt = true ) => {
6986 try {
70- let filePath = path . join ( app . getPath ( 'downloads' ) , options . defaultPath ) ;
7187 const callerWindow = BrowserWindow . fromWebContents ( event . sender ) ;
72- // prompt is true when the user has set the preference to prompt for download location
73- if ( prompt ) {
74- const result = await dialog . showSaveDialog ( callerWindow , {
75- title : 'Save File' ,
76- ...options ,
77- } ) ;
78-
79- if ( result . canceled ) {
80- return ;
81- }
82- filePath = result . filePath ;
83- }
84-
88+ const filePath = await fileDownloadPath ( callerWindow , options , prompt ) ;
8589 downloadQueue [ filePath ] = new DownloadItem ( filePath , ( ) => {
8690 updateProgress ( callerWindow ) ;
8791 } , ( ) => {
@@ -118,4 +122,13 @@ export function setupDownloader() {
118122 openFile && shell . openPath ( filePath ) ;
119123 }
120124 } ) ;
125+
126+ // non-streaming direct download
127+ ipcMain . handle ( 'download-base64-url' , async ( event , base64url , options , prompt = true , openFile = false ) => {
128+ const callerWindow = BrowserWindow . fromWebContents ( event . sender ) ;
129+ const filePath = await fileDownloadPath ( callerWindow , options , prompt ) ;
130+ const buffer = Buffer . from ( base64url . split ( ',' ) [ 1 ] , 'base64' ) ;
131+ fs . writeFileSync ( filePath , buffer ) ;
132+ openFile && shell . openPath ( filePath ) ;
133+ } ) ;
121134}
0 commit comments