-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (48 loc) · 1.91 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { app, BrowserWindow, ipcMain, dialog, Notification } = require('electron');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
win.loadFile('index.html');
ipcMain.handle('download-file', async (event, { url, filePath }) => {
try {
const response = await axios({
method: 'get',
url: url,
responseType: 'stream'
});
return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
let totalLength = parseInt(response.headers['content-length'], 10);
let downloadedLength = 0;
response.data.on('data', (chunk) => {
downloadedLength += chunk.length;
const progress = Math.round((downloadedLength / totalLength) * 100);
event.sender.send('download-progress', progress);
});
writer.on('finish', () => resolve({ success: true }));
writer.on('error', (error) => reject({ success: false, error: error.message }));
});
} catch (error) {
console.error('Download error:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('show-save-dialog', async (event, options) => {
return await dialog.showSaveDialog(win, options);
});
ipcMain.on('show-notification', (event, { title, body }) => {
new Notification({ title, body }).show();
});
}
app.whenReady().then(createWindow);