mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-07-29 18:25:47 +02:00
Feat: Add createPlaylistFolders feature to organize downloads into dedicated folders with toggle settings
This commit is contained in:
@@ -6,13 +6,80 @@ 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");
|
||||
|
||||
let currentDownloadProcess = null;
|
||||
|
||||
// Détecte si une URL est une playlist
|
||||
function isPlaylistUrl(url) {
|
||||
return url.includes("?list=") || url.includes("&list=");
|
||||
}
|
||||
|
||||
function createPlaylistFolder(basePath, playlistTitle) {
|
||||
const sanitizedTitle = playlistTitle
|
||||
.replace(/[<>:"|?*]/g, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim()
|
||||
.substring(0, 200);
|
||||
|
||||
const playlistPath = path.join(basePath, sanitizedTitle);
|
||||
|
||||
try {
|
||||
if (!fs.existsSync(playlistPath)) {
|
||||
fs.mkdirSync(playlistPath, { recursive: true });
|
||||
logger.info(`Playlist folder created: ${playlistPath}`);
|
||||
return playlistPath;
|
||||
}
|
||||
|
||||
let counter = 1;
|
||||
let newPath;
|
||||
|
||||
while (counter <= 1000) {
|
||||
newPath = path.join(basePath, `${sanitizedTitle} ${counter}`);
|
||||
if (!fs.existsSync(newPath)) {
|
||||
fs.mkdirSync(newPath, { recursive: true });
|
||||
logger.info(`Playlist folder created with increment: ${newPath}`);
|
||||
return newPath;
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
logger.error(`Could not find available playlist folder after 1000 attempts`);
|
||||
throw new Error("Unable to create playlist folder");
|
||||
|
||||
} catch (err) {
|
||||
logger.warn(`Failed to create playlist folder with title "${sanitizedTitle}": ${err.message}`);
|
||||
|
||||
// Fallback: create folder "Untitled Playlist X"
|
||||
let counter = 1;
|
||||
let fallbackPath;
|
||||
|
||||
while (counter <= 1000) {
|
||||
fallbackPath = path.join(basePath, `Untitled Playlist ${counter}`);
|
||||
if (!fs.existsSync(fallbackPath)) {
|
||||
try {
|
||||
fs.mkdirSync(fallbackPath, { recursive: true });
|
||||
logger.info(`Fallback playlist folder created: ${fallbackPath}`);
|
||||
return fallbackPath;
|
||||
} catch (mkErr) {
|
||||
counter++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
logger.error(`Could not create playlist folder after 1000 attempts`);
|
||||
throw new Error("Unable to create playlist folder");
|
||||
}
|
||||
}
|
||||
|
||||
function fetchDownload(options, listeners, speedListeners, stageListeners) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const outputFolder = options.outputFolder || defaultDownloadFolder;
|
||||
logger.info(`CONFIG createPlaylistFolders: ${configFeatures.createPlaylistFolders}`);
|
||||
|
||||
let outputFolder = options.outputFolder || defaultDownloadFolder;
|
||||
|
||||
// Normalize path and validate it's safe (within Users folder)
|
||||
let safeOutputFolder = path.resolve(outputFolder);
|
||||
@@ -31,6 +98,22 @@ function fetchDownload(options, listeners, speedListeners, stageListeners) {
|
||||
return reject(new Error(`Unable to create download folder: ${err.message}`));
|
||||
}
|
||||
|
||||
// Détecte si c'est une playlist et crée un dossier approprié
|
||||
const isPlaylist = options.playlistTitle || isPlaylistUrl(options.url);
|
||||
|
||||
if (isPlaylist && configFeatures.createPlaylistFolders) {
|
||||
try {
|
||||
const playlistName = options.playlistTitle || "Untitled Playlist";
|
||||
safeOutputFolder = createPlaylistFolder(safeOutputFolder, playlistName);
|
||||
logger.info(`Playlist detected, using folder: ${safeOutputFolder}`);
|
||||
} catch (err) {
|
||||
logger.error(`Failed to create playlist folder: ${err.message}`);
|
||||
return reject(err);
|
||||
}
|
||||
} else if (isPlaylist && !configFeatures.createPlaylistFolders) {
|
||||
logger.info(`Playlist detected but createPlaylistFolders is disabled, using base folder`);
|
||||
}
|
||||
|
||||
const args = buildYtDlpArgs({ ...options, outputFolder: safeOutputFolder });
|
||||
logger.info(`[yt-dlp args] ${args.join(" ")}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user