Total Complexity | 405 |
Total Lines | 2104 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 2 | Features | 0 |
Complex classes like ProcessAdditional often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProcessAdditional, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class ProcessAdditional |
||
40 | { |
||
41 | /** |
||
42 | * How many compressed (rar/zip) files to check. |
||
43 | * |
||
44 | * @var int |
||
45 | */ |
||
46 | public const maxCompressedFilesToCheck = 10; |
||
47 | |||
48 | protected $_releases; |
||
49 | |||
50 | /** |
||
51 | * Count of releases to work on. |
||
52 | */ |
||
53 | protected int $_totalReleases; |
||
54 | |||
55 | protected $_release; |
||
56 | |||
57 | protected NZB $_nzb; |
||
58 | |||
59 | /** |
||
60 | * List of files with sizes/etc contained in the NZB. |
||
61 | */ |
||
62 | protected array $_nzbContents; |
||
63 | |||
64 | protected Par2Info $_par2Info; |
||
65 | |||
66 | protected ArchiveInfo $_archiveInfo; |
||
67 | |||
68 | /** |
||
69 | * @var bool|null|string |
||
70 | */ |
||
71 | protected mixed $_innerFileBlacklist; |
||
72 | |||
73 | protected int $_maxNestedLevels; |
||
74 | |||
75 | /** |
||
76 | * @var string|null |
||
77 | */ |
||
78 | protected mixed $_7zipPath; |
||
79 | |||
80 | /** |
||
81 | * @var null|string |
||
82 | */ |
||
83 | protected mixed $_unrarPath; |
||
84 | |||
85 | protected string $_killString; |
||
86 | |||
87 | protected string|bool $_showCLIReleaseID; |
||
88 | |||
89 | protected int $_queryLimit; |
||
90 | |||
91 | protected int $_segmentsToDownload; |
||
92 | |||
93 | protected int $_maximumRarSegments; |
||
94 | |||
95 | protected int $_maximumRarPasswordChecks; |
||
96 | |||
97 | protected string $_maxSize; |
||
98 | |||
99 | protected string $_minSize; |
||
100 | |||
101 | protected bool $_processThumbnails; |
||
102 | |||
103 | protected string $_audioSavePath; |
||
104 | |||
105 | protected string $_supportFileRegex; |
||
106 | |||
107 | protected bool $_echoCLI; |
||
108 | |||
109 | protected NNTP $_nntp; |
||
110 | |||
111 | protected Categorize $_categorize; |
||
112 | |||
113 | protected NameFixer $_nameFixer; |
||
114 | |||
115 | protected ReleaseExtra $_releaseExtra; |
||
116 | |||
117 | protected ReleaseImage $_releaseImage; |
||
118 | |||
119 | protected Nfo $_nfo; |
||
120 | |||
121 | protected bool $_extractUsingRarInfo; |
||
122 | |||
123 | protected bool $_alternateNNTP; |
||
124 | |||
125 | protected int $_ffMPEGDuration; |
||
126 | |||
127 | protected bool $_addPAR2Files; |
||
128 | |||
129 | protected bool $_processVideo; |
||
130 | |||
131 | protected bool $_processJPGSample; |
||
132 | |||
133 | protected bool $_processAudioSample; |
||
134 | |||
135 | protected bool $_processMediaInfo; |
||
136 | |||
137 | protected bool $_processAudioInfo; |
||
138 | |||
139 | protected bool $_processPasswords; |
||
140 | |||
141 | protected string $_audioFileRegex; |
||
142 | |||
143 | protected string $_ignoreBookRegex; |
||
144 | |||
145 | protected string $_videoFileRegex; |
||
146 | |||
147 | /** |
||
148 | * Have we created a video file for the current release? |
||
149 | */ |
||
150 | protected bool $_foundVideo; |
||
151 | |||
152 | /** |
||
153 | * Have we found MediaInfo data for a Video for the current release? |
||
154 | */ |
||
155 | protected bool $_foundMediaInfo; |
||
156 | |||
157 | /** |
||
158 | * Have we found MediaInfo data for a Audio file for the current release? |
||
159 | */ |
||
160 | protected bool $_foundAudioInfo; |
||
161 | |||
162 | /** |
||
163 | * Have we created a short Audio file sample for the current release? |
||
164 | */ |
||
165 | protected bool $_foundAudioSample; |
||
166 | |||
167 | /** |
||
168 | * Extension of the found audio file (MP3/FLAC/etc). |
||
169 | */ |
||
170 | protected string $_AudioInfoExtension; |
||
171 | |||
172 | /** |
||
173 | * Have we downloaded a JPG file for the current release? |
||
174 | */ |
||
175 | protected bool $_foundJPGSample; |
||
176 | |||
177 | /** |
||
178 | * Have we created a Video JPG image sample for the current release? |
||
179 | */ |
||
180 | protected bool $_foundSample; |
||
181 | |||
182 | /** |
||
183 | * Have we found PAR2 info on this release? |
||
184 | */ |
||
185 | protected bool $_foundPAR2Info; |
||
186 | |||
187 | /** |
||
188 | * Message ID's for found content to download. |
||
189 | */ |
||
190 | protected array $_sampleMessageIDs; |
||
191 | |||
192 | protected $_JPGMessageIDs; |
||
193 | |||
194 | protected $_MediaInfoMessageIDs; |
||
195 | |||
196 | protected $_AudioInfoMessageIDs; |
||
197 | |||
198 | protected $_RARFileMessageIDs; |
||
199 | |||
200 | /** |
||
201 | * Password status of the current release. |
||
202 | */ |
||
203 | protected int $_passwordStatus; |
||
204 | |||
205 | /** |
||
206 | * Does the current release have a password? |
||
207 | */ |
||
208 | protected bool $_releaseHasPassword; |
||
209 | |||
210 | /** |
||
211 | * Does the current release have an NFO file? |
||
212 | */ |
||
213 | protected bool $_releaseHasNoNFO; |
||
214 | |||
215 | /** |
||
216 | * Name of the current release's usenet group. |
||
217 | */ |
||
218 | protected string $_releaseGroupName; |
||
219 | |||
220 | /** |
||
221 | * Number of file information added to DB (from rar/zip/par2 contents). |
||
222 | */ |
||
223 | protected int $_addedFileInfo; |
||
224 | |||
225 | /** |
||
226 | * Number of file information we found from RAR/ZIP. |
||
227 | * (if some of it was already in DB, this count goes up, while the count above does not). |
||
228 | */ |
||
229 | protected int $_totalFileInfo; |
||
230 | |||
231 | /** |
||
232 | * How many compressed (rar/zip) files have we checked. |
||
233 | */ |
||
234 | protected int $_compressedFilesChecked; |
||
235 | |||
236 | /** |
||
237 | * Should we download the last rar? |
||
238 | */ |
||
239 | protected bool $_fetchLastFiles; |
||
240 | |||
241 | /** |
||
242 | * Are we downloading the last rar? |
||
243 | */ |
||
244 | protected bool $_reverse; |
||
245 | |||
246 | protected ManticoreSearch $manticore; |
||
247 | |||
248 | private FFMpeg $ffmpeg; |
||
249 | |||
250 | private FFProbe $ffprobe; |
||
251 | |||
252 | private MediaInfo $mediaInfo; |
||
253 | |||
254 | private ElasticSearchSiteSearch $elasticsearch; |
||
255 | |||
256 | /** |
||
257 | * ProcessAdditional constructor. |
||
258 | * |
||
259 | * |
||
260 | * @throws \Exception |
||
261 | */ |
||
262 | public function __construct() |
||
263 | { |
||
264 | $this->_echoCLI = config('nntmux.echocli'); |
||
265 | |||
266 | $this->_nntp = new NNTP(); |
||
267 | |||
268 | $this->_nzb = new NZB(); |
||
269 | $this->_archiveInfo = new ArchiveInfo(); |
||
270 | $this->_categorize = new Categorize(); |
||
271 | $this->_nameFixer = new NameFixer(); |
||
272 | $this->_releaseExtra = new ReleaseExtra(); |
||
273 | $this->_releaseImage = new ReleaseImage(); |
||
274 | $this->_par2Info = new Par2Info(); |
||
275 | $this->_nfo = new Nfo(); |
||
276 | $this->manticore = new ManticoreSearch(); |
||
277 | $this->elasticsearch = new ElasticSearchSiteSearch(); |
||
278 | $this->ffmpeg = FFMpeg::create(['timeout' => Settings::settingValue('..timeoutseconds')]); |
||
279 | $this->ffprobe = FFProbe::create(); |
||
280 | $this->mediaInfo = new MediaInfo(); |
||
281 | $this->mediaInfo->setConfig('use_oldxml_mediainfo_output_format', true); |
||
|
|||
282 | $this->mediaInfo->setConfig('command', Settings::settingValue('apps..mediainfopath')); |
||
283 | |||
284 | $this->_innerFileBlacklist = Settings::settingValue('indexer.ppa.innerfileblacklist') === '' ? false : Settings::settingValue('indexer.ppa.innerfileblacklist'); |
||
285 | $this->_maxNestedLevels = (int) Settings::settingValue('..maxnestedlevels') === 0 ? 3 : (int) Settings::settingValue('..maxnestedlevels'); |
||
286 | $this->_extractUsingRarInfo = (int) Settings::settingValue('..extractusingrarinfo') !== 0; |
||
287 | $this->_fetchLastFiles = (int) Settings::settingValue('archive.fetch.end') !== 0; |
||
288 | |||
289 | $this->_7zipPath = false; |
||
290 | $this->_unrarPath = false; |
||
291 | |||
292 | // Pass the binary extractors to ArchiveInfo. |
||
293 | $clients = []; |
||
294 | if (! empty(Settings::settingValue('apps..unrarpath'))) { |
||
295 | $this->_unrarPath = Settings::settingValue('apps..unrarpath'); |
||
296 | $clients += [ArchiveInfo::TYPE_RAR => $this->_unrarPath]; |
||
297 | } |
||
298 | if (! empty(Settings::settingValue('apps..zippath'))) { |
||
299 | $this->_7zipPath = Settings::settingValue('apps..zippath'); |
||
300 | $clients += [ArchiveInfo::TYPE_SZIP => $this->_7zipPath]; |
||
301 | $clients += [ArchiveInfo::TYPE_ZIP => $this->_7zipPath]; |
||
302 | } |
||
303 | $this->_archiveInfo->setExternalClients($clients); |
||
304 | |||
305 | $this->_killString = '"'; |
||
306 | if (! empty(Settings::settingValue('apps..timeoutpath')) && (int) Settings::settingValue('..timeoutseconds') > 0) { |
||
307 | $this->_killString = ( |
||
308 | '"'.Settings::settingValue('apps..timeoutpath'). |
||
309 | '" --foreground --signal=KILL '. |
||
310 | Settings::settingValue('..timeoutseconds').' "' |
||
311 | ); |
||
312 | } |
||
313 | |||
314 | $this->_showCLIReleaseID = (PHP_BINARY.' '.__DIR__.'/ProcessAdditional.php ReleaseID: '); |
||
315 | |||
316 | // Maximum amount of releases to fetch per run. |
||
317 | $this->_queryLimit = |
||
318 | (Settings::settingValue('..maxaddprocessed') !== '') ? (int) Settings::settingValue('..maxaddprocessed') : 25; |
||
319 | |||
320 | // Maximum message ID's to download per file type in the NZB (video, jpg, etc). |
||
321 | $this->_segmentsToDownload = |
||
322 | (Settings::settingValue('..segmentstodownload') !== '') ? (int) Settings::settingValue('..segmentstodownload') : 2; |
||
323 | |||
324 | // Maximum message ID's to download for a RAR file. |
||
325 | $this->_maximumRarSegments = |
||
326 | (Settings::settingValue('..maxpartsprocessed') !== '') ? (int) Settings::settingValue('..maxpartsprocessed') : 3; |
||
327 | |||
328 | // Maximum RAR files to check for a password before stopping. |
||
329 | $this->_maximumRarPasswordChecks = |
||
330 | (Settings::settingValue('..passchkattempts') !== '') ? (int) Settings::settingValue('..passchkattempts') : 1; |
||
331 | |||
332 | $this->_maximumRarPasswordChecks = (max($this->_maximumRarPasswordChecks, 1)); |
||
333 | |||
334 | // Maximum size of releases in GB. |
||
335 | $this->_maxSize = (Settings::settingValue('..maxsizetopostprocess') !== '') ? (int) Settings::settingValue('..maxsizetopostprocess') : 100; |
||
336 | // Minimum size of releases in MB. |
||
337 | $this->_minSize = (Settings::settingValue('..minsizetopostprocess') !== '') ? (int) Settings::settingValue('..minsizetopostprocess') : 100; |
||
338 | |||
339 | // Use the alternate NNTP provider for downloading Message-ID's ? |
||
340 | $this->_alternateNNTP = (int) Settings::settingValue('..alternate_nntp') === 1; |
||
341 | |||
342 | $this->_ffMPEGDuration = Settings::settingValue('..ffmpeg_duration') !== '' ? (int) Settings::settingValue('..ffmpeg_duration') : 5; |
||
343 | |||
344 | $this->_addPAR2Files = (int) Settings::settingValue('..addpar2') !== 0; |
||
345 | |||
346 | if (! Settings::settingValue('apps..ffmpegpath')) { |
||
347 | $this->_processAudioSample = $this->_processThumbnails = $this->_processVideo = false; |
||
348 | } else { |
||
349 | $this->_processAudioSample = (int) Settings::settingValue('..saveaudiopreview') !== 0; |
||
350 | $this->_processThumbnails = (int) Settings::settingValue('..processthumbnails') !== 0; |
||
351 | $this->_processVideo = (int) Settings::settingValue('..processvideos') !== 0; |
||
352 | } |
||
353 | |||
354 | $this->_processJPGSample = (int) Settings::settingValue('..processjpg') !== 0; |
||
355 | $this->_processMediaInfo = Settings::settingValue('apps..mediainfopath') !== ''; |
||
356 | $this->_processAudioInfo = $this->_processMediaInfo; |
||
357 | $this->_processPasswords = ! empty(Settings::settingValue('..checkpasswordedrar')) && ! empty(Settings::settingValue('apps..unrarpath')); |
||
358 | |||
359 | $this->_audioSavePath = storage_path('covers/audiosample/'); |
||
360 | |||
361 | $this->_audioFileRegex = '\.(AAC|AIFF|APE|AC3|ASF|DTS|FLAC|MKA|MKS|MP2|MP3|RA|OGG|OGM|W64|WAV|WMA)'; |
||
362 | $this->_ignoreBookRegex = '/\b(epub|lit|mobi|pdf|sipdf|html)\b.*\.rar(?!.{20,})/i'; |
||
363 | $this->_supportFileRegex = '/\.(vol\d{1,3}\+\d{1,3}|par2|srs|sfv|nzb'; |
||
364 | $this->_videoFileRegex = '\.(AVI|F4V|IFO|M1V|M2V|M4V|MKV|MOV|MP4|MPEG|MPG|MPGV|MPV|OGV|QT|RM|RMVB|TS|VOB|WMV)'; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Clear out the main temp path when done. |
||
369 | */ |
||
370 | public function __destruct() |
||
371 | { |
||
372 | $this->_clearMainTmpPath(); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @throws \Exception |
||
377 | */ |
||
378 | public function start(string $groupID = '', string $guidChar = ''): void |
||
391 | } |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @var string Main temp path to work on. |
||
396 | */ |
||
397 | protected string $_mainTmpPath; |
||
398 | |||
399 | /** |
||
400 | * @var string Temp path for current release. |
||
401 | */ |
||
402 | protected string $tmpPath; |
||
403 | |||
404 | /** |
||
405 | * @throws \RuntimeException |
||
406 | * @throws \Exception |
||
407 | */ |
||
408 | protected function _setMainTempPath(&$guidChar, string &$groupID = ''): void |
||
409 | { |
||
410 | // Set up the temporary files folder location. |
||
411 | $this->_mainTmpPath = (string) Settings::settingValue('..tmpunrarpath'); |
||
412 | |||
413 | // Check if it ends with a dir separator. |
||
414 | if (! preg_match('/[\/\\\\]$/', $this->_mainTmpPath)) { |
||
415 | $this->_mainTmpPath .= '/'; |
||
416 | } |
||
417 | |||
418 | // If we are doing per group, use the groupID has a inner path, so other scripts don't delete the files we are working on. |
||
419 | if ($groupID !== '') { |
||
420 | $this->_mainTmpPath .= ($groupID.'/'); |
||
421 | } elseif ($guidChar !== '') { |
||
422 | $this->_mainTmpPath .= ($guidChar.'/'); |
||
423 | } |
||
424 | |||
425 | if (! File::isDirectory($this->_mainTmpPath)) { |
||
426 | if (! File::makeDirectory($this->_mainTmpPath, 0777, true, true) && ! File::isDirectory($this->_mainTmpPath)) { |
||
427 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $this->_mainTmpPath)); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | if (! File::isDirectory($this->_mainTmpPath)) { |
||
432 | throw new \RuntimeException('Could not create the tmpunrar folder ('.$this->_mainTmpPath.')'); |
||
433 | } |
||
434 | |||
435 | $this->_clearMainTmpPath(); |
||
436 | |||
437 | $this->tmpPath = $this->_mainTmpPath; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Clear out old folders/files from the main temp folder. |
||
442 | */ |
||
443 | protected function _clearMainTmpPath(): void |
||
444 | { |
||
445 | if ($this->_mainTmpPath !== '') { |
||
446 | $this->_recursivePathDelete( |
||
447 | $this->_mainTmpPath, |
||
448 | // These are folders we don't want to delete. |
||
449 | [ |
||
450 | // This is the actual temp folder. |
||
451 | $this->_mainTmpPath, |
||
452 | ] |
||
453 | ); |
||
454 | } |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Get all releases that need to be processed. |
||
459 | * |
||
460 | * |
||
461 | * @void |
||
462 | */ |
||
463 | protected function _fetchReleases(int|string $groupID, string &$guidChar): void |
||
464 | { |
||
465 | $releasesQuery = Release::query() |
||
466 | ->where('releases.nzbstatus', '=', 1) |
||
467 | ->where('releases.passwordstatus', '=', -1) |
||
468 | ->where('releases.haspreview', '=', -1) |
||
469 | ->where('categories.disablepreview', '=', 0); |
||
470 | if ($this->_maxSize > 0) { |
||
471 | $releasesQuery->where('releases.size', '<', (int) $this->_maxSize * 1073741824); |
||
472 | } |
||
473 | if ($this->_minSize > 0) { |
||
474 | $releasesQuery->where('releases.size', '>', (int) $this->_minSize * 1048576); |
||
475 | } |
||
476 | if (! empty($groupID)) { |
||
477 | $releasesQuery->where('releases.groups_id', $groupID); |
||
478 | } |
||
479 | if (! empty($guidChar)) { |
||
480 | $releasesQuery->where('releases.leftguid', $guidChar); |
||
481 | } |
||
482 | $releasesQuery->select(['releases.id', 'releases.id as releases_id', 'releases.guid', 'releases.name', 'releases.size', 'releases.groups_id', 'releases.nfostatus', 'releases.fromname', 'releases.completion', 'releases.categories_id', 'releases.searchname', 'releases.predb_id', 'categories.disablepreview']) |
||
483 | ->leftJoin('categories', 'categories.id', '=', 'releases.categories_id') |
||
484 | ->orderBy('releases.passwordstatus') |
||
485 | ->orderByDesc('releases.postdate') |
||
486 | ->limit($this->_queryLimit); |
||
487 | |||
488 | $this->_releases = $releasesQuery->get(); |
||
489 | $this->_totalReleases = $this->_releases->count(); |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Output the description and start time. |
||
494 | * |
||
495 | * @void |
||
496 | */ |
||
497 | protected function _echoDescription(): void |
||
498 | { |
||
499 | if ($this->_totalReleases > 1 && $this->_echoCLI) { |
||
500 | $this->_echo( |
||
501 | PHP_EOL. |
||
502 | 'Additional post-processing, started at: '. |
||
503 | now()->format('D M d, Y G:i a'). |
||
504 | PHP_EOL. |
||
505 | 'Downloaded: (xB) = yEnc article, f= Failed ;Processing: z = ZIP file, r = RAR file'. |
||
506 | PHP_EOL. |
||
507 | 'Added: s = Sample image, j = JPEG image, A = Audio sample, a = Audio MediaInfo, v = Video sample'. |
||
508 | PHP_EOL. |
||
509 | 'Added: m = Video MediaInfo, n = NFO, ^ = File details from inside the RAR/ZIP', |
||
510 | 'header' |
||
511 | ); |
||
512 | } |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Loop through the releases, processing them 1 at a time. |
||
517 | * |
||
518 | * @throws \RuntimeException |
||
519 | * @throws \Exception |
||
520 | */ |
||
521 | protected function _processReleases(): void |
||
522 | { |
||
523 | foreach ($this->_releases as $this->_release) { |
||
524 | $this->_echo( |
||
525 | PHP_EOL.'['.$this->_release->id.']['. |
||
526 | human_filesize($this->_release->size, 1).']', |
||
527 | 'primaryOver' |
||
528 | ); |
||
529 | |||
530 | cli_set_process_title($this->_showCLIReleaseID.$this->_release->id); |
||
531 | |||
532 | // Create folder to store temporary files. |
||
533 | if (! $this->_createTempFolder()) { |
||
534 | continue; |
||
535 | } |
||
536 | |||
537 | // Get NZB contents. |
||
538 | if (! $this->_getNZBContents()) { |
||
539 | continue; |
||
540 | } |
||
541 | |||
542 | // Reset the current release variables. |
||
543 | $this->_resetReleaseStatus(); |
||
544 | |||
545 | // Go through the files in the NZB, get the amount of book files. |
||
546 | $totalBooks = $this->_processNZBContents(); |
||
547 | |||
548 | // Check if this NZB is a large collection of books. |
||
549 | $bookFlood = false; |
||
550 | if ($totalBooks > 80 && ($totalBooks * 2) >= \count($this->_nzbContents)) { |
||
551 | $bookFlood = true; |
||
552 | } |
||
553 | |||
554 | if ($this->_processPasswords || $this->_processThumbnails || $this->_processMediaInfo || $this->_processAudioInfo || $this->_processVideo |
||
555 | ) { |
||
556 | // Process usenet Message-ID downloads. |
||
557 | $this->_processMessageIDDownloads(); |
||
558 | |||
559 | // Process compressed (RAR/ZIP) files inside the NZB. |
||
560 | if (! $bookFlood && $this->_NZBHasCompressedFile) { |
||
561 | // Download the RARs/ZIPs, extract the files inside them and insert the file info into the DB. |
||
562 | $this->_processNZBCompressedFiles(); |
||
563 | |||
564 | // Download rar/zip in reverse order, to get the last rar or zip file. |
||
565 | if ($this->_fetchLastFiles) { |
||
566 | $this->_processNZBCompressedFiles(true); |
||
567 | } |
||
568 | |||
569 | if (! $this->_releaseHasPassword) { |
||
570 | // Process the extracted files to get video/audio samples/etc. |
||
571 | $this->_processExtractedFiles(); |
||
572 | } |
||
573 | } |
||
574 | } |
||
575 | |||
576 | // Update the release to say we processed it. |
||
577 | $this->_finalizeRelease(); |
||
578 | |||
579 | // Delete all files / folders for this release. |
||
580 | $this->_recursivePathDelete($this->tmpPath); |
||
581 | } |
||
582 | if ($this->_echoCLI) { |
||
583 | echo PHP_EOL; |
||
584 | } |
||
585 | } |
||
586 | |||
587 | protected function _recursivePathDelete(string $path, array $ignoredFolders = []): void |
||
600 | } |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Create a temporary storage folder for the current release. |
||
605 | * |
||
606 | * |
||
607 | * |
||
608 | * @throws \Exception |
||
609 | */ |
||
610 | protected function _createTempFolder(): bool |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Get list of contents inside a release's NZB file. |
||
628 | * |
||
629 | * |
||
630 | * @throws \Exception |
||
631 | */ |
||
632 | protected function _getNZBContents(): bool |
||
633 | { |
||
634 | $nzbPath = $this->_nzb->NZBPath($this->_release->guid); |
||
635 | if ($nzbPath !== false) { |
||
636 | $nzbContents = Utility::unzipGzipFile($nzbPath); |
||
637 | if (! $nzbContents) { |
||
638 | if ($this->_echoCLI) { |
||
639 | $this->_echo('NZB is empty or broken for GUID: '.$this->_release->guid, 'warning'); |
||
640 | } |
||
641 | $this->_deleteRelease(); |
||
642 | |||
643 | return false; |
||
644 | } |
||
645 | // Get a list of files in the nzb. |
||
646 | $this->_nzbContents = $this->_nzb->nzbFileList($nzbContents, ['no-file-key' => false, 'strip-count' => true]); |
||
647 | if (\count($this->_nzbContents) === 0) { |
||
648 | if ($this->_echoCLI) { |
||
649 | $this->_echo('NZB is potentially broken for GUID: '.$this->_release->guid, 'warning'); |
||
650 | } |
||
651 | $this->_deleteRelease(); |
||
652 | |||
653 | return false; |
||
654 | } |
||
655 | // Sort keys. |
||
656 | ksort($this->_nzbContents, SORT_NATURAL); |
||
657 | |||
658 | return true; |
||
659 | } |
||
660 | if ($this->_echoCLI) { |
||
661 | $this->_echo('NZB not found for GUID: '.$this->_release->guid, 'warning'); |
||
662 | } |
||
663 | $this->_deleteRelease(); |
||
664 | |||
665 | return false; |
||
666 | } |
||
667 | |||
668 | protected function _deleteRelease(): void |
||
669 | { |
||
670 | Release::whereId($this->_release->id)->delete(); |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * Current file we are working on inside a NZB. |
||
675 | */ |
||
676 | protected array $_currentNZBFile; |
||
677 | |||
678 | /** |
||
679 | * Does the current NZB contain a compressed (RAR/ZIP) file? |
||
680 | */ |
||
681 | protected bool $_NZBHasCompressedFile; |
||
682 | |||
683 | /** |
||
684 | * Process the files inside the NZB, find Message-ID's to download. |
||
685 | * If we find files with book extensions, return the amount. |
||
686 | */ |
||
687 | protected function _processNZBContents(): int |
||
688 | { |
||
689 | $totalBookFiles = 0; |
||
690 | foreach ($this->_nzbContents as $this->_currentNZBFile) { |
||
691 | try { |
||
692 | // Check if it's not a nfo, nzb, par2 etc... |
||
693 | if (preg_match($this->_supportFileRegex.'|nfo\b|inf\b|ofn\b)($|[ ")\]-])(?!.{20,})/i', $this->_currentNZBFile['title'])) { |
||
694 | continue; |
||
695 | } |
||
696 | |||
697 | // Check if it's a rar/zip. |
||
698 | if (! $this->_NZBHasCompressedFile && |
||
699 | preg_match( |
||
700 | '/\.(part\d+|[r|z]\d+|rar|0+|0*10?|zipr\d{2,3}|zipx?)(\s*\.rar)*($|[ ")\]-])|"[a-f0-9]{32}\.[1-9]\d{1,2}".*\(\d+\/\d{2,}\)$/i', |
||
701 | $this->_currentNZBFile['title'] |
||
702 | ) |
||
703 | ) { |
||
704 | $this->_NZBHasCompressedFile = true; |
||
705 | } |
||
706 | |||
707 | // Look for a video sample, make sure it's not an image. |
||
708 | if ($this->_processThumbnails && empty($this->_sampleMessageIDs) && isset($this->_currentNZBFile['segments']) && stripos($this->_currentNZBFile['title'], 'sample') !== false && ! preg_match('/\.jpe?g$/i', $this->_currentNZBFile['title']) |
||
709 | ) { |
||
710 | // Get the amount of segments for this file. |
||
711 | $segCount = (\count($this->_currentNZBFile['segments']) - 1); |
||
712 | // If it's more than 1 try to get up to the site specified value of segments. |
||
713 | for ($i = 0; $i < $this->_segmentsToDownload; $i++) { |
||
714 | if ($i > $segCount) { |
||
715 | break; |
||
716 | } |
||
717 | $this->_sampleMessageIDs[] = (string) $this->_currentNZBFile['segments'][$i]; |
||
718 | } |
||
719 | } |
||
720 | |||
721 | // Look for a JPG picture, make sure it's not a CD cover. |
||
722 | if ($this->_processJPGSample && empty($this->_JPGMessageIDs) && isset($this->_currentNZBFile['segments']) && ! preg_match('/flac|lossless|mp3|music|inner-sanctum|sound/i', $this->_releaseGroupName) && preg_match('/\.jpe?g[. ")\]]/i', $this->_currentNZBFile['title']) |
||
723 | ) { |
||
724 | // Get the amount of segments for this file. |
||
725 | $segCount = (\count($this->_currentNZBFile['segments']) - 1); |
||
726 | // If it's more than 1 try to get up to the site specified value of segments. |
||
727 | for ($i = 0; $i < $this->_segmentsToDownload; $i++) { |
||
728 | if ($i > $segCount) { |
||
729 | break; |
||
730 | } |
||
731 | $this->_JPGMessageIDs[] = (string) $this->_currentNZBFile['segments'][$i]; |
||
732 | } |
||
733 | } |
||
734 | |||
735 | // Look for a video file, make sure it's not a sample, for MediaInfo. |
||
736 | if ($this->_processMediaInfo && empty($this->_MediaInfoMessageIDs) && isset($this->_currentNZBFile['segments'][0]) && stripos($this->_currentNZBFile['title'], 'sample') !== false && preg_match('/'.$this->_videoFileRegex.'[. ")\]]/i', $this->_currentNZBFile['title']) |
||
737 | ) { |
||
738 | $this->_MediaInfoMessageIDs = (string) $this->_currentNZBFile['segments'][0]; |
||
739 | } |
||
740 | |||
741 | // Look for an audio file. |
||
742 | if ($this->_processAudioInfo && empty($this->_AudioInfoMessageIDs) && isset($this->_currentNZBFile['segments']) && preg_match('/'.$this->_audioFileRegex.'[. ")\]]/i', $this->_currentNZBFile['title'], $type) |
||
743 | ) { |
||
744 | // Get the extension. |
||
745 | $this->_AudioInfoExtension = $type[1]; |
||
746 | $this->_AudioInfoMessageIDs = (string) $this->_currentNZBFile['segments'][0]; |
||
747 | } |
||
748 | |||
749 | // Some releases contain many books, increment this to ignore them later. |
||
750 | if (preg_match($this->_ignoreBookRegex, $this->_currentNZBFile['title'])) { |
||
751 | $totalBookFiles++; |
||
752 | } |
||
753 | } catch (\ErrorException $e) { |
||
754 | Log::debug($e->getTraceAsString()); |
||
755 | } |
||
756 | } |
||
757 | |||
758 | return $totalBookFiles; |
||
759 | } |
||
760 | |||
761 | /** |
||
762 | * List of message-id's we have tried for rar/zip files. |
||
763 | */ |
||
764 | protected array $_triedCompressedMids = []; |
||
765 | |||
766 | /** |
||
767 | * @throws \Exception |
||
768 | */ |
||
769 | protected function _processNZBCompressedFiles(bool $reverse = false): void |
||
770 | { |
||
771 | $this->_reverse = $reverse; |
||
772 | |||
773 | if ($this->_reverse) { |
||
774 | if (! krsort($this->_nzbContents)) { |
||
775 | return; |
||
776 | } |
||
777 | } else { |
||
778 | $this->_triedCompressedMids = []; |
||
779 | } |
||
780 | |||
781 | $failed = $downloaded = 0; |
||
782 | // Loop through the files, attempt to find if password-ed and files. Starting with what not to process. |
||
783 | foreach ($this->_nzbContents as $nzbFile) { |
||
784 | if ($downloaded >= $this->_maximumRarSegments) { |
||
785 | break; |
||
786 | } |
||
787 | |||
788 | if ($failed >= $this->_maximumRarPasswordChecks) { |
||
789 | break; |
||
790 | } |
||
791 | |||
792 | if ($this->_releaseHasPassword) { |
||
793 | $this->_echo('Skipping processing of rar '.$nzbFile['title'].' it has a password.', 'primaryOver'); |
||
794 | break; |
||
795 | } |
||
796 | |||
797 | // Probably not a rar/zip. |
||
798 | if (! preg_match( |
||
799 | '/\.(part\d+|[r|z]\d+|rar|0+|0*10?|zipr\d{2,3}|zipx?)(\s*\.rar)*($|[ ")\]-])|"[a-f0-9]{32}\.[1-9]\d{1,2}".*\(\d+\/\d{2,}\)$/i', |
||
800 | $nzbFile['title'] |
||
801 | ) |
||
802 | ) { |
||
803 | continue; |
||
804 | } |
||
805 | |||
806 | // Get message-id's for the rar file. |
||
807 | $segCount = (\count($nzbFile['segments']) - 1); |
||
808 | $mID = []; |
||
809 | for ($i = 0; $i < $this->_maximumRarSegments; $i++) { |
||
810 | if ($i > $segCount) { |
||
811 | break; |
||
812 | } |
||
813 | $segment = (string) $nzbFile['segments'][$i]; |
||
814 | if (! $this->_reverse) { |
||
815 | $this->_triedCompressedMids[] = $segment; |
||
816 | } elseif (\in_array($segment, $this->_triedCompressedMids, false)) { |
||
817 | // We already downloaded this file. |
||
818 | continue 2; |
||
819 | } |
||
820 | $mID[] = $segment; |
||
821 | } |
||
822 | // Nothing to download. |
||
823 | if (empty($mID)) { |
||
824 | continue; |
||
825 | } |
||
826 | |||
827 | // Download the article(s) from usenet. |
||
828 | $fetchedBinary = $this->_nntp->getMessages($this->_releaseGroupName, $mID, $this->_alternateNNTP); |
||
829 | if ($this->_nntp::isError($fetchedBinary)) { |
||
830 | $fetchedBinary = false; |
||
831 | } |
||
832 | |||
833 | if ($fetchedBinary !== false) { |
||
834 | // Echo we downloaded compressed file. |
||
835 | if ($this->_echoCLI) { |
||
836 | $this->_echo('(cB)', 'primaryOver'); |
||
837 | } |
||
838 | |||
839 | $downloaded++; |
||
840 | |||
841 | // Process the compressed file. |
||
842 | $decompressed = $this->_processCompressedData($fetchedBinary); |
||
843 | |||
844 | if ($decompressed || $this->_releaseHasPassword) { |
||
845 | break; |
||
846 | } |
||
847 | } else { |
||
848 | $failed++; |
||
849 | if ($this->_echoCLI) { |
||
850 | $this->_echo('f('.$failed.')', 'warningOver'); |
||
851 | } |
||
852 | } |
||
853 | } |
||
854 | } |
||
855 | |||
856 | /** |
||
857 | * Check if the data is a ZIP / RAR file, extract files, get file info. |
||
858 | * |
||
859 | * |
||
860 | * @throws \Exception |
||
861 | */ |
||
862 | protected function _processCompressedData(string &$compressedData): bool |
||
863 | { |
||
864 | $this->_compressedFilesChecked++; |
||
865 | // Give the data to archive info so it can check if it's a rar. |
||
866 | if (! $this->_archiveInfo->setData($compressedData, true)) { |
||
867 | if (config('app.debug') === true) { |
||
868 | $this->_debug('Data is probably not RAR or ZIP.'); |
||
869 | } |
||
870 | |||
871 | return false; |
||
872 | } |
||
873 | |||
874 | // Check if there's an error. |
||
875 | if ($this->_archiveInfo->error !== '') { |
||
876 | if (config('app.debug') === true) { |
||
877 | $this->_debug('ArchiveInfo Error: '.$this->_archiveInfo->error); |
||
878 | } |
||
879 | |||
880 | return false; |
||
881 | } |
||
882 | |||
883 | try { |
||
884 | // Get a summary of the compressed file. |
||
885 | $dataSummary = $this->_archiveInfo->getSummary(true); |
||
886 | } catch (\Exception $exception) { |
||
887 | // Log the exception and continue to next item |
||
888 | if (config('app.debug') === true) { |
||
889 | Log::warning($exception->getTraceAsString()); |
||
890 | } |
||
891 | |||
892 | return false; |
||
893 | } |
||
894 | |||
895 | // Check if the compressed file is encrypted. |
||
896 | if (! empty($this->_archiveInfo->isEncrypted) || (isset($dataSummary['is_encrypted']) && (int) $dataSummary['is_encrypted'] !== 0)) { |
||
897 | if (config('app.debug') === true) { |
||
898 | $this->_debug('ArchiveInfo: Compressed file has a password.'); |
||
899 | } |
||
900 | $this->_releaseHasPassword = true; |
||
901 | $this->_passwordStatus = Releases::PASSWD_RAR; |
||
902 | |||
903 | return false; |
||
904 | } |
||
905 | |||
906 | if ($this->_reverse) { |
||
907 | $fileData = $dataSummary['file_list'] ?? []; |
||
908 | if (! empty($fileData)) { |
||
909 | $rarFileName = Arr::pluck($fileData, 'name'); |
||
910 | if (preg_match(NameFixer::PREDB_REGEX, $rarFileName[0], $hit)) { |
||
911 | $preCheck = Predb::whereTitle($hit[0])->first(); |
||
912 | $this->_release->preid = $preCheck !== null ? $preCheck->value('id') : 0; |
||
913 | (new NameFixer())->updateRelease($this->_release, $preCheck->title ?? ucwords($hit[0], '.'), 'RarInfo FileName Match', true, 'Filenames, ', true, true, $this->_release->preid); |
||
914 | } elseif (! empty($dataSummary['archives']) && ! empty($dataSummary['archives'][$rarFileName[0]]['file_list'])) { |
||
915 | $archiveData = $dataSummary['archives'][$rarFileName[0]]['file_list']; |
||
916 | $archiveFileName = Arr::pluck($archiveData, 'name'); |
||
917 | if (preg_match(NameFixer::PREDB_REGEX, $archiveFileName[0], $match2)) { |
||
918 | $preCheck = Predb::whereTitle($match2[0])->first(); |
||
919 | $this->_release->preid = $preCheck !== null ? $preCheck->value('id') : 0; |
||
920 | (new NameFixer())->updateRelease($this->_release, $preCheck->title ?? ucwords($match2[0], '.'), 'RarInfo FileName Match', true, 'Filenames, ', true, true, $this->_release->preid); |
||
921 | } |
||
922 | } |
||
923 | } |
||
924 | } |
||
925 | |||
926 | switch ($dataSummary['main_type']) { |
||
927 | case ArchiveInfo::TYPE_RAR: |
||
928 | if ($this->_echoCLI) { |
||
929 | $this->_echo('r', 'primaryOver'); |
||
930 | } |
||
931 | |||
932 | if (! $this->_extractUsingRarInfo && $this->_unrarPath !== false) { |
||
933 | $fileName = $this->tmpPath.uniqid('', true).'.rar'; |
||
934 | File::put($fileName, $compressedData); |
||
935 | runCmd($this->_killString.$this->_unrarPath.'" e -ai -ep -c- -id -inul -kb -or -p- -r -y "'.$fileName.'" "'.$this->tmpPath.'unrar/"'); |
||
936 | File::delete($fileName); |
||
937 | } |
||
938 | break; |
||
939 | case ArchiveInfo::TYPE_SZIP: |
||
940 | if ($this->_echoCLI) { |
||
941 | $this->_echo('7z', 'primaryOver'); |
||
942 | } |
||
943 | |||
944 | if (! $this->_extractUsingRarInfo && ! empty($this->_7zipPath)) { |
||
945 | $fileName = $this->tmpPath.uniqid('', true).'.7z'; |
||
946 | File::put($fileName, $compressedData); |
||
947 | // Pass the -p flag to the 7zip command to make sure it doesn't get stuck in password prompt |
||
948 | runCmd($this->_killString.$this->_7zipPath.'" x "'.$fileName.'" -p -bd -y -o"'.$this->tmpPath.'unzip/"'); |
||
949 | File::delete($fileName); |
||
950 | } |
||
951 | break; |
||
952 | case ArchiveInfo::TYPE_ZIP: |
||
953 | if ($this->_echoCLI) { |
||
954 | $this->_echo('z', 'primaryOver'); |
||
955 | } |
||
956 | |||
957 | if (! $this->_extractUsingRarInfo && ! empty($this->_7zipPath)) { |
||
958 | $fileName = $this->tmpPath.uniqid('', true).'.zip'; |
||
959 | File::put($fileName, $compressedData); |
||
960 | // Pass the -p flag to the 7zip command to make sure it doesn't get stuck in password prompt |
||
961 | runCmd($this->_killString.$this->_7zipPath.'" x -tzip "'.$fileName.'" -p -bd -y -o"'.$this->tmpPath.'unzip/"'); |
||
962 | File::delete($fileName); |
||
963 | } |
||
964 | break; |
||
965 | default: |
||
966 | return false; |
||
967 | } |
||
968 | |||
969 | return $this->_processCompressedFileList(); |
||
970 | } |
||
971 | |||
972 | /** |
||
973 | * Get a list of all files in the compressed file, add the file info to the DB. |
||
974 | * |
||
975 | * |
||
976 | * @throws \Exception |
||
977 | */ |
||
978 | protected function _processCompressedFileList(): bool |
||
979 | { |
||
980 | // Get a list of files inside the Compressed file. |
||
981 | $files = $this->_archiveInfo->getArchiveFileList(); |
||
982 | if (! \is_array($files) || \count($files) === 0) { |
||
983 | return false; |
||
984 | } |
||
985 | |||
986 | // Loop through the files. |
||
987 | foreach ($files as $file) { |
||
988 | if ($this->_releaseHasPassword) { |
||
989 | break; |
||
990 | } |
||
991 | |||
992 | if (isset($file['name'])) { |
||
993 | if (isset($file['error'])) { |
||
994 | if (config('app.debug') === true) { |
||
995 | $this->_debug("Error: {$file['error']} (in: {$file['source']})"); |
||
996 | } |
||
997 | |||
998 | continue; |
||
999 | } |
||
1000 | |||
1001 | if (isset($file['pass']) && $file['pass'] === true) { |
||
1002 | $this->_releaseHasPassword = true; |
||
1003 | $this->_passwordStatus = Releases::PASSWD_RAR; |
||
1004 | break; |
||
1005 | } |
||
1006 | |||
1007 | if ($this->_innerFileBlacklist !== false && preg_match($this->_innerFileBlacklist, $file['name'])) { |
||
1008 | $this->_releaseHasPassword = true; |
||
1009 | $this->_passwordStatus = Releases::PASSWD_RAR; |
||
1010 | break; |
||
1011 | } |
||
1012 | |||
1013 | $fileName = []; |
||
1014 | if (preg_match('/[^\/\\\\]*\.[a-zA-Z0-9]*$/', $file['name'], $fileName)) { |
||
1015 | $fileName = $fileName[0]; |
||
1016 | } else { |
||
1017 | $fileName = ''; |
||
1018 | } |
||
1019 | |||
1020 | if ($this->_extractUsingRarInfo) { |
||
1021 | // Extract files from the rar. |
||
1022 | if (isset($file['compressed']) && (int) $file['compressed'] === 0) { |
||
1023 | File::put( |
||
1024 | $this->tmpPath.random_int(10, 999999).'_'.$fileName, |
||
1025 | $this->_archiveInfo->getFileData($file['name'], $file['source']) |
||
1026 | ); |
||
1027 | } // If the files are compressed, use a binary extractor. |
||
1028 | else { |
||
1029 | $this->_archiveInfo->extractFile($file['name'], $this->tmpPath.random_int(10, 999999).'_'.$fileName); |
||
1030 | } |
||
1031 | } |
||
1032 | } |
||
1033 | $this->_addFileInfo($file); |
||
1034 | } |
||
1035 | if ($this->_addedFileInfo > 0) { |
||
1036 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
1037 | $this->elasticsearch->updateRelease($this->_release->id); |
||
1038 | } else { |
||
1039 | $this->manticore->updateRelease($this->_release->id); |
||
1040 | } |
||
1041 | } |
||
1042 | |||
1043 | return $this->_totalFileInfo > 0; |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * @throws \Exception |
||
1048 | */ |
||
1049 | protected function _addFileInfo(&$file): void |
||
1050 | { |
||
1051 | // Don't add rar/zip files to the DB. |
||
1052 | if (! isset($file['error']) && isset($file['source']) && |
||
1053 | ! preg_match($this->_supportFileRegex.'|part\d+|[r|z]\d{1,3}|zipr\d{2,3}|\d{2,3}|zipx|zip|rar)(\s*\.rar)?$/i', $file['name']) |
||
1054 | ) { |
||
1055 | // Cache the amount of files we find in the RAR or ZIP, return this to say we did find RAR or ZIP content. |
||
1056 | // This is so we don't download more RAR or ZIP files for no reason. |
||
1057 | $this->_totalFileInfo++; |
||
1058 | |||
1059 | /* Check if we already have the file or not. |
||
1060 | * Also make sure we don't add too many files, some releases have 100's of files, like PS3 releases. |
||
1061 | */ |
||
1062 | if ($this->_addedFileInfo < 11 && ReleaseFile::query()->where(['releases_id' => $this->_release->id, 'name' => $file['name'], 'size' => $file['size']])->first() === null) { |
||
1063 | $addReleaseFiles = ReleaseFile::addReleaseFiles($this->_release->id, $file['name'], $file['size'], $file['date'], $file['pass'], '', $file['crc32'] ?? ''); |
||
1064 | if (! empty($addReleaseFiles)) { |
||
1065 | $this->_addedFileInfo++; |
||
1066 | |||
1067 | if ($this->_echoCLI) { |
||
1068 | $this->_echo('^', 'primaryOver'); |
||
1069 | } |
||
1070 | |||
1071 | // Check for "codec spam" |
||
1072 | if (preg_match('/alt\.binaries\.movies($|\.divx$)/', $this->_releaseGroupName) && |
||
1073 | preg_match('/[\/\\\\]Codec[\/\\\\]Setup\.exe$/i', $file['name']) |
||
1074 | ) { |
||
1075 | if (config('app.debug') === true) { |
||
1076 | $this->_debug('Codec spam found, setting release to potentially passworded.'); |
||
1077 | } |
||
1078 | $this->_releaseHasPassword = true; |
||
1079 | $this->_passwordStatus = Releases::PASSWD_RAR; |
||
1080 | } //Run a PreDB filename check on insert to try and match the release |
||
1081 | elseif ($file['name'] !== '' && ! str_starts_with($file['name'], '.')) { |
||
1082 | $this->_release['filename'] = $file['name']; |
||
1083 | $this->_release['releases_id'] = $this->_release->id; |
||
1084 | $this->_nameFixer->matchPreDbFiles($this->_release, 1, 1, true); |
||
1085 | } |
||
1086 | } |
||
1087 | } |
||
1088 | } |
||
1089 | } |
||
1090 | |||
1091 | /** |
||
1092 | * Go through all the extracted files in the temp folder and process them. |
||
1093 | * |
||
1094 | * @throws \Exception |
||
1095 | */ |
||
1096 | protected function _processExtractedFiles(): void |
||
1097 | { |
||
1098 | $nestedLevels = 0; |
||
1099 | |||
1100 | // Go through all the files in the temp folder, look for compressed files, extract them and the nested ones. |
||
1101 | while ($nestedLevels < $this->_maxNestedLevels) { |
||
1102 | // Break out if we checked more than x compressed files. |
||
1103 | if ($this->_compressedFilesChecked >= self::maxCompressedFilesToCheck) { |
||
1104 | break; |
||
1105 | } |
||
1106 | |||
1107 | $foundCompressedFile = false; |
||
1108 | |||
1109 | // Get all the compressed files in the temp folder. |
||
1110 | $files = $this->_getTempDirectoryContents('/.*\.([rz]\d{2,}|rar|zipx?|0{0,2}1)($|[^a-z0-9])/i'); |
||
1111 | |||
1112 | if ($files !== false) { |
||
1113 | foreach ($files as $file) { |
||
1114 | // Check if the file exists. |
||
1115 | if (File::isFile($file[0])) { |
||
1116 | $rarData = @File::get($file[0]); |
||
1117 | if ($rarData !== false) { |
||
1118 | $this->_processCompressedData($rarData); |
||
1119 | $foundCompressedFile = true; |
||
1120 | } |
||
1121 | File::delete($file[0]); |
||
1122 | } |
||
1123 | } |
||
1124 | } |
||
1125 | |||
1126 | // If we found no compressed files, break out. |
||
1127 | if (! $foundCompressedFile) { |
||
1128 | break; |
||
1129 | } |
||
1130 | |||
1131 | $nestedLevels++; |
||
1132 | } |
||
1133 | |||
1134 | $fileType = []; |
||
1135 | |||
1136 | // Get all the remaining files in the temp dir. |
||
1137 | $files = $this->_getTempDirectoryContents(); |
||
1138 | if ($files !== false) { |
||
1139 | foreach ($files as $file) { |
||
1140 | $file = $file->getPathname(); |
||
1141 | |||
1142 | // Skip /. and /.. |
||
1143 | if (preg_match('/[\/\\\\]\.{1,2}$/', $file)) { |
||
1144 | continue; |
||
1145 | } |
||
1146 | |||
1147 | if (File::isFile($file)) { |
||
1148 | // Process PAR2 files. |
||
1149 | if (! $this->_foundPAR2Info && preg_match('/\.par2$/', $file)) { |
||
1150 | $this->_siftPAR2Info($file); |
||
1151 | } // Process NFO files. |
||
1152 | elseif ($this->_releaseHasNoNFO && preg_match('/(\.(nfo|inf|ofn)|info\.txt)$/i', $file)) { |
||
1153 | $this->_processNfoFile($file); |
||
1154 | } // Process audio files. |
||
1155 | elseif ((! $this->_foundAudioInfo || ! $this->_foundAudioSample) && preg_match('/(.*)'.$this->_audioFileRegex.'$/i', $file, $fileType)) { |
||
1156 | // Try to get audio sample/audio media info. |
||
1157 | File::move($file, $this->tmpPath.'audiofile.'.$fileType[2]); |
||
1158 | $this->_getAudioInfo($this->tmpPath.'audiofile.'.$fileType[2], $fileType[2]); |
||
1159 | File::delete($this->tmpPath.'audiofile.'.$fileType[2]); |
||
1160 | } // Process JPG files. |
||
1161 | elseif (! $this->_foundJPGSample && preg_match('/\.jpe?g$/i', $file)) { |
||
1162 | $this->_getJPGSample($file); |
||
1163 | File::delete($file); |
||
1164 | } // Video sample // video clip // video media info. |
||
1165 | elseif ((! $this->_foundSample || ! $this->_foundVideo || ! $this->_foundMediaInfo) && preg_match('/(.*)'.$this->_videoFileRegex.'$/i', $file)) { |
||
1166 | $this->_processVideoFile($file); |
||
1167 | } // Check file's magic info. |
||
1168 | else { |
||
1169 | $output = Utility::fileInfo($file); |
||
1170 | if (! empty($output)) { |
||
1171 | switch (true) { |
||
1172 | case ! $this->_foundJPGSample && preg_match('/^JPE?G/i', $output): |
||
1173 | $this->_getJPGSample($file); |
||
1174 | File::delete($file); |
||
1175 | break; |
||
1176 | |||
1177 | case (! $this->_foundMediaInfo || ! $this->_foundSample || ! $this->_foundVideo) && preg_match('/Matroska data|MPEG v4|MPEG sequence, v2|\WAVI\W/i', $output): |
||
1178 | $this->_processVideoFile($file); |
||
1179 | break; |
||
1180 | |||
1181 | case (! $this->_foundAudioSample || ! $this->_foundAudioInfo) && preg_match('/^FLAC|layer III|Vorbis audio/i', $output, $fileType): |
||
1182 | switch ($fileType[0]) { |
||
1183 | case 'FLAC': |
||
1184 | $fileType = 'FLAC'; |
||
1185 | break; |
||
1186 | case 'layer III': |
||
1187 | $fileType = 'MP3'; |
||
1188 | break; |
||
1189 | case 'Vorbis audio': |
||
1190 | $fileType = 'OGG'; |
||
1191 | break; |
||
1192 | } |
||
1193 | File::move($file, $this->tmpPath.'audiofile.'.$fileType); |
||
1194 | $this->_getAudioInfo($this->tmpPath.'audiofile.'.$fileType, $fileType); |
||
1195 | File::delete($this->tmpPath.'audiofile.'.$fileType); |
||
1196 | break; |
||
1197 | |||
1198 | case ! $this->_foundPAR2Info && stripos($output, 'Parity') === 0: |
||
1199 | $this->_siftPAR2Info($file); |
||
1200 | break; |
||
1201 | } |
||
1202 | } |
||
1203 | } |
||
1204 | } |
||
1205 | } |
||
1206 | } |
||
1207 | } |
||
1208 | |||
1209 | /** |
||
1210 | * Download all binaries from usenet and form samples / get media info / etc from them. |
||
1211 | * |
||
1212 | * @void |
||
1213 | * |
||
1214 | * @throws \Exception |
||
1215 | */ |
||
1216 | protected function _processMessageIDDownloads(): void |
||
1217 | { |
||
1218 | $this->_processSampleMessageIDs(); |
||
1219 | $this->_processMediaInfoMessageIDs(); |
||
1220 | $this->_processAudioInfoMessageIDs(); |
||
1221 | $this->_processJPGMessageIDs(); |
||
1222 | } |
||
1223 | |||
1224 | /** |
||
1225 | * Download and process binaries for sample videos. |
||
1226 | * |
||
1227 | * @void |
||
1228 | * |
||
1229 | * @throws \Exception |
||
1230 | */ |
||
1231 | protected function _processSampleMessageIDs(): void |
||
1232 | { |
||
1233 | // Download and process sample image. |
||
1234 | if (! $this->_foundSample || ! $this->_foundVideo) { |
||
1235 | if (! empty($this->_sampleMessageIDs)) { |
||
1236 | // Download it from usenet. |
||
1237 | $sampleBinary = $this->_nntp->getMessages($this->_releaseGroupName, $this->_sampleMessageIDs, $this->_alternateNNTP); |
||
1238 | if ($this->_nntp::isError($sampleBinary)) { |
||
1239 | $sampleBinary = false; |
||
1240 | } |
||
1241 | |||
1242 | if ($sampleBinary !== false) { |
||
1243 | if ($this->_echoCLI) { |
||
1244 | $this->_echo('(sB)', 'primaryOver'); |
||
1245 | } |
||
1246 | |||
1247 | // Check if it's more than 40 bytes. |
||
1248 | if (\strlen($sampleBinary) > 40) { |
||
1249 | $fileLocation = $this->tmpPath.'sample_'.random_int(0, 99999).'.avi'; |
||
1250 | // Try to create the file. |
||
1251 | File::put($fileLocation, $sampleBinary); |
||
1252 | |||
1253 | // Try to get a sample picture. |
||
1254 | if (! $this->_foundSample) { |
||
1255 | $this->_foundSample = $this->_getSample($fileLocation); |
||
1256 | } |
||
1257 | |||
1258 | // Try to get a sample video. |
||
1259 | if (! $this->_foundVideo) { |
||
1260 | $this->_foundVideo = $this->_getVideo($fileLocation); |
||
1261 | } |
||
1262 | } |
||
1263 | } elseif ($this->_echoCLI) { |
||
1264 | $this->_echo('f', 'warningOver'); |
||
1265 | } |
||
1266 | } |
||
1267 | } |
||
1268 | } |
||
1269 | |||
1270 | /** |
||
1271 | * Download and process binaries for media info from videos. |
||
1272 | * |
||
1273 | * @void |
||
1274 | * |
||
1275 | * @throws \Exception |
||
1276 | */ |
||
1277 | protected function _processMediaInfoMessageIDs(): void |
||
1278 | { |
||
1279 | // Download and process mediainfo. Also try to get a sample if we didn't get one yet. |
||
1280 | if (! $this->_foundMediaInfo || ! $this->_foundSample || ! $this->_foundVideo) { |
||
1281 | if (! empty($this->_MediaInfoMessageIDs)) { |
||
1282 | // Try to download it from usenet. |
||
1283 | $mediaBinary = $this->_nntp->getMessages($this->_releaseGroupName, $this->_MediaInfoMessageIDs, $this->_alternateNNTP); |
||
1284 | if ($this->_nntp::isError($mediaBinary)) { |
||
1285 | // If error set it to false. |
||
1286 | $mediaBinary = false; |
||
1287 | } |
||
1288 | |||
1289 | if ($mediaBinary !== false) { |
||
1290 | if ($this->_echoCLI) { |
||
1291 | $this->_echo('(mB)', 'primaryOver'); |
||
1292 | } |
||
1293 | |||
1294 | // If it's more than 40 bytes... |
||
1295 | if (\strlen($mediaBinary) > 40) { |
||
1296 | $fileLocation = $this->tmpPath.'media.avi'; |
||
1297 | // Create a file on the disk with it. |
||
1298 | File::put($fileLocation, $mediaBinary); |
||
1299 | |||
1300 | // Try to get media info. |
||
1301 | if (! $this->_foundMediaInfo) { |
||
1302 | $this->_foundMediaInfo = $this->_getMediaInfo($fileLocation); |
||
1303 | } |
||
1304 | |||
1305 | // Try to get a sample picture. |
||
1306 | if (! $this->_foundSample) { |
||
1307 | $this->_foundSample = $this->_getSample($fileLocation); |
||
1308 | } |
||
1309 | |||
1310 | // Try to get a sample video. |
||
1311 | if (! $this->_foundVideo) { |
||
1312 | $this->_foundVideo = $this->_getVideo($fileLocation); |
||
1313 | } |
||
1314 | } |
||
1315 | } elseif ($this->_echoCLI) { |
||
1316 | $this->_echo('f', 'warningOver'); |
||
1317 | } |
||
1318 | } |
||
1319 | } |
||
1320 | } |
||
1321 | |||
1322 | /** |
||
1323 | * Download and process binaries for media info from songs. |
||
1324 | * |
||
1325 | * @void |
||
1326 | * |
||
1327 | * @throws \Exception |
||
1328 | */ |
||
1329 | protected function _processAudioInfoMessageIDs(): void |
||
1330 | { |
||
1331 | // Download audio file, use media info to try to get the artist / album. |
||
1332 | if (! $this->_foundAudioInfo || ! $this->_foundAudioSample) { |
||
1333 | if (! empty($this->_AudioInfoMessageIDs)) { |
||
1334 | // Try to download it from usenet. |
||
1335 | $audioBinary = $this->_nntp->getMessages($this->_releaseGroupName, $this->_AudioInfoMessageIDs, $this->_alternateNNTP); |
||
1336 | if ($this->_nntp::isError($audioBinary)) { |
||
1337 | $audioBinary = false; |
||
1338 | } |
||
1339 | |||
1340 | if ($audioBinary !== false) { |
||
1341 | if ($this->_echoCLI) { |
||
1342 | $this->_echo('(aB)', 'primaryOver'); |
||
1343 | } |
||
1344 | |||
1345 | $fileLocation = $this->tmpPath.'audio.'.$this->_AudioInfoExtension; |
||
1346 | // Create a file with it. |
||
1347 | File::put($fileLocation, $audioBinary); |
||
1348 | |||
1349 | // Try to get media info / sample of the audio file. |
||
1350 | $this->_getAudioInfo($fileLocation, $this->_AudioInfoExtension); |
||
1351 | } elseif ($this->_echoCLI) { |
||
1352 | $this->_echo('f', 'warningOver'); |
||
1353 | } |
||
1354 | } |
||
1355 | } |
||
1356 | } |
||
1357 | |||
1358 | /** |
||
1359 | * Download and process binaries for JPG pictures. |
||
1360 | * |
||
1361 | * @void |
||
1362 | * |
||
1363 | * @throws \Exception |
||
1364 | */ |
||
1365 | protected function _processJPGMessageIDs(): void |
||
1366 | { |
||
1367 | // Download JPG file. |
||
1368 | if (! $this->_foundJPGSample && ! empty($this->_JPGMessageIDs)) { |
||
1369 | // Try to download it. |
||
1370 | $jpgBinary = $this->_nntp->getMessages($this->_releaseGroupName, $this->_JPGMessageIDs, $this->_alternateNNTP); |
||
1371 | if ($this->_nntp::isError($jpgBinary)) { |
||
1372 | $jpgBinary = false; |
||
1373 | } |
||
1374 | |||
1375 | if ($jpgBinary !== false) { |
||
1376 | if ($this->_echoCLI) { |
||
1377 | $this->_echo('(jB)', 'primaryOver'); |
||
1378 | } |
||
1379 | |||
1380 | // Try to create a file with it. |
||
1381 | File::put($this->tmpPath.'samplepicture.jpg', $jpgBinary); |
||
1382 | |||
1383 | // Check if image exif data is jpeg |
||
1384 | if (exif_imagetype($this->tmpPath.'samplepicture.jpg') === IMAGETYPE_JPEG) { |
||
1385 | // Try to resize and move it. |
||
1386 | $this->_foundJPGSample = ($this->_releaseImage->saveImage($this->_release->guid.'_thumb', |
||
1387 | $this->tmpPath.'samplepicture.jpg', $this->_releaseImage->jpgSavePath, 650, 650) === 1); |
||
1388 | |||
1389 | if ($this->_foundJPGSample) { |
||
1390 | // Update the DB to say we got it. |
||
1391 | Release::query()->where('id', $this->_release->id)->update(['jpgstatus' => 1]); |
||
1392 | |||
1393 | if ($this->_echoCLI) { |
||
1394 | $this->_echo('j', 'primaryOver'); |
||
1395 | } |
||
1396 | } |
||
1397 | |||
1398 | File::delete($this->tmpPath.'samplepicture.jpg'); |
||
1399 | } |
||
1400 | } elseif ($this->_echoCLI) { |
||
1401 | $this->_echo('f', 'warningOver'); |
||
1402 | } |
||
1403 | } |
||
1404 | } |
||
1405 | |||
1406 | /** |
||
1407 | * Update the release to say we processed it. |
||
1408 | */ |
||
1409 | protected function _finalizeRelease(): void |
||
1410 | { |
||
1411 | $updateRows = ['haspreview' => 0]; |
||
1412 | |||
1413 | // If samples exist from previous runs, set flags. |
||
1414 | if (File::isFile($this->_releaseImage->imgSavePath.$this->_release->guid.'_thumb.jpg')) { |
||
1415 | $updateRows = ['haspreview' => 1]; |
||
1416 | } |
||
1417 | |||
1418 | if (File::isFile($this->_releaseImage->vidSavePath.$this->_release->guid.'.ogv')) { |
||
1419 | $updateRows += ['videostatus' => 1]; |
||
1420 | } |
||
1421 | |||
1422 | if (File::isFile($this->_releaseImage->jpgSavePath.$this->_release->guid.'_thumb.jpg')) { |
||
1423 | $updateRows += ['jpgstatus' => 1]; |
||
1424 | } |
||
1425 | |||
1426 | // Get the amount of files we found inside the RAR/ZIP files. |
||
1427 | |||
1428 | $releaseFilesCount = ReleaseFile::whereReleasesId($this->_release->id)->count('releases_id'); |
||
1429 | |||
1430 | if ($releaseFilesCount === null) { |
||
1431 | $releaseFilesCount = 0; |
||
1432 | } |
||
1433 | |||
1434 | $this->_passwordStatus = max([$this->_passwordStatus]); |
||
1435 | |||
1436 | // Set the release to no password if password processing is off. |
||
1437 | if (! $this->_processPasswords) { |
||
1438 | $this->_releaseHasPassword = false; |
||
1439 | } |
||
1440 | |||
1441 | // If we failed to get anything from the RAR/ZIPs, decrement the passwordstatus, if the rar/zip has no password. |
||
1442 | if (! $this->_releaseHasPassword && $this->_NZBHasCompressedFile && $releaseFilesCount === 0) { |
||
1443 | $release = Release::query()->where('id', $this->_release->id); |
||
1444 | $release->decrement('passwordstatus'); |
||
1445 | $release->update( |
||
1446 | $updateRows |
||
1447 | ); |
||
1448 | } // Else update the release with the password status (if the admin enabled the setting). |
||
1449 | else { |
||
1450 | $updateRows += ['passwordstatus' => $this->_processPasswords ? $this->_passwordStatus : Releases::PASSWD_NONE, |
||
1451 | 'rarinnerfilecount' => $releaseFilesCount, ]; |
||
1452 | Release::query()->where('id', $this->_release->id)->update( |
||
1453 | $updateRows |
||
1454 | ); |
||
1455 | } |
||
1456 | } |
||
1457 | |||
1458 | /** |
||
1459 | * @return bool|string|\Symfony\Component\Finder\SplFileInfo[] |
||
1460 | */ |
||
1461 | protected function _getTempDirectoryContents(string $pattern = '', string $path = ''): array|bool|string |
||
1462 | { |
||
1463 | if ($path === '') { |
||
1464 | $path = $this->tmpPath; |
||
1465 | } |
||
1466 | |||
1467 | $files = File::allFiles($path); |
||
1468 | try { |
||
1469 | if ($pattern !== '') { |
||
1470 | $allFiles = []; |
||
1471 | foreach ($files as $file) { |
||
1472 | if (preg_match($pattern, $file->getRelativePathname())) { |
||
1473 | $allFiles .= $file; |
||
1474 | } |
||
1475 | } |
||
1476 | |||
1477 | return $allFiles; |
||
1478 | } |
||
1479 | |||
1480 | return $files; |
||
1481 | } catch (\Throwable $e) { |
||
1482 | if (config('app.debug') === true) { |
||
1483 | Log::error($e->getTraceAsString()); |
||
1484 | $this->_debug('ERROR: Could not open temp dir: '.$e->getMessage()); |
||
1485 | } |
||
1486 | |||
1487 | return false; |
||
1488 | } |
||
1489 | } |
||
1490 | |||
1491 | /** |
||
1492 | * @throws \Exception |
||
1493 | */ |
||
1494 | protected function _getAudioInfo($fileLocation, $fileExtension): bool |
||
1495 | { |
||
1496 | // Return values. |
||
1497 | $retVal = $audVal = false; |
||
1498 | |||
1499 | // Check if audio sample fetching is on. |
||
1500 | if (! $this->_processAudioSample) { |
||
1501 | $audVal = true; |
||
1502 | } |
||
1503 | |||
1504 | // Check if media info fetching is on. |
||
1505 | if (! $this->_processAudioInfo) { |
||
1506 | $retVal = true; |
||
1507 | } |
||
1508 | |||
1509 | $rQuery = Release::query()->where('proc_pp', '=', 0)->where('id', $this->_release->id)->select(['searchname', 'fromname', 'categories_id'])->first(); |
||
1510 | |||
1511 | $musicParent = (string) Category::MUSIC_ROOT; |
||
1512 | if ($rQuery === null || ! preg_match( |
||
1513 | sprintf( |
||
1514 | '/%d\d{3}|%d|%d|%d/', |
||
1515 | $musicParent[0], |
||
1516 | Category::OTHER_MISC, |
||
1517 | Category::MOVIE_OTHER, |
||
1518 | Category::TV_OTHER |
||
1519 | ), |
||
1520 | $rQuery->id |
||
1521 | ) |
||
1522 | ) { |
||
1523 | return false; |
||
1524 | } |
||
1525 | |||
1526 | if (File::isFile($fileLocation)) { |
||
1527 | // Check if media info is enabled. |
||
1528 | if (! $retVal) { |
||
1529 | // Get the media info for the file. |
||
1530 | try { |
||
1531 | $xmlArray = $this->mediaInfo->getInfo($fileLocation, false); |
||
1532 | |||
1533 | if ($xmlArray !== null) { |
||
1534 | foreach ($xmlArray->getAudios() as $track) { |
||
1535 | if ($track->get('album') !== null && $track->get('performer') !== null) { |
||
1536 | if ((int) $this->_release->predb_id === 0 && config('nntmux.rename_music_mediainfo')) { |
||
1537 | // Make the extension upper case. |
||
1538 | $ext = strtoupper($fileExtension); |
||
1539 | |||
1540 | // Form a new search name. |
||
1541 | if (! empty($track->get('recorded_date')) && preg_match('/(?:19|20)\d\d/', $track->get('recorded_date')->getFullname(), $Year)) { |
||
1542 | $newName = $track->get('performer')->getFullName().' - '.$track->get('album')->getFullName().' ('.$Year[0].') '.$ext; |
||
1543 | } else { |
||
1544 | $newName = $track->get('performer')->getFullName().' - '.$track->get('album')->getFullName().' '.$ext; |
||
1545 | } |
||
1546 | |||
1547 | // Get the category or try to determine it. |
||
1548 | if ($ext === 'MP3') { |
||
1549 | $newCat = Category::MUSIC_MP3; |
||
1550 | } elseif ($ext === 'FLAC') { |
||
1551 | $newCat = Category::MUSIC_LOSSLESS; |
||
1552 | } else { |
||
1553 | $newCat = $this->_categorize->determineCategory($rQuery->groups_id, $newName, $rQuery->fromname); |
||
1554 | } |
||
1555 | |||
1556 | $newTitle = escapeString(substr($newName, 0, 255)); |
||
1557 | // Update the search name. |
||
1558 | $release = Release::whereId($this->_release->id); |
||
1559 | $release->update(['searchname' => $newTitle, 'categories_id' => $newCat['categories_id'], 'iscategorized' => 1, 'isrenamed' => 1, 'proc_pp' => 1]); |
||
1560 | |||
1561 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
1562 | $this->elasticsearch->updateRelease($this->_release->id); |
||
1563 | } else { |
||
1564 | $this->manticore->updateRelease($this->_release->id); |
||
1565 | } |
||
1566 | |||
1567 | // Echo the changed name. |
||
1568 | if ($this->_echoCLI) { |
||
1569 | NameFixer::echoChangedReleaseName( |
||
1570 | [ |
||
1571 | 'new_name' => $newTitle, |
||
1572 | 'old_name' => $rQuery->searchname, |
||
1573 | 'new_category' => $newCat, |
||
1574 | 'old_category' => $rQuery->id, |
||
1575 | 'group' => $rQuery->groups_id, |
||
1576 | 'releases_id' => $this->_release->id, |
||
1577 | 'method' => 'ProcessAdditional->_getAudioInfo', |
||
1578 | ] |
||
1579 | ); |
||
1580 | } |
||
1581 | } |
||
1582 | |||
1583 | // Add the media info. |
||
1584 | $this->_releaseExtra->addFromXml($this->_release->id, $xmlArray); |
||
1585 | |||
1586 | $retVal = true; |
||
1587 | $this->_foundAudioInfo = true; |
||
1588 | if ($this->_echoCLI) { |
||
1589 | $this->_echo('a', 'primaryOver'); |
||
1590 | } |
||
1591 | break; |
||
1592 | } |
||
1593 | } |
||
1594 | } |
||
1595 | } catch (\RuntimeException $e) { |
||
1596 | Log::debug($e->getMessage()); |
||
1597 | } catch (\TypeError $e) { |
||
1598 | Log::debug($e->getMessage()); |
||
1599 | } |
||
1600 | } |
||
1601 | |||
1602 | // Check if creating audio samples is enabled. |
||
1603 | if (! $audVal) { |
||
1604 | // File name to store audio file. |
||
1605 | $audioFileName = ($this->_release->guid.'.ogg'); |
||
1606 | |||
1607 | // Create an audio sample. |
||
1608 | if ($this->ffprobe->isValid($fileLocation)) { |
||
1609 | try { |
||
1610 | $audioSample = $this->ffmpeg->open($fileLocation); |
||
1611 | $format = new Vorbis(); |
||
1612 | $audioSample->clip(TimeCode::fromSeconds(30), TimeCode::fromSeconds(30)); |
||
1613 | $audioSample->save($format, $this->tmpPath.$audioFileName); |
||
1614 | } catch (\InvalidArgumentException $e) { |
||
1615 | if (config('app.debug') === true) { |
||
1616 | Log::error($e->getTraceAsString()); |
||
1617 | } |
||
1618 | //We do nothing, just prevent displaying errors because the file cannot be open(corrupted or incomplete file) |
||
1619 | } |
||
1620 | } |
||
1621 | |||
1622 | // Check if the new file was created. |
||
1623 | if (File::isFile($this->tmpPath.$audioFileName)) { |
||
1624 | // Try to move the temp audio file. |
||
1625 | $renamed = File::move($this->tmpPath.$audioFileName, $this->_audioSavePath.$audioFileName); |
||
1626 | |||
1627 | if (! $renamed) { |
||
1628 | // Try to copy it if it fails. |
||
1629 | $copied = File::copy($this->tmpPath.$audioFileName, $this->_audioSavePath.$audioFileName); |
||
1630 | |||
1631 | // Delete the old file. |
||
1632 | File::delete($this->tmpPath.$audioFileName); |
||
1633 | |||
1634 | // If it didn't copy continue. |
||
1635 | if (! $copied) { |
||
1636 | return false; |
||
1637 | } |
||
1638 | } |
||
1639 | |||
1640 | // Try to set the file perms. |
||
1641 | @chmod($this->_audioSavePath.$audioFileName, 0764); |
||
1642 | |||
1643 | // Update DB to said we got a audio sample. |
||
1644 | Release::query()->where('id', $this->_release->id)->update(['audiostatus' => 1]); |
||
1645 | |||
1646 | $audVal = $this->_foundAudioSample = true; |
||
1647 | |||
1648 | if ($this->_echoCLI) { |
||
1649 | $this->_echo('A', 'primaryOver'); |
||
1650 | } |
||
1651 | } |
||
1652 | } |
||
1653 | } |
||
1654 | |||
1655 | return $retVal && $audVal; |
||
1656 | } |
||
1657 | |||
1658 | /** |
||
1659 | * Try to get JPG picture, resize it and store it on disk. |
||
1660 | */ |
||
1661 | protected function _getJPGSample(string $fileLocation): void |
||
1662 | { |
||
1663 | // Try to resize/move the image. |
||
1664 | $this->_foundJPGSample = ( |
||
1665 | $this->_releaseImage->saveImage( |
||
1666 | $this->_release->guid.'_thumb', |
||
1667 | $fileLocation, |
||
1668 | $this->_releaseImage->jpgSavePath, |
||
1669 | 650, |
||
1670 | 650 |
||
1671 | ) === 1 |
||
1672 | ); |
||
1673 | |||
1674 | // If it's successful, tell the DB. |
||
1675 | if ($this->_foundJPGSample) { |
||
1676 | Release::query()->where('id', $this->_release->id)->update(['jpgstatus' => 1]); |
||
1677 | } |
||
1678 | } |
||
1679 | |||
1680 | private function getVideoTime(string $videoLocation): string |
||
1681 | { |
||
1682 | // Get the real duration of the file. |
||
1683 | if ($this->ffprobe->isValid($videoLocation)) { |
||
1684 | $time = $this->ffprobe->format($videoLocation)->get('duration'); |
||
1685 | } |
||
1686 | |||
1687 | if (empty($time) || ! preg_match('/time=(\d{1,2}:\d{1,2}:)?(\d{1,2})\.(\d{1,2})\s*bitrate=/i', $time, $numbers)) { |
||
1688 | return ''; |
||
1689 | } |
||
1690 | |||
1691 | // Reduce the last number by 1, this is to make sure we don't ask avconv/ffmpeg for non existing data. |
||
1692 | if ($numbers[3] > 0) { |
||
1693 | $numbers[3]--; |
||
1694 | } elseif ($numbers[1] > 0) { |
||
1695 | $numbers[2]--; |
||
1696 | $numbers[3] = '99'; |
||
1697 | } |
||
1698 | |||
1699 | // Manually pad the numbers in case they are 1 number. to get 02 for example instead of 2. |
||
1700 | return '00:00:'.str_pad($numbers[2], 2, '0', STR_PAD_LEFT).'.'.str_pad($numbers[3], 2, '0', STR_PAD_LEFT); |
||
1701 | } |
||
1702 | |||
1703 | /** |
||
1704 | * @throws \Exception |
||
1705 | */ |
||
1706 | protected function _getSample(string $fileLocation): bool |
||
1707 | { |
||
1708 | if (! $this->_processThumbnails) { |
||
1709 | return false; |
||
1710 | } |
||
1711 | |||
1712 | if (File::isFile($fileLocation)) { |
||
1713 | // Create path to temp file. |
||
1714 | $fileName = ($this->tmpPath.'zzzz'.random_int(5, 12).random_int(5, 12).'.jpg'); |
||
1715 | |||
1716 | $time = $this->getVideoTime($fileLocation); |
||
1717 | |||
1718 | // Create the image. |
||
1719 | if ($this->ffprobe->isValid($fileLocation)) { |
||
1720 | try { |
||
1721 | $this->ffmpeg->open($fileLocation)->frame(TimeCode::fromString($time === '' ? '00:00:03:00' : $time))->save($fileName); |
||
1722 | } catch (\RuntimeException $runtimeException) { |
||
1723 | if (config('app.debug') === true) { |
||
1724 | Log::error($runtimeException->getTraceAsString()); |
||
1725 | } |
||
1726 | //We show no error we just log it, we failed to save the frame and move on |
||
1727 | } catch (\InvalidArgumentException $e) { |
||
1728 | if (config('app.debug') === true) { |
||
1729 | Log::error($e->getTraceAsString()); |
||
1730 | } |
||
1731 | //We do nothing, just prevent displaying errors because the file cannot be open(corrupted or incomplete file) |
||
1732 | } catch (\Throwable $e) { |
||
1733 | if (config('app.debug') === true) { |
||
1734 | Log::error($e->getTraceAsString()); |
||
1735 | } |
||
1736 | //Again we do nothing, we just want to catch the error |
||
1737 | } |
||
1738 | } |
||
1739 | |||
1740 | // Check if the file exists. |
||
1741 | if (File::isFile($fileName)) { |
||
1742 | // Try to resize/move the image. |
||
1743 | $saved = $this->_releaseImage->saveImage( |
||
1744 | $this->_release->guid.'_thumb', |
||
1745 | $fileName, |
||
1746 | $this->_releaseImage->imgSavePath, |
||
1747 | 800, |
||
1748 | 600 |
||
1749 | ); |
||
1750 | |||
1751 | // Delete the temp file we created. |
||
1752 | File::delete($fileName); |
||
1753 | |||
1754 | // Check if it saved. |
||
1755 | if ($saved === 1) { |
||
1756 | if ($this->_echoCLI) { |
||
1757 | $this->_echo('s', 'primaryOver'); |
||
1758 | } |
||
1759 | |||
1760 | return true; |
||
1761 | } |
||
1762 | } |
||
1763 | } |
||
1764 | |||
1765 | return false; |
||
1766 | } |
||
1767 | |||
1768 | /** |
||
1769 | * @throws \Exception |
||
1770 | */ |
||
1771 | protected function _getVideo(string $fileLocation): bool |
||
1772 | { |
||
1773 | if (! $this->_processVideo) { |
||
1774 | return false; |
||
1775 | } |
||
1776 | |||
1777 | // Try to find an avi file. |
||
1778 | if (File::isFile($fileLocation)) { |
||
1779 | // Create a filename to store the temp file. |
||
1780 | $fileName = ($this->tmpPath.'zzzz'.$this->_release->guid.'.ogv'); |
||
1781 | |||
1782 | $newMethod = false; |
||
1783 | // If wanted sample length is less than 60, try to get sample from the end of the video. |
||
1784 | if ($this->_ffMPEGDuration < 60) { |
||
1785 | // Get the real duration of the file. |
||
1786 | $time = $this->getVideoTime($fileLocation); |
||
1787 | |||
1788 | if ($time !== '' && preg_match('/(\d{2}).(\d{2})/', $time, $numbers)) { |
||
1789 | $newMethod = true; |
||
1790 | // Get the lowest time we can start making the video at based on how many seconds the admin wants the video to be. |
||
1791 | if ($numbers[1] <= $this->_ffMPEGDuration) { |
||
1792 | // If the clip is shorter than the length we want. |
||
1793 | // The lowest we want is 0. |
||
1794 | $lowestLength = '00:00:00.00'; |
||
1795 | } else { |
||
1796 | // If the clip is longer than the length we want. |
||
1797 | // The lowest we want is the the difference between the max video length and our wanted total time. |
||
1798 | $lowestLength = ($numbers[1] - $this->_ffMPEGDuration); |
||
1799 | // Form the time string. |
||
1800 | $end = '.'.$numbers[2]; |
||
1801 | $lowestLength = match (\strlen($lowestLength)) { |
||
1802 | 1 => ('00:00:0'.$lowestLength.$end), |
||
1803 | 2 => ('00:00:'.$lowestLength.$end), |
||
1804 | default => '00:00:60.00', |
||
1805 | }; |
||
1806 | } |
||
1807 | |||
1808 | // Try to get the sample (from the end instead of the start). |
||
1809 | if ($this->ffprobe->isValid($fileLocation)) { |
||
1810 | try { |
||
1811 | $video = $this->ffmpeg->open($fileLocation); |
||
1812 | $videoSample = $video->clip(TimeCode::fromString($lowestLength), TimeCode::fromSeconds($this->_ffMPEGDuration)); |
||
1813 | $format = new Ogg(); |
||
1814 | $format->setAudioCodec('libvorbis'); |
||
1815 | $videoSample->filters()->resize(new Dimension(320, -1), ResizeFilter::RESIZEMODE_SCALE_HEIGHT); |
||
1816 | $videoSample->save($format, $fileName); |
||
1817 | } catch (\InvalidArgumentException $e) { |
||
1818 | if (config('app.debug') === true) { |
||
1819 | Log::error($e->getTraceAsString()); |
||
1820 | } |
||
1821 | //We do nothing, just prevent displaying errors because the file cannot be open(corrupted or incomplete file) |
||
1822 | } |
||
1823 | } |
||
1824 | } |
||
1825 | } |
||
1826 | |||
1827 | // If longer than 60 or we could not get the video length, run the old way. |
||
1828 | if (! $newMethod && $this->ffprobe->isValid($fileLocation)) { |
||
1829 | try { |
||
1830 | $video = $this->ffmpeg->open($fileLocation); |
||
1831 | $videoSample = $video->clip(TimeCode::fromSeconds(0), TimeCode::fromSeconds($this->_ffMPEGDuration)); |
||
1832 | $format = new Ogg(); |
||
1833 | $format->setAudioCodec('libvorbis'); |
||
1834 | $videoSample->filters()->resize(new Dimension(320, -1), ResizeFilter::RESIZEMODE_SCALE_HEIGHT); |
||
1835 | $videoSample->save($format, $fileName); |
||
1836 | } catch (\InvalidArgumentException $e) { |
||
1837 | if (config('app.debug') === true) { |
||
1838 | Log::error($e->getTraceAsString()); |
||
1839 | } |
||
1840 | //We do nothing, just prevent displaying errors because the file cannot be open(corrupted or incomplete file) |
||
1841 | } |
||
1842 | } |
||
1843 | |||
1844 | // Until we find the video file. |
||
1845 | if (File::isFile($fileName)) { |
||
1846 | // Create a path to where the file should be moved. |
||
1847 | $newFile = ($this->_releaseImage->vidSavePath.$this->_release->guid.'.ogv'); |
||
1848 | |||
1849 | // Try to move the file to the new path. |
||
1850 | // If we couldn't rename it, try to copy it. |
||
1851 | if (! @File::move($fileName, $newFile)) { |
||
1852 | $copied = @File::copy($fileName, $newFile); |
||
1853 | |||
1854 | // Delete the old file. |
||
1855 | File::delete($fileName); |
||
1856 | |||
1857 | // If it didn't copy, continue. |
||
1858 | if (! $copied) { |
||
1859 | return false; |
||
1860 | } |
||
1861 | } |
||
1862 | |||
1863 | // Change the permissions. |
||
1864 | @chmod($newFile, 0764); |
||
1865 | |||
1866 | // Update query to say we got the video. |
||
1867 | Release::query()->where('guid', $this->_release->guid)->update(['videostatus' => 1]); |
||
1868 | if ($this->_echoCLI) { |
||
1869 | $this->_echo('v', 'primaryOver'); |
||
1870 | } |
||
1871 | |||
1872 | return true; |
||
1873 | } |
||
1874 | } |
||
1875 | |||
1876 | return false; |
||
1877 | } |
||
1878 | |||
1879 | /** |
||
1880 | * @throws \Exception |
||
1881 | */ |
||
1882 | protected function _getMediaInfo($fileLocation): bool |
||
1883 | { |
||
1884 | if (! $this->_processMediaInfo) { |
||
1885 | return false; |
||
1886 | } |
||
1887 | |||
1888 | // Look for the video file. |
||
1889 | if (File::isFile($fileLocation)) { |
||
1890 | try { |
||
1891 | $xmlArray = $this->mediaInfo->getInfo($fileLocation, true); |
||
1892 | |||
1893 | // Check if we got it. |
||
1894 | |||
1895 | if ($xmlArray === null) { |
||
1896 | return false; |
||
1897 | } |
||
1898 | |||
1899 | // Insert it into the DB. |
||
1900 | $this->_releaseExtra->addFull($this->_release->id, $xmlArray); |
||
1901 | $this->_releaseExtra->addFromXml($this->_release->id, $xmlArray); |
||
1902 | |||
1903 | if ($this->_echoCLI) { |
||
1904 | $this->_echo('m', 'primaryOver'); |
||
1905 | } |
||
1906 | |||
1907 | return true; |
||
1908 | } catch (\RuntimeException $e) { |
||
1909 | Log::debug($e->getMessage()); |
||
1910 | |||
1911 | return false; |
||
1912 | } catch (\TypeError $e) { |
||
1913 | Log::debug($e->getMessage()); |
||
1914 | |||
1915 | return false; |
||
1916 | } catch (\ErrorException $e) { |
||
1917 | Log::debug($e->getMessage()); |
||
1918 | |||
1919 | return false; |
||
1920 | } |
||
1921 | } |
||
1922 | |||
1923 | return false; |
||
1924 | } |
||
1925 | |||
1926 | /** |
||
1927 | * @throws \Exception |
||
1928 | */ |
||
1929 | protected function _siftPAR2Info($fileLocation): void |
||
1930 | { |
||
1931 | $this->_par2Info->open($fileLocation); |
||
1932 | |||
1933 | if ($this->_par2Info->error) { |
||
1934 | return; |
||
1935 | } |
||
1936 | $releaseInfo = Release::query()->where('id', $this->_release->id)->select(['postdate', 'proc_pp'])->first(); |
||
1937 | |||
1938 | if ($releaseInfo === null) { |
||
1939 | return; |
||
1940 | } |
||
1941 | |||
1942 | $postDate = Carbon::createFromFormat('Y-m-d H:i:s', $releaseInfo->postdate)->getTimestamp(); |
||
1943 | |||
1944 | // Only get a new name if the category is OTHER. |
||
1945 | $foundName = true; |
||
1946 | if ((int) $releaseInfo->proc_pp === 0 && config('nntmux.rename_par2') && |
||
1947 | \in_array( |
||
1948 | (int) $this->_release->categories_id, |
||
1949 | Category::OTHERS_GROUP, |
||
1950 | false |
||
1951 | ) |
||
1952 | ) { |
||
1953 | $foundName = false; |
||
1954 | } |
||
1955 | |||
1956 | $filesAdded = 0; |
||
1957 | |||
1958 | foreach ($this->_par2Info->getFileList() as $file) { |
||
1959 | if (! isset($file['name'])) { |
||
1960 | continue; |
||
1961 | } |
||
1962 | |||
1963 | // If we found a name and added 10 files, stop. |
||
1964 | if ($foundName && $filesAdded > 10) { |
||
1965 | break; |
||
1966 | } |
||
1967 | |||
1968 | // Add to release files. |
||
1969 | if ($this->_addPAR2Files) { |
||
1970 | if ($filesAdded < 11 && ReleaseFile::query()->where(['releases_id' => $this->_release->id, 'name' => $file['name']])->first() === null |
||
1971 | ) { |
||
1972 | // Try to add the files to the DB. |
||
1973 | if (ReleaseFile::addReleaseFiles($this->_release->id, $file['name'], $file['size'], $postDate, 0, $file['hash_16K'])) { |
||
1974 | $filesAdded++; |
||
1975 | } |
||
1976 | } |
||
1977 | } else { |
||
1978 | $filesAdded++; |
||
1979 | } |
||
1980 | |||
1981 | // Try to get a new name. |
||
1982 | if (! $foundName) { |
||
1983 | $this->_release->textstring = $file['name']; |
||
1984 | $this->_release->releases_id = $this->_release->id; |
||
1985 | if ($this->_nameFixer->checkName($this->_release, $this->_echoCLI, 'PAR2, ', 1, 1)) { |
||
1986 | $foundName = true; |
||
1987 | } |
||
1988 | } |
||
1989 | } |
||
1990 | // Update the file count with the new file count + old file count. |
||
1991 | Release::query()->where('id', $this->_release->id)->increment('rarinnerfilecount', $filesAdded); |
||
1992 | $this->_foundPAR2Info = true; |
||
1993 | } |
||
1994 | |||
1995 | /** |
||
1996 | * @throws \Exception |
||
1997 | */ |
||
1998 | protected function _processNfoFile($fileLocation): void |
||
2003 | } |
||
2004 | } |
||
2005 | |||
2006 | /** |
||
2007 | * @throws \Exception |
||
2008 | */ |
||
2009 | protected function _processVideoFile($fileLocation): void |
||
2010 | { |
||
2011 | // Try to get a sample with it. |
||
2012 | if (! $this->_foundSample) { |
||
2013 | $this->_foundSample = $this->_getSample($fileLocation); |
||
2014 | } |
||
2015 | |||
2016 | /* Try to get a video with it. |
||
2017 | * Don't get it here if _sampleMessageIDs is empty |
||
2018 | * or has 1 message-id (Saves downloading another part). |
||
2019 | */ |
||
2020 | if (! $this->_foundVideo && \count($this->_sampleMessageIDs) < 2) { |
||
2021 | $this->_foundVideo = $this->_getVideo($fileLocation); |
||
2022 | } |
||
2023 | |||
2024 | // Try to get media info with it. |
||
2025 | if (! $this->_foundMediaInfo) { |
||
2026 | $this->_foundMediaInfo = $this->_getMediaInfo($fileLocation); |
||
2027 | } |
||
2028 | } |
||
2029 | |||
2030 | /** |
||
2031 | * Comparison function for uSort, for sorting NZB files. |
||
2032 | */ |
||
2033 | protected function _sortNZB(array|string|null $a, array|string|null $b): int |
||
2034 | { |
||
2035 | $pos = 0; |
||
2036 | $af = $bf = false; |
||
2037 | $a = preg_replace('/\d+[ ._-]?(\/|\||[o0]f)[ ._-]?\d+?(?![ ._-]\d)/i', ' ', $a['title']); |
||
2038 | $b = preg_replace('/\d+[ ._-]?(\/|\||[o0]f)[ ._-]?\d+?(?![ ._-]\d)/i', ' ', $b['title']); |
||
2039 | |||
2040 | if (preg_match('/\.(part\d+|[r|z]\d+)(\s*\.rar)*($|[ ")\]-])/i', $a)) { |
||
2041 | $af = true; |
||
2042 | } |
||
2043 | if (preg_match('/\.(part\d+|[r|z]\d+)(\s*\.rar)*($|[ ")\]-])/i', $b)) { |
||
2044 | $bf = true; |
||
2045 | } |
||
2046 | |||
2047 | if (! $af && preg_match('/\.rar($|[ ")\]-])/i', $a)) { |
||
2048 | $a = preg_replace('/\.rar(?:$|[ ")\]-])/i', '.*rar', $a); |
||
2049 | $af = true; |
||
2050 | } |
||
2051 | if (! $bf && preg_match('/\.rar($|[ ")\]-])/i', $b)) { |
||
2052 | $b = preg_replace('/\.rar(?:$|[ ")\]-])/i', '.*rar', $b); |
||
2053 | $bf = true; |
||
2054 | } |
||
2055 | |||
2056 | if (! $af && ! $bf) { |
||
2057 | return strnatcasecmp($a, $b); |
||
2058 | } |
||
2059 | |||
2060 | if (! $bf) { |
||
2061 | return -1; |
||
2062 | } |
||
2063 | |||
2064 | if (! $af) { |
||
2065 | return 1; |
||
2066 | } |
||
2067 | |||
2068 | if ($af && $bf) { |
||
2069 | return strnatcasecmp($a, $b); |
||
2070 | } |
||
2071 | |||
2072 | if ($af) { |
||
2073 | return -1; |
||
2074 | } |
||
2075 | |||
2076 | if ($bf) { |
||
2077 | return 1; |
||
2078 | } |
||
2079 | |||
2080 | return $pos; |
||
2081 | } |
||
2082 | |||
2083 | /** |
||
2084 | * Reset some variables for the current release. |
||
2085 | */ |
||
2086 | protected function _resetReleaseStatus(): void |
||
2117 | } |
||
2118 | |||
2119 | /** |
||
2120 | * Echo a string to CLI. |
||
2121 | * |
||
2122 | * @param string $string String to echo. |
||
2123 | * @param string $type Method type. |
||
2124 | * |
||
2125 | * @void |
||
2126 | */ |
||
2127 | protected function _echo(string $string, string $type): void |
||
2128 | { |
||
2129 | if ($this->_echoCLI) { |
||
2131 | } |
||
2132 | } |
||
2133 | |||
2134 | /** |
||
2135 | * Echo a string to CLI. For debugging. |
||
2136 | * |
||
2137 | * |
||
2138 | * @void |
||
2139 | */ |
||
2140 | protected function _debug(string $string): void |
||
2143 | } |
||
2144 | } |
||
2145 |