-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (112 loc) · 3.58 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const { app, BrowserWindow, ipcMain } = require('electron');
const express = require('express');
const fs = require('fs');
const path = require('path');
const eapp = express();
const archiver = require('archiver');
const { spawn } = require('child_process');
const axios = require('axios')
eapp.use(express.json());
eapp.get('/files', async (req, res) => {
const uploadFilePath = path.join(__dirname, 'upload')
let jsonData = [];
fs.readdir(uploadFilePath, (err, data) => {
if (err) {
throw err
} else {
data.forEach(file => {
jsonData.push({ name: file })
});
res.send(jsonData)
}
})
});
eapp.get('/download/:foldername', async (req, res) => {
const foldername = req.params.foldername;
const folderPath = path.join(__dirname, 'upload', foldername);
const zipFileName = `${foldername}.zip`;
const zipFilePath = path.join(__dirname, 'upload', zipFileName);
const output = fs.createWriteStream(zipFilePath);
const archive = archiver('zip', { zlib: { level: 9 } });
archive.pipe(output);
archive.directory(folderPath, false);
archive.finalize();
await new Promise((resolve) => {
output.on('close', () => {
resolve();
});
});
res.download(zipFilePath, (err) => {
if (err) {
console.error('Dosya indirme hatası:', err);
res.status(500).send('Dosya indirme hatası');
}
fs.unlinkSync(zipFilePath);
});
});
const port = 3000;
eapp.listen(port, () => {
console.log('Sunucu http://localhost:' + port + ' adresinde çalışıyor.');
});
function createWindow() {
const win = new BrowserWindow({
minWidth: 800,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
devTools: true,
nodeIntegration: true
},
icon: './src/assets/lght.ico'
});
win.loadFile('./windows/index.html');
win.removeMenu();
win.webContents.openDevTools();
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.handle('fetchData', async (event, data) => {
let fdata = await fetch(data);
let parsed = await fdata.json();
return parsed
})
function extractProgress(data) {
const progressMatch = data.toString().match(/\b(\d{1,3})%\b/);
if (progressMatch && progressMatch.length > 1) {
const progress = parseInt(progressMatch[1], 10);
console.log(`Download Progress: ${progress}%`);
}
}
ipcMain.handle('downloadFile', async (event, data) => {
axios({
method: 'get',
url: data.url,
responseType: 'stream',
}).then(response => {
const filepath = path.join(__dirname, `download`, `${data.fileName}.zip`);
const outputStream = fs.createWriteStream(filepath);
response.data.pipe(outputStream);
outputStream.on('finish', () => {
event.sender.send('file-downloaded', data.fileName);
});
outputStream.on('error', (err) => {
console.error('Dosya indirme hatası:', err);
event.sender.send('dosya-indirme-hatasi', 'Dosya indirme hatası');
});
}).catch(error => {
console.error('İstek hatası:', error);
event.sender.send('istek-hatasi', 'İstek hatası');
});
})