From 79b6a8bbb4e8e74e0838737f6364be71ff8abf97 Mon Sep 17 00:00:00 2001 From: MasutaYunikon Date: Tue, 21 Apr 2026 15:58:01 +0200 Subject: [PATCH] fix: ne retire pas les fichiers quand on en ajoute sur le torrent --- server/torrent-clients/qbittorrent.ts | 25 ++++++++++++++++--------- server/torrent-clients/rtorrent.ts | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/server/torrent-clients/qbittorrent.ts b/server/torrent-clients/qbittorrent.ts index f2f20b8..1e14354 100644 --- a/server/torrent-clients/qbittorrent.ts +++ b/server/torrent-clients/qbittorrent.ts @@ -70,17 +70,24 @@ async function qbApplyFilePriority( const files: any[] = await res.json() if (!Array.isArray(files) || files.length === 0) continue - // Désactiver tous les autres fichiers - const unwanted = files.map((_, i) => i).filter(i => i !== fileIndex) - if (unwanted.length > 0) { - const form = new FormData() - form.append('hash', hash) - form.append('id', unwanted.join('|')) - form.append('priority', '0') - await fetch(`${config.url}/api/v2/torrents/filePrio`, { method: 'POST', body: form, headers: { Cookie: `SID=${sid}` } }) + // Si tous les fichiers sont à priorité normale (état initial du torrent), + // on applique le filtrage complet : on désactive tout sauf la cible. + // Si certains sont déjà à 0 (filtrage déjà effectué pour un épisode précédent), + // on se contente d'activer la nouvelle cible sans toucher aux autres. + const isInitialState = files.every((f: any) => (f.priority ?? 1) > 0) + + if (isInitialState) { + const unwanted = files.map((_, i) => i).filter(i => i !== fileIndex) + if (unwanted.length > 0) { + const form = new FormData() + form.append('hash', hash) + form.append('id', unwanted.join('|')) + form.append('priority', '0') + await fetch(`${config.url}/api/v2/torrents/filePrio`, { method: 'POST', body: form, headers: { Cookie: `SID=${sid}` } }) + } } - // Activer le fichier cible + // Activer le fichier cible (dans tous les cas) const form2 = new FormData() form2.append('hash', hash) form2.append('id', String(fileIndex)) diff --git a/server/torrent-clients/rtorrent.ts b/server/torrent-clients/rtorrent.ts index 5341f2a..ccb6a95 100644 --- a/server/torrent-clients/rtorrent.ts +++ b/server/torrent-clients/rtorrent.ts @@ -142,12 +142,22 @@ async function rtApplyFilePriority( for (let attempt = 0; attempt < 100; attempt++) { await new Promise(r => setTimeout(r, 300)) try { - const files: any[] = await rpcCall(config, 'f.multicall', [hash, '', 'f.size_bytes=']) + // Récupérer taille ET priorité courante de chaque fichier + const files: any[] = await rpcCall(config, 'f.multicall', [hash, '', 'f.size_bytes=', 'f.priority=']) const fileCount = Array.isArray(files) ? files.length : 0 if (fileCount === 0) continue + // Si tous les fichiers sont à priorité normale (état initial), + // on filtre tout sauf la cible. Sinon on active juste la cible. + const isInitialState = files.every((f: any) => Number(f[1]) > 0) + for (let i = 0; i < fileCount; i++) { - await rpcCall(config, 'f.priority.set', [hash, i, i === fileIndex ? 1 : 0]) + if (i === fileIndex) { + await rpcCall(config, 'f.priority.set', [hash, i, 1]) + } else if (isInitialState) { + await rpcCall(config, 'f.priority.set', [hash, i, 0]) + } + // Si pas initial state : on ne touche pas aux autres fichiers } logger.info('rtorrent', `Fichier ${fileIndex} sélectionné pour ${hash.slice(0, 8)}…`)