diff --git a/src/main/download-manager.ts b/src/main/download-manager.ts index bc67e09..af1d03f 100644 --- a/src/main/download-manager.ts +++ b/src/main/download-manager.ts @@ -134,6 +134,39 @@ const LARGE_BINARY_FILE_RE = /\.(?:part\d+\.rar|rar|r\d{2,3}|zip(?:\.\d+)?|7z(?: /** Files that are legitimately tiny (< 5 KB) and should NOT be rejected as suspicious. */ const KNOWN_SMALL_FILE_RE = /\.(?:sfv|nfo|nzb|md5|sha1|sha256|crc|txt|url|lnk|srr)$/i; +/** Folder name patterns indicating bonus/extras content that should NOT be moved + * to the flat MKV library or auto-renamed. Matches as a substring of the folder name. */ +const BONUS_DIR_PATTERNS = [ + "extras", "extra", "bonus", "featurettes", "featurette", "specials", "special features", + "behind the scenes", "behindthescenes", "deleted scenes", "deletedscenes", + "making of", "makingof", "outtakes", "trailers", "interviews", "documentaries" +]; + +/** Filename token patterns for bonus content (e.g. "making-of-e02.mkv"). */ +const BONUS_FILENAME_RE = /(?:^|[._\-\s])(?:making[._\-\s]?of|behind[._\-\s]?the[._\-\s]?scenes|deleted[._\-\s]?scene|alternate[._\-\s]?ending|gag[._\-\s]?reel|featurette|outtakes?|bloopers?|interview|extended[._\-\s]?scene|exclusive[._\-\s]?scene)(?:[._\-\s]|$)/i; + +/** Detect if a file path lies inside a bonus/extras subdirectory of the package. + * Walks up the path from filePath until packageDir and checks each segment. */ +function isInsideBonusDir(filePath: string, packageDir: string): boolean { + if (!filePath || !packageDir) return false; + let current = path.dirname(filePath); + const root = path.resolve(packageDir); + let safety = 0; + while (current && safety++ < 32) { + const resolvedCurrent = path.resolve(current); + if (resolvedCurrent === root) return false; + if (!isPathInsideDir(current, packageDir)) return false; + const segment = path.basename(current).toLowerCase(); + for (const pattern of BONUS_DIR_PATTERNS) { + if (segment.includes(pattern)) return true; + } + const parent = path.dirname(current); + if (!parent || parent === current) break; + current = parent; + } + return false; +} + function expectedMinBytes(totalBytes: number | null | undefined, strict: boolean): number { if (!totalBytes || totalBytes <= 0) { return 10240; @@ -3408,6 +3441,12 @@ export class DownloadManager extends EventEmitter { if (sampleTokenRe.test(sourceBaseName) || sampleDirNames.has(parentDirName) || sampleSuffixRe.test(sourceBaseName)) { continue; } + // Skip bonus/extras content (Featurettes, Making-Of, Behind-The-Scenes, etc.) + // These have generic descriptive names and would get renamed to misleading + // episode names if matched against the package's SxxExx pattern. + if (isInsideBonusDir(sourcePath, extractDir) || BONUS_FILENAME_RE.test(sourceBaseName)) { + continue; + } const folderCandidates: string[] = []; let currentDir = path.dirname(sourcePath); while (currentDir && isPathInsideDir(currentDir, extractDir)) { @@ -3846,11 +3885,15 @@ export class DownloadManager extends EventEmitter { return; } - // Filter: Sample-Dateien ausschließen (Sample-Ordner + "sample" im Dateinamen) + // Filter: Sample- und Bonus-Dateien ausschließen + // - Sample-Ordner / "sample" im Dateinamen + // - Bonus-Subordner (Extras, Bonus, Featurettes, etc.) + // - Bonus-Dateinamen (Making-Of, Deleted-Scene, etc.) const sampleDirNames = new Set(["sample", "samples"]); const sampleTokenRe = /(^|[._\-\s])sample([._\-\s]|$)/i; const mkvFiles: string[] = []; let sampleSkipped = 0; + let bonusSkipped = 0; for (const filePath of allMkvFiles) { if (shouldAbort?.()) { return; @@ -3861,13 +3904,21 @@ export class DownloadManager extends EventEmitter { sampleSkipped += 1; continue; } + if (isInsideBonusDir(filePath, sourceDir) || BONUS_FILENAME_RE.test(stem)) { + bonusSkipped += 1; + logger.info(`MKV-Sammelordner: Bonus-Datei uebersprungen: ${path.basename(filePath)} (Pfad: ${path.relative(sourceDir, filePath)})`); + continue; + } mkvFiles.push(filePath); } if (sampleSkipped > 0) { logger.info(`MKV-Sammelordner: pkg=${pkg.name}, ${sampleSkipped} Sample-MKV(s) übersprungen`); } + if (bonusSkipped > 0) { + logger.info(`MKV-Sammelordner: pkg=${pkg.name}, ${bonusSkipped} Bonus-MKV(s) übersprungen (Extras/Featurettes/etc.)`); + } if (mkvFiles.length === 0) { - logger.info(`MKV-Sammelordner: pkg=${pkg.name}, keine MKV nach Sample-Filter`); + logger.info(`MKV-Sammelordner: pkg=${pkg.name}, keine MKV nach Sample/Bonus-Filter`); return; } diff --git a/tests/download-manager.test.ts b/tests/download-manager.test.ts index 19f5724..f6f42d3 100644 --- a/tests/download-manager.test.ts +++ b/tests/download-manager.test.ts @@ -9356,6 +9356,101 @@ describe("download manager", () => { expect(fs.existsSync(originalExtractedPath)).toBe(false); }, 20000); + it("does NOT move bonus files from Extras subdirectory to flat library", async () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-")); + tempDirs.push(root); + + const packageName = "Breaking.Bad.S04.GERMAN.5.1.DL.BluRay.720p.x264-TSCC"; + const outputDir = path.join(root, "downloads", packageName); + const extractDir = path.join(root, "extract", packageName); + fs.mkdirSync(outputDir, { recursive: true }); + + // Build archive containing one real episode + several bonus files in an Extras subdirectory + const zip = new AdmZip(); + zip.addFile("Breaking.Bad.S04E01.GERMAN.5.1.DL.BluRay.720p.x264-TSCC.mkv", Buffer.from("episode-data")); + zip.addFile("Breaking.Bad.S04Extras.720p.BluRay.x264-TSCC/Schrotflinte.mkv", Buffer.from("bonus-1")); + zip.addFile("Breaking.Bad.S04Extras.720p.BluRay.x264-TSCC/Die.Autoexplosion.mkv", Buffer.from("bonus-2")); + zip.addFile("Breaking.Bad.S04Extras.720p.BluRay.x264-TSCC/White.House.mkv", Buffer.from("bonus-3")); + const archivePath = path.join(outputDir, "episode.zip"); + zip.writeZip(archivePath); + const archiveSize = fs.statSync(archivePath).size; + + const session = emptySession(); + const packageId = `${packageName}-pkg`; + const itemId = `${packageName}-item`; + const createdAt = Date.now() - 20_000; + session.packageOrder = [packageId]; + session.packages[packageId] = { + id: packageId, + name: packageName, + outputDir, + extractDir, + status: "downloading", + itemIds: [itemId], + cancelled: false, + enabled: true, + createdAt, + updatedAt: createdAt + }; + session.items[itemId] = { + id: itemId, + packageId, + url: `https://dummy/${packageName}`, + provider: "realdebrid", + status: "completed", + retries: 0, + speedBps: 0, + downloadedBytes: archiveSize, + totalBytes: archiveSize, + progressPercent: 100, + fileName: "episode.zip", + targetPath: archivePath, + resumable: true, + attempts: 1, + lastError: "", + fullStatus: "Fertig (100 MB)", + createdAt, + updatedAt: createdAt + }; + + const mkvLibraryDir = path.join(root, "mkv-library"); + + new DownloadManager( + { + ...defaultSettings(), + token: "rd-token", + outputDir: path.join(root, "downloads"), + extractDir: path.join(root, "extract"), + autoExtract: true, + autoRename4sf4sj: false, + collectMkvToLibrary: true, + mkvLibraryDir, + enableIntegrityCheck: false, + cleanupMode: "none" + }, + session, + createStoragePaths(path.join(root, "state")) + ); + + // Wait until the real episode landed in the library + const flattenedEpisode = path.join(mkvLibraryDir, "Breaking.Bad.S04E01.GERMAN.5.1.DL.BluRay.720p.x264-TSCC.mkv"); + await waitFor(() => fs.existsSync(flattenedEpisode), 12000); + + // Bonus files MUST NOT be in the flat library + expect(fs.existsSync(path.join(mkvLibraryDir, "Schrotflinte.mkv"))).toBe(false); + expect(fs.existsSync(path.join(mkvLibraryDir, "Die.Autoexplosion.mkv"))).toBe(false); + expect(fs.existsSync(path.join(mkvLibraryDir, "White.House.mkv"))).toBe(false); + + // Bonus files MUST still exist in the extract dir Extras subfolder + const extrasDir = path.join(extractDir, "Breaking.Bad.S04Extras.720p.BluRay.x264-TSCC"); + expect(fs.existsSync(path.join(extrasDir, "Schrotflinte.mkv"))).toBe(true); + expect(fs.existsSync(path.join(extrasDir, "Die.Autoexplosion.mkv"))).toBe(true); + expect(fs.existsSync(path.join(extrasDir, "White.House.mkv"))).toBe(true); + + // The real episode must be in the library and removed from extract + expect(fs.existsSync(flattenedEpisode)).toBe(true); + }, 20000); + it("keeps existing MKV names and appends a suffix while flattening", async () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-")); tempDirs.push(root);