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.js51
1 files changed, 0 insertions, 51 deletions
diff --git a/src/electron/ipc-api/download.js b/src/electron/ipc-api/download.js
deleted file mode 100644
index ba261ba1e..000000000
--- a/src/electron/ipc-api/download.js
+++ /dev/null
@@ -1,51 +0,0 @@
1import { ipcMain, dialog, BrowserWindow } from 'electron';
2import { download } from 'electron-dl';
3import mime from 'mime-types';
4import { writeFileSync } from 'fs-extra';
5
6const debug = require('debug')('Ferdi:ipcApi:download');
7
8function decodeBase64Image(dataString) {
9 const matches = dataString.match(/^data:([A-Za-z-+/]+);base64,(.+)$/);
10
11 if (matches.length !== 3) {
12 return new Error('Invalid input string');
13 }
14
15 return Buffer.from(matches[2], 'base64');
16}
17
18export default (params) => {
19 ipcMain.on('download-file', async (event, { url, content, fileOptions = {} }) => {
20 const win = BrowserWindow.getFocusedWindow();
21
22 try {
23 if (!content) {
24 const dl = await download(win, url, {
25 saveAs: true,
26 });
27 debug('File saved to', dl.savePath);
28 } else {
29 const extension = mime.extension(fileOptions.mime);
30 const filename = `${fileOptions.name}.${extension}`;
31
32 try {
33 const saveDialog = await dialog.showSaveDialog(params.mainWindow, {
34 defaultPath: filename,
35 });
36
37 if (saveDialog.canceled) return;
38
39 const binaryImage = decodeBase64Image(content);
40 writeFileSync(saveDialog.filePath, binaryImage, 'binary');
41
42 debug('File blob saved to', saveDialog.filePath);
43 } catch (err) {
44 console.log(err);
45 }
46 }
47 } catch (e) {
48 console.error(e);
49 }
50 });
51};