mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-28 17:01:16 +00:00
Harden the renaming
This commit is contained in:
@@ -16,6 +16,17 @@ class FileNameCleaner
|
||||
{
|
||||
use DetectsHashedNames;
|
||||
|
||||
/**
|
||||
* Generic DVD structure filenames and similarly low-information names.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,17 @@ class FilePrioritizer
|
||||
|
||||
private const PRIORITY_OTHER = 50;
|
||||
|
||||
/**
|
||||
* Generic DVD structure names and similar low-information files.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user