feat: wire updater IPC into main process and preload
This commit is contained in:
parent
f4172f5c2a
commit
5ec19ef53e
75
main.js
75
main.js
@ -1,9 +1,11 @@
|
|||||||
const { app, BrowserWindow, ipcMain, dialog, clipboard } = require('electron');
|
const { app, BrowserWindow, ipcMain, dialog, clipboard } = require('electron');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
const ConfigStore = require('./lib/config-store');
|
const ConfigStore = require('./lib/config-store');
|
||||||
const UploadManager = require('./lib/upload-manager');
|
const UploadManager = require('./lib/upload-manager');
|
||||||
const { HOSTER_CONFIGS } = require('./lib/hosters');
|
const { HOSTER_CONFIGS } = require('./lib/hosters');
|
||||||
const VidmolyUploader = require('./lib/vidmoly-upload');
|
const VidmolyUploader = require('./lib/vidmoly-upload');
|
||||||
|
const { checkForUpdate, installUpdate, abortUpdate } = require('./lib/updater');
|
||||||
|
|
||||||
let mainWindow;
|
let mainWindow;
|
||||||
const configStore = new ConfigStore(app);
|
const configStore = new ConfigStore(app);
|
||||||
@ -36,6 +38,24 @@ function normalizeApiError(payload, fallback) {
|
|||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getLogFilePath() {
|
||||||
|
// Next to the EXE when packaged, next to project root when dev
|
||||||
|
const baseDir = app.isPackaged
|
||||||
|
? path.dirname(process.execPath)
|
||||||
|
: path.join(__dirname);
|
||||||
|
return path.join(baseDir, 'fileuploader.log');
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendUploadLog(hoster, link, fileName) {
|
||||||
|
try {
|
||||||
|
const now = new Date();
|
||||||
|
const pad = (n) => String(n).padStart(2, '0');
|
||||||
|
const dateStr = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
||||||
|
const line = `${dateStr}|${hoster}|${link}||${fileName}|\n`;
|
||||||
|
fs.appendFileSync(getLogFilePath(), line, 'utf-8');
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
|
||||||
async function checkDoodstreamHealth(hosterConfig) {
|
async function checkDoodstreamHealth(hosterConfig) {
|
||||||
const apiKey = hosterConfig && hosterConfig.apiKey
|
const apiKey = hosterConfig && hosterConfig.apiKey
|
||||||
? String(hosterConfig.apiKey).trim()
|
? String(hosterConfig.apiKey).trim()
|
||||||
@ -188,7 +208,18 @@ function createWindow() {
|
|||||||
mainWindow.loadFile(path.join(__dirname, 'renderer', 'index.html'));
|
mainWindow.loadFile(path.join(__dirname, 'renderer', 'index.html'));
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(createWindow);
|
app.whenReady().then(() => {
|
||||||
|
createWindow();
|
||||||
|
// Auto-check for updates after 3 seconds
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
const result = await checkForUpdate();
|
||||||
|
if (result && result.available && mainWindow && !mainWindow.isDestroyed()) {
|
||||||
|
mainWindow.webContents.send('app:update-available', result);
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
}, 3000);
|
||||||
|
});
|
||||||
|
|
||||||
app.on('window-all-closed', () => {
|
app.on('window-all-closed', () => {
|
||||||
app.quit();
|
app.quit();
|
||||||
@ -261,6 +292,18 @@ ipcMain.handle('start-upload', (_event, payload) => {
|
|||||||
|
|
||||||
uploadManager.on('batch-done', (summary) => {
|
uploadManager.on('batch-done', (summary) => {
|
||||||
configStore.appendHistory(summary);
|
configStore.appendHistory(summary);
|
||||||
|
// Write successful uploads to fileuploader.log
|
||||||
|
for (const file of summary.files || []) {
|
||||||
|
for (const result of file.results || []) {
|
||||||
|
if (result.status === 'done' && (result.download_url || result.embed_url)) {
|
||||||
|
appendUploadLog(
|
||||||
|
result.hoster || '',
|
||||||
|
result.download_url || result.embed_url || '',
|
||||||
|
file.name || ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||||
mainWindow.webContents.send('upload-batch-done', summary);
|
mainWindow.webContents.send('upload-batch-done', summary);
|
||||||
}
|
}
|
||||||
@ -287,3 +330,33 @@ ipcMain.handle('copy-to-clipboard', (_event, text) => {
|
|||||||
clipboard.writeText(text);
|
clipboard.writeText(text);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.handle('app:check-updates', async () => {
|
||||||
|
try {
|
||||||
|
return await checkForUpdate();
|
||||||
|
} catch (err) {
|
||||||
|
return { available: false, error: err.message };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ipcMain.handle('app:install-update', async () => {
|
||||||
|
try {
|
||||||
|
installUpdate((progress) => {
|
||||||
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||||
|
mainWindow.webContents.send('app:update-progress', progress);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return { started: true };
|
||||||
|
} catch (err) {
|
||||||
|
return { error: err.message };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ipcMain.handle('app:abort-update', () => {
|
||||||
|
abortUpdate();
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
ipcMain.handle('app:get-version', () => {
|
||||||
|
return app.getVersion();
|
||||||
|
});
|
||||||
|
|||||||
14
preload.js
14
preload.js
@ -19,6 +19,18 @@ contextBridge.exposeInMainWorld('api', {
|
|||||||
// Clipboard
|
// Clipboard
|
||||||
copyToClipboard: (text) => ipcRenderer.invoke('copy-to-clipboard', text),
|
copyToClipboard: (text) => ipcRenderer.invoke('copy-to-clipboard', text),
|
||||||
|
|
||||||
|
// Updates
|
||||||
|
checkForUpdate: () => ipcRenderer.invoke('app:check-updates'),
|
||||||
|
installUpdate: () => ipcRenderer.invoke('app:install-update'),
|
||||||
|
abortUpdate: () => ipcRenderer.invoke('app:abort-update'),
|
||||||
|
getVersion: () => ipcRenderer.invoke('app:get-version'),
|
||||||
|
onUpdateAvailable: (callback) => {
|
||||||
|
ipcRenderer.on('app:update-available', (_event, data) => callback(data));
|
||||||
|
},
|
||||||
|
onUpdateProgress: (callback) => {
|
||||||
|
ipcRenderer.on('app:update-progress', (_event, data) => callback(data));
|
||||||
|
},
|
||||||
|
|
||||||
// Events (main -> renderer)
|
// Events (main -> renderer)
|
||||||
onUploadProgress: (callback) => {
|
onUploadProgress: (callback) => {
|
||||||
ipcRenderer.on('upload-progress', (_event, data) => callback(data));
|
ipcRenderer.on('upload-progress', (_event, data) => callback(data));
|
||||||
@ -29,5 +41,7 @@ contextBridge.exposeInMainWorld('api', {
|
|||||||
removeAllListeners: () => {
|
removeAllListeners: () => {
|
||||||
ipcRenderer.removeAllListeners('upload-progress');
|
ipcRenderer.removeAllListeners('upload-progress');
|
||||||
ipcRenderer.removeAllListeners('upload-batch-done');
|
ipcRenderer.removeAllListeners('upload-batch-done');
|
||||||
|
ipcRenderer.removeAllListeners('app:update-available');
|
||||||
|
ipcRenderer.removeAllListeners('app:update-progress');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user