diff options
Diffstat (limited to 'src/electron/ipc-api/download.js')
-rw-r--r-- | src/electron/ipc-api/download.js | 51 |
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 @@ | |||
1 | import { ipcMain, dialog, BrowserWindow } from 'electron'; | ||
2 | import { download } from 'electron-dl'; | ||
3 | import mime from 'mime-types'; | ||
4 | import { writeFileSync } from 'fs-extra'; | ||
5 | |||
6 | const debug = require('debug')('Ferdi:ipcApi:download'); | ||
7 | |||
8 | function 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 | |||
18 | export 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 | }; | ||