From 6953b85b4c247431023baab0576c92e8d50c2658 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 11 Mar 2026 15:36:48 +0100 Subject: [PATCH] Harden the renaming --- app/Services/NameFixing/FileNameCleaner.php | 53 ++++++++++++++++++++- app/Services/NameFixing/FilePrioritizer.php | 46 +++++++++++++++++- 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/app/Services/NameFixing/FileNameCleaner.php b/app/Services/NameFixing/FileNameCleaner.php index a292ad4ea..913266368 100644 --- a/app/Services/NameFixing/FileNameCleaner.php +++ b/app/Services/NameFixing/FileNameCleaner.php @@ -16,6 +16,17 @@ class FileNameCleaner { use DetectsHashedNames; + /** + * Generic DVD structure filenames and similarly low-information names. + * + * @var list + */ + private const IGNORABLE_NAME_PATTERNS = [ + '/^(?:audio|video)[._-]?ts$/i', + '/^vts[._-]?\d{1,2}[._-]?\d{1,2}$/i', + '/^\d{1,3}$/', + ]; + /** * Archive extension patterns to remove. */ @@ -81,6 +92,10 @@ class FileNameCleaner // Extract filename from path $fileName = $this->extractFilenameFromPath($fileName); + if ($this->isIgnorableForMatching($fileName)) { + return false; + } + // Remove sample/proof indicators $fileName = preg_replace('/[\.\-_](sample|proof|subs?|thumbs?|cover|screens?)[\.\-_]?$/i', '', $fileName); @@ -106,7 +121,11 @@ class FileNameCleaner // Trim whitespace and punctuation $fileName = trim($fileName, " \t\n\r\0\x0B.-_"); - return $fileName !== '' ? $fileName : false; + if ($fileName === '' || $this->looksLikeStructuralJunk($fileName)) { + return false; + } + + return $fileName; } /** @@ -293,4 +312,36 @@ class FileNameCleaner return false; } + + /** + * Ignore junk inputs that should never drive PreDB matches. + */ + protected function isIgnorableForMatching(string $fileName): bool + { + if (preg_match('/\.url$/i', $fileName)) { + return true; + } + + return $this->looksLikeStructuralJunk((string) pathinfo($fileName, PATHINFO_FILENAME)); + } + + /** + * Detect low-information DVD structure names and numeric image files. + */ + protected function looksLikeStructuralJunk(string $fileName): bool + { + $normalized = strtolower(trim($fileName, " \t\n\r\0\x0B.-_")); + + if ($normalized === '') { + return true; + } + + foreach (self::IGNORABLE_NAME_PATTERNS as $pattern) { + if (preg_match($pattern, $normalized)) { + return true; + } + } + + return false; + } } diff --git a/app/Services/NameFixing/FilePrioritizer.php b/app/Services/NameFixing/FilePrioritizer.php index 31fef9cc3..7a37d88be 100644 --- a/app/Services/NameFixing/FilePrioritizer.php +++ b/app/Services/NameFixing/FilePrioritizer.php @@ -36,6 +36,17 @@ class FilePrioritizer private const PRIORITY_OTHER = 50; + /** + * Generic DVD structure names and similar low-information files. + * + * @var list + */ + private const IGNORABLE_NAME_PATTERNS = [ + '/^(?:audio|video)[._-]?ts$/i', + '/^vts[._-]?\d{1,2}[._-]?\d{1,2}$/i', + '/^\d{1,3}$/', + ]; + /** * Prioritize files for name matching. * @@ -108,8 +119,8 @@ class FilePrioritizer foreach ($files as $file) { $lowerFile = strtolower($file); - // Skip sample/proof files - if ($this->isSampleOrProof($file)) { + // Skip junk files that do not carry release identity. + if ($this->shouldIgnoreForNameMatching($file) || $this->isSampleOrProof($file)) { continue; } @@ -224,6 +235,37 @@ class FilePrioritizer return str_ends_with($lowerFilename, '.nfo'); } + /** + * Skip files that are usually just DVD structure noise or URL spam. + */ + protected function shouldIgnoreForNameMatching(string $filename): bool + { + $baseName = $this->extractFilenameFromPath($filename); + + if (preg_match('/\.url$/i', $baseName)) { + return true; + } + + $baseName = strtolower(trim((string) pathinfo($baseName, PATHINFO_FILENAME), " \t\n\r\0\x0B.-_")); + + foreach (self::IGNORABLE_NAME_PATTERNS as $pattern) { + if (preg_match($pattern, $baseName)) { + return true; + } + } + + return false; + } + + protected function extractFilenameFromPath(string $filename): string + { + if (preg_match('/[\\\\\/]([^\\\\\/]+)$/', $filename, $match)) { + return $match[1]; + } + + return $filename; + } + /** * Extract the most likely release name from multiple RAR files. *