1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Service\Storage; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
12
|
|
|
|
13
|
|
|
class FilenameCleaner |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $meta = [ |
19
|
|
|
// ripping |
20
|
|
|
'CamRip', |
21
|
|
|
'Cam', |
22
|
|
|
'Telesync', |
23
|
|
|
'TS', |
24
|
|
|
'Telecine', |
25
|
|
|
'TC', |
26
|
|
|
'Super Telesync', |
27
|
|
|
'SuperTS', |
28
|
|
|
'Super-TS', |
29
|
|
|
'VHS-Rip', |
30
|
|
|
'VHSRip', |
31
|
|
|
'Screener', |
32
|
|
|
'Scr', |
33
|
|
|
'VHS-Screener', |
34
|
|
|
'VHSScr', |
35
|
|
|
'PPVRip', |
36
|
|
|
'DVD-Screener', |
37
|
|
|
'DVDScr', |
38
|
|
|
'TV-Rip', |
39
|
|
|
'TVRip', |
40
|
|
|
'Sat-Rip', |
41
|
|
|
'SatRip', |
42
|
|
|
'HDTV-Rip', |
43
|
|
|
'HDTVRip', |
44
|
|
|
'PDTVRip', |
45
|
|
|
'HDRip', |
46
|
|
|
'BDRip', |
47
|
|
|
'DVD-Rip', |
48
|
|
|
'DVDRip', |
49
|
|
|
'LaserDisc-RIP', |
50
|
|
|
'LDRip', |
51
|
|
|
'Workprint', |
52
|
|
|
'WP', |
53
|
|
|
'WebRip', |
54
|
|
|
'Web-DL', |
55
|
|
|
'Web-DLRip', |
56
|
|
|
'Remux', |
57
|
|
|
'DCPrip', |
58
|
|
|
// DVD |
59
|
|
|
'DVD', |
60
|
|
|
'DVD5', |
61
|
|
|
'DVD9', |
62
|
|
|
'DVD10', |
63
|
|
|
'DVD18', |
64
|
|
|
// subtitles |
65
|
|
|
'Sub', |
66
|
|
|
'Subtitles', |
67
|
|
|
// display resolutions |
68
|
|
|
'8K', |
69
|
|
|
'5K', |
70
|
|
|
'4K', |
71
|
|
|
'1080p', |
72
|
|
|
'720p', |
73
|
|
|
'480p', |
74
|
|
|
]; |
75
|
|
|
|
76
|
13 |
|
public function __construct() |
77
|
|
|
{ |
78
|
13 |
|
$this->meta = array_map('preg_quote', $this->meta); |
79
|
13 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param SplFileInfo $file |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
13 |
|
public function clean(SplFileInfo $file) |
87
|
|
|
{ |
88
|
13 |
|
$name = $file->getFilename(); |
89
|
13 |
|
if ($file->isFile()) { |
90
|
11 |
|
$name = pathinfo($name, PATHINFO_FILENAME); |
91
|
11 |
|
} |
92
|
|
|
|
93
|
13 |
|
$name = str_replace("\t\n\r\0\x0B", ' ', $name); // remove control characters |
94
|
13 |
|
$name = str_replace('_', ' ', $name); |
95
|
|
|
|
96
|
13 |
|
$name = preg_replace('/\[[^\[\]]*\]/', ' ', $name); // remove [...] |
97
|
13 |
|
$name = preg_replace('/\([^\(\)]*\)/', ' ', $name); // remove (...) |
98
|
|
|
|
99
|
|
|
// remove all file meta data |
100
|
13 |
|
$reg = sprintf('/[ .,](?:%s)[ .,]/i', implode('|', $this->meta)); |
101
|
13 |
|
while (preg_match($reg, $name . ' ')) { |
102
|
3 |
|
$name = preg_replace($reg, ' ', $name . ' '); |
103
|
3 |
|
} |
104
|
|
|
|
105
|
13 |
|
$name = str_replace(' ', ' ', $name); // remove double spaces |
106
|
13 |
|
$name = trim($name, ' .,-'); |
107
|
|
|
|
108
|
13 |
|
return $name; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|