mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-07-29 18:25:47 +02:00
Refactor: split main.js into app/ modules
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
const { autoUpdater } = require("electron-updater");
|
const { autoUpdater } = require("electron-updater");
|
||||||
const { app, Notification, shell} = require("electron");
|
const { app, Notification, shell} = require("electron");
|
||||||
const { logger } = require("./logger");
|
const { logger } = require("../server/logger");
|
||||||
|
|
||||||
function AutoUpdater() {
|
function AutoUpdater() {
|
||||||
|
|
||||||
32
app/dependencyCheck.js
Normal file
32
app/dependencyCheck.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
const fs = require("fs");
|
||||||
|
const { app, dialog } = require("electron");
|
||||||
|
const { logger } = require("../server/logger");
|
||||||
|
|
||||||
|
function checkNativeDependencies() {
|
||||||
|
const { binaryPaths } = require("../server/helpers/path.helpers");
|
||||||
|
|
||||||
|
const deps = [
|
||||||
|
{ name: "yt-dlp.exe", path: binaryPaths.ytDlp },
|
||||||
|
{ name: "ffmpeg.exe", path: binaryPaths.ffmpeg },
|
||||||
|
{ name: "ffprobe.exe", path: binaryPaths.ffprobe },
|
||||||
|
{ name: "deno.exe", path: binaryPaths.deno },
|
||||||
|
];
|
||||||
|
|
||||||
|
const missing = deps.filter(d => !fs.existsSync(d.path));
|
||||||
|
if (missing.length === 0) return true;
|
||||||
|
|
||||||
|
const list = missing.map(d => d.name).join(", ");
|
||||||
|
logger.error(`Missing dependencies: ${list}`);
|
||||||
|
|
||||||
|
app.whenReady().then(() => {
|
||||||
|
dialog.showErrorBox(
|
||||||
|
"Missing dependencies",
|
||||||
|
`The following files are missing in the 'ressources' folder:\n${list}\n\nThe application will now exit. Try to reinstall.`
|
||||||
|
);
|
||||||
|
app.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { checkNativeDependencies };
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
const RPC = require("discord-rpc");
|
const RPC = require("discord-rpc");
|
||||||
const { logger } = require("./logger");
|
const { logger } = require("../server/logger");
|
||||||
|
|
||||||
const clientId = `${config.DiscordRPCID}`;
|
const clientId = `${config.DiscordRPCID}`;
|
||||||
const rpc = new RPC.Client({ transport: "ipc" });
|
const rpc = new RPC.Client({ transport: "ipc" });
|
||||||
111
app/ipcHandlers.js
Normal file
111
app/ipcHandlers.js
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
const { ipcMain, dialog, shell } = require("electron");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
const { logger, logDir } = require("../server/logger");
|
||||||
|
const { configFeatures } = require("../config");
|
||||||
|
const config = require("../config");
|
||||||
|
const { validateDownloadPath, getDefaultDownloadPath } = require("./pathValidator");
|
||||||
|
|
||||||
|
const configFolderPath = path.join(
|
||||||
|
config.localMode
|
||||||
|
|
||||||
|
? path.join(__dirname, "..")
|
||||||
|
: path.join(path.dirname(process.execPath), "resources"), "config", "config.json"
|
||||||
|
);
|
||||||
|
|
||||||
|
const FEATURE_WHITELIST = new Set([
|
||||||
|
"autoUpdate",
|
||||||
|
"discordRPC",
|
||||||
|
"customTopBar",
|
||||||
|
"autoCheckInfo",
|
||||||
|
"addThumbnail",
|
||||||
|
"addMetadata",
|
||||||
|
"verboseLogs",
|
||||||
|
"autoDownloadPlaylist",
|
||||||
|
"customCodec",
|
||||||
|
]);
|
||||||
|
|
||||||
|
function registerIpcHandlers(getMainWindow) {
|
||||||
|
|
||||||
|
// Infos générales
|
||||||
|
ipcMain.handle("version", () => config.version);
|
||||||
|
ipcMain.handle("features", () => configFeatures);
|
||||||
|
|
||||||
|
// Sélection et validation de dossier
|
||||||
|
ipcMain.handle("select-download-folder", async () => {
|
||||||
|
const result = await dialog.showOpenDialog({ properties: ["openDirectory"] });
|
||||||
|
if (result.canceled) {
|
||||||
|
logger.info("Folder selection cancelled by user");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const validated = validateDownloadPath(result.filePaths[0]);
|
||||||
|
logger.info(`Folder selected and validated: ${validated}`);
|
||||||
|
return validated;
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(`Unsafe or invalid folder rejected: ${err.message}`);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ipcMain.handle("validate-download-path", (_, userPath) => validateDownloadPath(userPath));
|
||||||
|
ipcMain.handle("get-default-download-path", () => getDefaultDownloadPath());
|
||||||
|
|
||||||
|
// Progression dans la taskbar
|
||||||
|
ipcMain.on("set-progress", (_, percent) => {
|
||||||
|
getMainWindow()?.setProgressBar(percent / 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Contrôles fenêtre (custom top bar)
|
||||||
|
ipcMain.on("window-minimize", () => getMainWindow()?.minimize());
|
||||||
|
ipcMain.on("window-maximize", () => {
|
||||||
|
const win = getMainWindow();
|
||||||
|
if (!win) return;
|
||||||
|
win.isMaximized() ? win.unmaximize() : win.maximize();
|
||||||
|
});
|
||||||
|
ipcMain.on("window-close", () => getMainWindow()?.close());
|
||||||
|
|
||||||
|
// Actions custom
|
||||||
|
ipcMain.on("open-devtools", () =>
|
||||||
|
getMainWindow()?.webContents.openDevTools({ mode: "detach" })
|
||||||
|
);
|
||||||
|
ipcMain.on("open-logs", () => logDir && shell.openPath(logDir));
|
||||||
|
ipcMain.on("open-website", () =>
|
||||||
|
shell.openExternal("https://masteracnolo.github.io/FreedomLoader/index.html")
|
||||||
|
);
|
||||||
|
ipcMain.on("open-wiki", () =>
|
||||||
|
shell.openExternal("https://masteracnolo.github.io/FreedomLoader/pages/wiki.html")
|
||||||
|
);
|
||||||
|
ipcMain.on("open-config", () => shell.openPath(configFolderPath));
|
||||||
|
|
||||||
|
// Modification des features
|
||||||
|
ipcMain.handle("set-feature", (event, { key, value }) => {
|
||||||
|
try {
|
||||||
|
if (!FEATURE_WHITELIST.has(key)) {
|
||||||
|
logger.warn(`Rejected feature (not whitelisted): ${key}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (configFeatures[key] === value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
configFeatures[key] = value;
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
configFolderPath,
|
||||||
|
JSON.stringify(configFeatures, null, 2),
|
||||||
|
"utf-8"
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.info(`Feature updated: ${key} = ${value}`);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`set-feature failed (${key}): ${err.message}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { registerIpcHandlers };
|
||||||
32
app/pathValidator.js
Normal file
32
app/pathValidator.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
const { logger } = require("../server/logger");
|
||||||
|
|
||||||
|
let _defaultPath = null;
|
||||||
|
|
||||||
|
function getDefaultDownloadPath() {
|
||||||
|
if (!_defaultPath) {
|
||||||
|
const { defaultDownloadFolder } = require("../server/helpers/path.helpers");
|
||||||
|
_defaultPath = defaultDownloadFolder;
|
||||||
|
}
|
||||||
|
return _defaultPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateDownloadPath(userPath) {
|
||||||
|
const { isSafePath } = require("../server/helpers/validation.helpers");
|
||||||
|
|
||||||
|
if (!userPath) return getDefaultDownloadPath();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resolved = fs.realpathSync(path.resolve(userPath));
|
||||||
|
if (!isSafePath(resolved)) {
|
||||||
|
throw new Error("Path not allowed: system folders are blocked!");
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`Invalid download path: ${userPath} — ${err.message}`);
|
||||||
|
throw new Error(`Invalid or inaccessible path: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { validateDownloadPath, getDefaultDownloadPath };
|
||||||
50
app/windowManager.js
Normal file
50
app/windowManager.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
const { BrowserWindow, app } = require("electron");
|
||||||
|
const path = require("path");
|
||||||
|
const { logger } = require("../server/logger");
|
||||||
|
const { configFeatures } = require("../config");
|
||||||
|
const config = require("../config");
|
||||||
|
|
||||||
|
let mainWindow = null;
|
||||||
|
|
||||||
|
async function createMainWindow() {
|
||||||
|
if (mainWindow) {
|
||||||
|
logger.warn("Window already exists, no new creation!");
|
||||||
|
return mainWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
mainWindow = new BrowserWindow({
|
||||||
|
title: `Freedom Loader ${config.version}`,
|
||||||
|
width: 750,
|
||||||
|
height: 800,
|
||||||
|
minWidth: 750,
|
||||||
|
minHeight: 800,
|
||||||
|
frame: !configFeatures.customTopBar,
|
||||||
|
devTools: !app.isPackaged,
|
||||||
|
webPreferences: {
|
||||||
|
nodeIntegration: false,
|
||||||
|
contextIsolation: true,
|
||||||
|
preload: path.join(__dirname, "../preload.js"),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mainWindow.loadURL(`http://localhost:${config.applicationPort}`);
|
||||||
|
logger.info("Window loaded");
|
||||||
|
} catch (err) {
|
||||||
|
logger.error("Window loading error:", err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
mainWindow.on("closed", () => {
|
||||||
|
logger.info("Main window closed");
|
||||||
|
mainWindow = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mainWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMainWindow() {
|
||||||
|
return mainWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { createMainWindow, getMainWindow };
|
||||||
12
app/ytDlpUpdater.js
Normal file
12
app/ytDlpUpdater.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
const { execFile } = require("child_process");
|
||||||
|
const { logger } = require("../server/logger");
|
||||||
|
|
||||||
|
function updateYtDlp(ytDlpPath) {
|
||||||
|
logger.info("yt-dlp update check starting...");
|
||||||
|
execFile(ytDlpPath, ["-U"], (err, stdout) => {
|
||||||
|
if (err) logger.warn("yt-dlp update failed (continuing):", err.message);
|
||||||
|
else logger.info(`yt-dlp update: ${stdout.trim()}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { updateYtDlp };
|
||||||
299
main.js
299
main.js
@@ -1,301 +1,72 @@
|
|||||||
const config = require("./config.js");
|
process.on("uncaughtException", (err) => {
|
||||||
const { app, BrowserWindow, ipcMain, dialog, Menu, shell } = require("electron");
|
console.error("Uncaught exception:", err);
|
||||||
const path = require("path");
|
app.quit();
|
||||||
const fs = require("fs");
|
});
|
||||||
|
|
||||||
const { logger, logSessionStart, logSessionEnd, logDir } = require("./server/logger");
|
process.on("unhandledRejection", (reason) => {
|
||||||
const { AutoUpdater } = require("./server/update.js");
|
console.error("Unhandled rejection:", reason);
|
||||||
const { configFeatures } = require("./config.js");
|
app.quit();
|
||||||
const { startRPC, stopRPC} = require("./server/discordRPC");
|
});
|
||||||
|
|
||||||
let mainWindow;
|
const { app } = require("electron");
|
||||||
const logsFolderPath = logDir;
|
|
||||||
|
|
||||||
const basePath = config.localMode
|
const { logger, logSessionStart, logSessionEnd } = require("./server/logger");
|
||||||
? path.join(__dirname )
|
const { AutoUpdater } = require("./app/autoUpdater");
|
||||||
: path.join(path.dirname(process.execPath), "resources");
|
const { startRPC, stopRPC } = require("./app/discordRPC");
|
||||||
|
|
||||||
const configFolderPath = path.join(basePath, "config" ,"config.json");
|
const { configFeatures } = require("./config");
|
||||||
|
const config = require("./config");
|
||||||
|
|
||||||
// Default download path (centralized, lazy loaded)
|
const { checkNativeDependencies } = require("./app/dependencyCheck");
|
||||||
let defaultDownloadPath;
|
const { updateYtDlp } = require("./app/ytDlpUpdater");
|
||||||
|
const { createMainWindow, getMainWindow } = require("./app/windowManager");
|
||||||
|
const { registerIpcHandlers } = require("./app/ipcHandlers");
|
||||||
|
|
||||||
app.setAppUserModelId("com.masteracnolo.freedomloader"); // pour notifications Windows
|
app.setAppUserModelId("com.masteracnolo.freedomloader");
|
||||||
app.disableHardwareAcceleration();
|
app.disableHardwareAcceleration();
|
||||||
|
|
||||||
ipcMain.handle("version", () => config.version);
|
if (!config.localMode) {
|
||||||
|
const gotLock = app.requestSingleInstanceLock();
|
||||||
// Gestion single instance
|
if (gotLock) {
|
||||||
const gotLock = app.requestSingleInstanceLock();
|
|
||||||
|
|
||||||
// Native dependencies check (yt-dlp.exe, ffmpeg.exe, ffprobe.exe, Deno)
|
|
||||||
function checkNativeDependencies() {
|
|
||||||
// Import centralized paths after app initialization
|
|
||||||
const { binaryPaths } = require("./server/helpers/path.helpers.js");
|
|
||||||
|
|
||||||
const deps = [
|
|
||||||
{ name: "yt-dlp.exe", path: binaryPaths.ytDlp },
|
|
||||||
{ name: "ffmpeg.exe", path: binaryPaths.ffmpeg },
|
|
||||||
{ name: "ffprobe.exe", path: binaryPaths.ffprobe },
|
|
||||||
{ name: "deno.exe", path: binaryPaths.deno },
|
|
||||||
];
|
|
||||||
const missing = deps.filter(dep => !fs.existsSync(dep.path));
|
|
||||||
let errorMsg = "";
|
|
||||||
if (missing.length > 0) {
|
|
||||||
const missingList = missing.map(dep => dep.name).join(", ");
|
|
||||||
logger.error(`Missing dependencies: ${missingList}`);
|
|
||||||
errorMsg += `The following files are missing in the 'ressources' folder:\n${missingList}`;
|
|
||||||
}
|
|
||||||
if (errorMsg) {
|
|
||||||
app.whenReady().then(() => {
|
|
||||||
dialog.showErrorBox(
|
|
||||||
"Missing dependencies",
|
|
||||||
`${errorMsg}\n\nThe application will now exit. Try to reinstall`
|
|
||||||
);
|
|
||||||
app.quit();
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!config.localMode){
|
|
||||||
if (!gotLock) {
|
|
||||||
// Une instance existe déjà -> fermer l'ancienne et continuer la nouvelle
|
|
||||||
// Ici la nouvelle instance continue normalement
|
|
||||||
} else {
|
|
||||||
if (!checkNativeDependencies()) {
|
|
||||||
// Arrêt déjà géré dans la fonction
|
|
||||||
} else {
|
|
||||||
app.on("second-instance", () => {
|
app.on("second-instance", () => {
|
||||||
// La vieille instance se ferme
|
logger.info("New instance detected, closing older...");
|
||||||
if (mainWindow) {
|
getMainWindow()?.destroy();
|
||||||
logger.info("New Instance Detected, closing the older...");
|
|
||||||
mainWindow.destroy();
|
|
||||||
mainWindow = null;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
// Création fenêtre principale
|
|
||||||
async function createMainWindow() {
|
|
||||||
if (mainWindow) {
|
|
||||||
logger.warn("Window already exists, no new creation!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mainWindow = new BrowserWindow({
|
|
||||||
title: `Freedom Loader ${config.version}`,
|
|
||||||
width: 750,
|
|
||||||
height: 800,
|
|
||||||
minWidth: 750,
|
|
||||||
minHeight: 800,
|
|
||||||
frame: !configFeatures.customTopBar,
|
|
||||||
devTools: `${app.isPackaged ? false : true}`,
|
|
||||||
webPreferences: {
|
|
||||||
nodeIntegration: false,
|
|
||||||
contextIsolation: true,
|
|
||||||
preload: path.join(__dirname, "preload.js"),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
|
||||||
await mainWindow.loadURL(`http://localhost:${config.applicationPort}`);
|
|
||||||
logger.info("Window Loaded");
|
|
||||||
} catch (err) {
|
|
||||||
logger.error("Window Loading Error:", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
mainWindow.on("closed", () => {
|
|
||||||
logger.info("Main Window Closed");
|
|
||||||
mainWindow = null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateDownloadPath(userPath) {
|
|
||||||
const { isSafePath } = require("./server/helpers/validation.helpers.js");
|
|
||||||
|
|
||||||
// Lazy load default path
|
|
||||||
if (!defaultDownloadPath) {
|
|
||||||
const { defaultDownloadFolder } = require("./server/helpers/path.helpers.js");
|
|
||||||
defaultDownloadPath = defaultDownloadFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!userPath) return defaultDownloadPath;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Canonical resolution and symlink following
|
|
||||||
const resolved = fs.realpathSync(path.resolve(userPath));
|
|
||||||
|
|
||||||
// Use the same validation as backend (allows all drives except system folders)
|
|
||||||
if (!isSafePath(resolved)) {
|
|
||||||
throw new Error("Path not allowed: system folders are blocked!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolved;
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(`Invalid download path: ${userPath} - ${err.message}`);
|
|
||||||
throw new Error(`Invalid or inaccessible path: ${err.message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// IPC
|
|
||||||
ipcMain.handle("select-download-folder", async () => {
|
|
||||||
const result = await dialog.showOpenDialog({ properties: ["openDirectory"] });
|
|
||||||
if (result.canceled) {
|
|
||||||
logger.info("Folder selection cancelled by user");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (result.filePaths.length > 0) {
|
|
||||||
const selectedPath = result.filePaths[0];
|
|
||||||
try {
|
|
||||||
const validatedPath = validateDownloadPath(selectedPath);
|
|
||||||
logger.info(`Folder selected and validated: ${validatedPath}`);
|
|
||||||
return validatedPath;
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn(`Unsafe or invalid folder rejected: ${err.message}`);
|
|
||||||
throw err; // Propagate error to UI
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
ipcMain.handle("validate-download-path", (event, userPath) => {
|
|
||||||
return validateDownloadPath(userPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
ipcMain.handle("get-default-download-path", () => {
|
|
||||||
if (!defaultDownloadPath) {
|
|
||||||
const { defaultDownloadFolder } = require("./server/helpers/path");
|
|
||||||
defaultDownloadPath = defaultDownloadFolder;
|
|
||||||
}
|
|
||||||
return defaultDownloadPath;
|
|
||||||
});
|
|
||||||
|
|
||||||
ipcMain.on("set-progress", (event, percent) => {
|
|
||||||
if (mainWindow) mainWindow.setProgressBar(percent / 100); // Electron attend 0 → 1
|
|
||||||
});
|
|
||||||
|
|
||||||
// Topbar window controls
|
|
||||||
ipcMain.on("window-minimize", () => {
|
|
||||||
if (mainWindow) mainWindow.minimize();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Toggle Maximize -> UnMaximize
|
|
||||||
ipcMain.on("window-maximize", () => {
|
|
||||||
if (mainWindow) {
|
|
||||||
if (mainWindow.isMaximized()) {
|
|
||||||
mainWindow.unmaximize();
|
|
||||||
} else {
|
|
||||||
mainWindow.maximize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ipcMain.on("window-close", () => {
|
|
||||||
if (mainWindow) mainWindow.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Topbar custom actions
|
|
||||||
ipcMain.on("open-devtools", () => {
|
|
||||||
if (mainWindow) mainWindow.webContents.openDevTools({ mode: 'detach' });
|
|
||||||
});
|
|
||||||
ipcMain.on("open-logs", () => {
|
|
||||||
if (logsFolderPath) shell.openPath(logsFolderPath);
|
|
||||||
});
|
|
||||||
ipcMain.on("open-website", () => {
|
|
||||||
shell.openExternal("https://masteracnolo.github.io/FreedomLoader/index.html");
|
|
||||||
});
|
|
||||||
ipcMain.on("open-wiki", () => {
|
|
||||||
shell.openExternal("https://masteracnolo.github.io/FreedomLoader/pages/wiki.html");
|
|
||||||
});
|
|
||||||
ipcMain.on("open-config", () => {
|
|
||||||
if (configFolderPath) shell.openPath(configFolderPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// App ready
|
|
||||||
app.whenReady().then(async () => {
|
app.whenReady().then(async () => {
|
||||||
logSessionStart();
|
logSessionStart();
|
||||||
logger.info("App Ready, Server Express starting...");
|
|
||||||
|
|
||||||
const serverPath = path.join(__dirname, "server", "server.js")
|
if (!config.localMode && !checkNativeDependencies()) return;
|
||||||
|
|
||||||
const expressServer = require(serverPath);
|
const { userYtDlp } = require("./server/helpers/path.helpers");
|
||||||
|
updateYtDlp(userYtDlp);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await expressServer.startServer();
|
await require("./server/server").startServer();
|
||||||
logger.info("Express Server Started");
|
|
||||||
|
|
||||||
ipcMain.handle("features", () => {
|
registerIpcHandlers(getMainWindow);
|
||||||
return configFeatures;
|
|
||||||
});
|
|
||||||
|
|
||||||
const featureWhitelist = new Set([
|
|
||||||
"autoUpdate",
|
|
||||||
"discordRPC",
|
|
||||||
"customTopBar",
|
|
||||||
"autoCheckInfo",
|
|
||||||
"addThumbnail",
|
|
||||||
"addMetadata",
|
|
||||||
"verboseLogs",
|
|
||||||
"autoDownloadPlaylist",
|
|
||||||
"customCodec"
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
|
||||||
ipcMain.handle("set-feature", (event, { key, value }) => {
|
|
||||||
try {
|
|
||||||
if (!featureWhitelist.has(key)) {
|
|
||||||
logger.warn(`Rejected feature (not whitelisted): ${key}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// optionnel mais propre
|
|
||||||
if (configFeatures[key] === value) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
configFeatures[key] = value;
|
|
||||||
|
|
||||||
fs.writeFileSync(
|
|
||||||
configFolderPath,
|
|
||||||
JSON.stringify(configFeatures, null, 2),
|
|
||||||
"utf-8"
|
|
||||||
);
|
|
||||||
|
|
||||||
logger.info(`Feature updated: ${key} = ${value}`);
|
|
||||||
return true;
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(`set-feature failed (${key}): ${err.message}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
if (configFeatures.discordRPC) startRPC(); // Discord RPC
|
|
||||||
|
|
||||||
await createMainWindow();
|
await createMainWindow();
|
||||||
|
|
||||||
if (configFeatures.autoUpdate) AutoUpdater(mainWindow); // Auto Update
|
if (configFeatures.discordRPC) startRPC();
|
||||||
|
|
||||||
|
if (configFeatures.autoUpdate) AutoUpdater(getMainWindow());
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error("Window or Server error :", err);
|
logger.error("Boot error:", err);
|
||||||
app.quit();
|
app.quit();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.on("window-all-closed", () => {
|
app.on("window-all-closed", () => {
|
||||||
logger.info("Shuting Down App...");
|
logger.info("Shutting down...");
|
||||||
app.quit();
|
app.quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.on("before-quit", async () => {
|
app.on("before-quit", async () => {
|
||||||
await stopRPC();
|
await stopRPC();
|
||||||
logger.info("All Services Stopped. Have a nice day!")
|
logger.info("All services stopped. Have a nice day!");
|
||||||
logSessionEnd();
|
logSessionEnd();
|
||||||
});
|
});
|
||||||
@@ -2,30 +2,19 @@ const express = require("express");
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const { logger, logSessionEnd } = require("./logger");
|
const { logger, logSessionEnd } = require("./logger");
|
||||||
const config = require("../config");
|
const config = require("../config");
|
||||||
const { execFile } = require("child_process");
|
const { rateLimite } = require("./helpers/rateLimit.helpers");
|
||||||
const { userYtDlp } = require("./helpers/path.helpers");
|
|
||||||
const { rateLimite } = require("./helpers/rateLimit.helpers")
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(express.json());
|
|
||||||
|
|
||||||
// Update YT-DLP on Startup
|
|
||||||
execFile(userYtDlp, ["-U"], (err, stdout, stderr) => {
|
|
||||||
if (err) logger.warn("yt-dlp update error:", err);
|
|
||||||
else logger.info(`Update yt-dlp : ${stdout}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Middlewares
|
// Middlewares
|
||||||
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
app.use(express.static(path.join(__dirname, "../public")));
|
app.use(express.static(path.join(__dirname, "../public")));
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
app.use("/download", require("./routes/download.route"));
|
app.use("/download", require("./routes/download.route"));
|
||||||
app.use("/info", require("./routes/info.route"));
|
app.use("/info", require("./routes/info.route"));
|
||||||
|
app.get("/", rateLimite, (req, res) => {
|
||||||
// When we get API Root, it return the front pages.
|
|
||||||
app.get("/", rateLimite ,(req, res) => {
|
|
||||||
res.sendFile(path.join(__dirname, "../public/index.html"));
|
res.sendFile(path.join(__dirname, "../public/index.html"));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -37,14 +26,14 @@ async function startServer() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.on("error", (err) => {
|
server.on("error", (err) => {
|
||||||
logger.error("Express Server Error :", err);
|
logger.error("Express server error:", err);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
const gracefulExit = () => {
|
const gracefulExit = () => {
|
||||||
logSessionEnd();
|
logSessionEnd();
|
||||||
server.close(() => {
|
server.close(() => {
|
||||||
logger.info("Express Server close cleanly.");
|
logger.info("Express server closed cleanly.");
|
||||||
process.exit();
|
process.exit();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user