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:
47
app/autoUpdater.js
Normal file
47
app/autoUpdater.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const { autoUpdater } = require("electron-updater");
|
||||
const { app, Notification, shell} = require("electron");
|
||||
const { logger } = require("../server/logger");
|
||||
|
||||
function AutoUpdater() {
|
||||
|
||||
autoUpdater.on("update-available", (info) => {
|
||||
logger.info(`New Version Available : ${info.version}`);
|
||||
new Notification({
|
||||
title: "Freedom Loader",
|
||||
body: `New Version Available : ${info.version}. Application will restart`
|
||||
}).show();
|
||||
});
|
||||
|
||||
autoUpdater.on("update-downloaded", (info) => {
|
||||
logger.info(`Update Downloaded : ${info.version}`);
|
||||
new Notification({
|
||||
title: "Freedom Loader",
|
||||
body: `Update ${info.version} downloaded.`
|
||||
}).on("click", () =>
|
||||
shell.openExternal("https://github.com/MasterAcnolo/Freedom-Loader/releases/latest")
|
||||
).show();
|
||||
|
||||
setTimeout(() => {
|
||||
autoUpdater.quitAndInstall();
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
autoUpdater.on("error", (err) => {
|
||||
logger.error("Auto Update Error :", err.message);
|
||||
new Notification({
|
||||
title: "Freedom Loader - Update Error",
|
||||
body: err.message
|
||||
}).show();
|
||||
});
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
try {
|
||||
await autoUpdater.checkForUpdates();
|
||||
logger.info("Update check completed");
|
||||
} catch (err) {
|
||||
logger.error("Error during update check :", err.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { 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 };
|
||||
47
app/discordRPC.js
Normal file
47
app/discordRPC.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const config = require('../config');
|
||||
const RPC = require("discord-rpc");
|
||||
const { logger } = require("../server/logger");
|
||||
|
||||
const clientId = `${config.DiscordRPCID}`;
|
||||
const rpc = new RPC.Client({ transport: "ipc" });
|
||||
|
||||
let intervalId;
|
||||
|
||||
function startRPC() {
|
||||
rpc.on("ready", () => {
|
||||
const presence = {
|
||||
largeImageKey: "icon",
|
||||
smallImageKey: "acnolo_pfp",
|
||||
smallImageText: "By MasterAcnolo",
|
||||
startTimestamp: new Date(),
|
||||
details: `Open Source Download Tools - ${config.version}`,
|
||||
state: "masteracnolo.github.io/FreedomLoader",
|
||||
};
|
||||
rpc.clearActivity()
|
||||
rpc.setActivity(presence);
|
||||
|
||||
});
|
||||
|
||||
rpc.login({ clientId }).catch(err => {
|
||||
logger.error("Unable to connect to the RPC:", err);
|
||||
});
|
||||
}
|
||||
|
||||
async function stopRPC(){
|
||||
try {
|
||||
if (intervalId) clearInterval(intervalId);
|
||||
|
||||
if (rpc && rpc.transport) {
|
||||
await rpc.clearActivity()
|
||||
await rpc.destroy();
|
||||
} else{
|
||||
logger.error("Not Able to close RPC")
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
logger.error("Error while closing the RPC:", err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = { startRPC, stopRPC};
|
||||
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 };
|
||||
Reference in New Issue
Block a user