-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathnftDownloadHandlers.js
38 lines (33 loc) · 1.36 KB
/
nftDownloadHandlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { getOrInitWindow } from '../main'
const { app, ipcMain } = require('electron')
const { download } = require('electron-dl')
const fs = require('fs')
const downloadItems = {}
export function initNftDownloadHandlers() {
ipcMain.removeHandler('nft-download')
ipcMain.handle('nft-download', async (event, url, destination, nftId, walletId) => {
const userPath = app.getPath('userData')
const directory = app.isPackaged ? userPath : __dirname
await download(getOrInitWindow('main'), url, {
directory: directory + '/__storage__/' + destination,
filename: 'original',
saveAs: false,
showBadge: true,
showProgressBar: true,
onCompleted: () => {
delete downloadItems[nftId]
getOrInitWindow('main').webContents.send('nft-download-done', { nftId, walletId })
},
onCancel: () => {
delete downloadItems[nftId]
getOrInitWindow('main').webContents.send('nft-download-interrupted', { nftId, walletId })
},
onStarted: (item) => (downloadItems[nftId] = item),
})
})
ipcMain.removeHandler('cancel-nft-download')
ipcMain.handle('cancel-nft-download', async (event, nftId) => {
const downloadItem = downloadItems[nftId]
downloadItem?.cancel()
})
}