diff --git a/config.js b/config.js index 4134004..15f0b78 100644 --- a/config.js +++ b/config.js @@ -68,13 +68,30 @@ function loadFeatures() { return JSON.parse(raw); } +/** + * Reloads feature flags from disk. + * Use this instead of the cached configFeatures snapshot + * when you need the latest user settings at call time. + * + * @returns {Object} Fresh feature flags from config file + */ +function reloadFeatures() { + try { + const raw = fs.readFileSync(featuresPath, "utf-8"); + return JSON.parse(raw); + } catch (err) { + // Fallback to cached snapshot if file read fails + return configFeatures; + } +} + /** * In-memory snapshot of application feature flags. * * Note: Changes to the config file are not automatically reflected. * A restart or reload is required. */ -const configFeatures = loadFeatures(); +let configFeatures = loadFeatures(); module.exports = { /** @@ -102,6 +119,11 @@ module.exports = { */ configFeatures, + /** + * Function to reload the config + */ + reloadFeatures, + /** * Path to the active configuration file */ diff --git a/server/services/download.services.js b/server/services/download.services.js index 05fb0b1..49dfe7a 100644 --- a/server/services/download.services.js +++ b/server/services/download.services.js @@ -6,7 +6,7 @@ const { buildYtDlpArgs } = require("../helpers/buildArgs.helpers"); const notify = require("../helpers/notify.helpers"); const path = require("path"); const { isSafePath } = require("../helpers/validation.helpers"); -const { configFeatures } = require("../../config.js"); +const {reloadFeatures} = require("../../config"); /** * Reference to the currently running yt-dlp process. @@ -135,7 +135,10 @@ function createPlaylistFolder(basePath, playlistTitle) { function fetchDownload(options, listeners, speedListeners, stageListeners, playlistInfoListeners) { return new Promise((resolve, reject) => { - logger.info(`CONFIG createPlaylistFolders: ${configFeatures.createPlaylistFolders}`); + + const userConfig = reloadFeatures(); + + logger.info(`CONFIG createPlaylistFolders: ${userConfig.createPlaylistFolders}`); let outputFolder = options.outputFolder || defaultDownloadFolder; @@ -158,8 +161,8 @@ function fetchDownload(options, listeners, speedListeners, stageListeners, playl // Détecte si c'est une playlist et crée un dossier approprié const isPlaylist = options.playlistTitle || isPlaylistUrl(options.url); - - if (isPlaylist && configFeatures.createPlaylistFolders) { + + if (isPlaylist && userConfig.createPlaylistFolders) { try { const playlistName = options.playlistTitle || "Untitled Playlist"; safeOutputFolder = createPlaylistFolder(safeOutputFolder, playlistName); @@ -168,7 +171,7 @@ function fetchDownload(options, listeners, speedListeners, stageListeners, playl logger.error(`Failed to create playlist folder: ${err.message}`); return reject(err); } - } else if (isPlaylist && !configFeatures.createPlaylistFolders) { + } else if (isPlaylist && !userConfig.createPlaylistFolders) { logger.info(`Playlist detected but createPlaylistFolders is disabled, using base folder`); }