-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-handler.js
More file actions
283 lines (246 loc) · 11.1 KB
/
update-handler.js
File metadata and controls
283 lines (246 loc) · 11.1 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
const axios = require('axios');
const https = require('https');
const fs = require('fs');
const path = require('path');
const os = require('os');
class UpdateHandler {
constructor() {
this.currentVersion = app.getVersion();
this.releasesUrl = 'https://api.github.com/repos/mortezadalil/Pro-Translator/releases';
this.downloadWindow = null;
// Set up IPC handlers for the download window
ipcMain.on('update-progress', (event, data) => {
if (this.downloadWindow && !this.downloadWindow.isDestroyed()) {
this.downloadWindow.webContents.executeJavaScript(`
document.getElementById('progress').style.width = '${data.progress}%';
document.getElementById('status').textContent = '${data.status}';
`);
}
});
ipcMain.on('download-complete', () => {
if (this.downloadWindow && !this.downloadWindow.isDestroyed()) {
this.downloadWindow.webContents.executeJavaScript(`
document.getElementById('status').textContent = 'Download complete!';
`);
}
});
}
async checkForUpdates(showNoUpdateDialog = true) {
try {
console.log('Checking for updates...');
const response = await axios.get(this.releasesUrl, {
timeout: 10000,
headers: {
'User-Agent': 'Pro-Translator-App'
}
});
console.log('Response received:', response.status);
if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
throw new Error('No releases found in repository');
}
const latestRelease = response.data[0];
const latestVersion = latestRelease.tag_name.replace('v', '');
console.log(`Current version: ${this.currentVersion}, Latest version: ${latestVersion}`);
// Use the app's icon for dialogs
const appIcon = path.join(__dirname, 'build', 'icon.png');
if (this.compareVersions(latestVersion, this.currentVersion) > 0) {
const updateDialog = await dialog.showMessageBox({
type: 'info',
title: 'Update Available',
message: `A new version (${latestVersion}) is available. Would you like to download it?`,
buttons: ['Yes', 'No'],
defaultId: 0,
icon: appIcon
});
if (updateDialog.response === 0) {
this.downloadUpdate(latestRelease);
}
} else if (showNoUpdateDialog) {
dialog.showMessageBox({
type: 'info',
title: 'No Updates',
message: 'You are using the latest version.',
buttons: ['OK'],
icon: appIcon
});
}
} catch (error) {
console.error('Update check error:', error);
let errorMessage = 'Failed to check for updates. Please try again later.';
if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
errorMessage = 'No internet connection. Please check your network and try again.';
} else if (error.response?.status === 404) {
errorMessage = 'Repository not found. Please contact the developer.';
} else if (error.response?.status === 403) {
errorMessage = 'Rate limit exceeded. Please try again later.';
} else if (error.message.includes('No releases found')) {
errorMessage = 'No releases available yet. This is the current version.';
}
dialog.showErrorBox('Update Check Failed', errorMessage);
}
}
compareVersions(v1, v2) {
const v1Parts = v1.split('.').map(Number);
const v2Parts = v2.split('.').map(Number);
// Pad the shorter version with zeros
while (v1Parts.length < v2Parts.length) v1Parts.push(0);
while (v2Parts.length < v1Parts.length) v2Parts.push(0);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
if (v1Parts[i] > v2Parts[i]) return 1;
if (v1Parts[i] < v2Parts[i]) return -1;
}
return 0;
}
getDownloadUrl(release) {
const platform = os.platform();
const assets = release.assets;
if (platform === 'darwin') {
return assets.find(asset => asset.name.endsWith('.dmg'))?.browser_download_url;
} else if (platform === 'win32') {
return assets.find(asset => asset.name.endsWith('.exe'))?.browser_download_url;
} else if (platform === 'linux') {
return assets.find(asset => asset.name.endsWith('.deb'))?.browser_download_url;
}
return null;
}
createDownloadWindow() {
this.downloadWindow = new BrowserWindow({
width: 400,
height: 160,
resizable: false,
minimizable: false,
maximizable: false,
fullscreenable: false,
useContentSize: true,
autoHideMenuBar: true,
show: true,
icon: null,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
// Create a temporary HTML file for the download window
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
padding: 20px;
background-color: #f5f5f5;
text-align: center;
user-select: none;
box-sizing: border-box;
}
.progress-container {
width: 100%;
background-color: #e0e0e0;
border-radius: 4px;
margin: 20px 0;
overflow: hidden;
}
.progress-bar {
width: 0%;
height: 20px;
background-color: #4CAF50;
border-radius: 4px;
transition: width 0.3s;
}
.status {
margin-top: 10px;
color: #666;
}
</style>
</head>
<body>
<h3>Downloading New Version</h3>
<div class="progress-container">
<div class="progress-bar" id="progress"></div>
</div>
<div class="status" id="status">Preparing download...</div>
<script>
const { ipcRenderer } = require('electron');
ipcRenderer.on('update-progress', (event, data) => {
document.getElementById('progress').style.width = data.progress + '%';
document.getElementById('status').textContent = data.status;
});
ipcRenderer.on('download-complete', () => {
document.getElementById('status').textContent = 'Download complete!';
});
</script>
</body>
</html>
`;
// Load the HTML content
this.downloadWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(htmlContent)}`);
}
async downloadUpdate(release) {
const downloadUrl = this.getDownloadUrl(release);
if (!downloadUrl) {
dialog.showErrorBox('Download Error', 'Could not find a compatible download for your platform.');
return;
}
this.createDownloadWindow();
const downloadPath = path.join(os.tmpdir(), path.basename(downloadUrl));
const file = fs.createWriteStream(downloadPath);
try {
const response = await axios({
method: 'GET',
url: downloadUrl,
responseType: 'stream'
});
const totalLength = response.headers['content-length'];
let downloaded = 0;
response.data.on('data', (chunk) => {
downloaded += chunk.length;
const progress = Math.round((downloaded / totalLength) * 100);
if (this.downloadWindow && !this.downloadWindow.isDestroyed()) {
this.downloadWindow.webContents.executeJavaScript(`
document.getElementById('progress').style.width = '${progress}%';
document.getElementById('status').textContent = 'Downloading... ${progress}%';
`);
}
});
response.data.pipe(file);
file.on('finish', () => {
file.close();
if (this.downloadWindow && !this.downloadWindow.isDestroyed()) {
this.downloadWindow.webContents.executeJavaScript(`
document.getElementById('status').textContent = 'Download complete! Opening file...';
`);
setTimeout(() => {
this.downloadWindow.close();
// Open the downloaded file
const { shell } = require('electron');
shell.openPath(downloadPath);
}, 2000);
}
});
file.on('error', (err) => {
console.error('Download error:', err);
if (this.downloadWindow && !this.downloadWindow.isDestroyed()) {
this.downloadWindow.close();
}
dialog.showErrorBox('Download Error', 'Failed to download the update file.');
});
} catch (error) {
console.error('Download error:', error);
if (this.downloadWindow && !this.downloadWindow.isDestroyed()) {
this.downloadWindow.close();
}
dialog.showErrorBox('Download Error', 'Failed to download the update file.');
}
}
}
module.exports = new UpdateHandler();