aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api/download.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/electron/ipc-api/download.js')
-rw-r--r--src/electron/ipc-api/download.js26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/electron/ipc-api/download.js b/src/electron/ipc-api/download.js
index 36eca3c9f..16de66229 100644
--- a/src/electron/ipc-api/download.js
+++ b/src/electron/ipc-api/download.js
@@ -17,9 +17,15 @@ function decodeBase64Image(dataString) {
17 17
18export default (params) => { 18export default (params) => {
19 ipcMain.on('download-file', async (event, { url, content, fileOptions = {} }) => { 19 ipcMain.on('download-file', async (event, { url, content, fileOptions = {} }) => {
20 // We're passing a fake browserWindow to `electron-dl` in order to access the
21 // webContents of the webview that has initiated the download
22 const fakeWindow = {
23 webContents: event.sender.webContents,
24 };
25
20 try { 26 try {
21 if (!content) { 27 if (!content) {
22 const dl = await download(params.mainWindow, url, { 28 const dl = await download(fakeWindow, url, {
23 saveAs: true, 29 saveAs: true,
24 }); 30 });
25 debug('File saved to', dl.savePath); 31 debug('File saved to', dl.savePath);
@@ -27,14 +33,20 @@ export default (params) => {
27 const extension = mime.extension(fileOptions.mime); 33 const extension = mime.extension(fileOptions.mime);
28 const filename = `${fileOptions.name}.${extension}`; 34 const filename = `${fileOptions.name}.${extension}`;
29 35
30 dialog.showSaveDialog(params.mainWindow, { 36 try {
31 defaultPath: filename, 37 const saveDialog = await dialog.showSaveDialog(params.mainWindow, {
32 }, (name) => { 38 defaultPath: filename,
39 });
40
41 if (saveDialog.canceled) return;
42
33 const binaryImage = decodeBase64Image(content); 43 const binaryImage = decodeBase64Image(content);
34 fs.writeFileSync(name, binaryImage, 'binary'); 44 fs.writeFileSync(saveDialog.filePath, binaryImage, 'binary');
35 45
36 debug('File blob saved to', name); 46 debug('File blob saved to', saveDialog.filePath);
37 }); 47 } catch (err) {
48 console.log(err);
49 }
38 } 50 }
39 } catch (e) { 51 } catch (e) {
40 console.error(e); 52 console.error(e);