1 | <?php |
||
2 | /** |
||
3 | * Audio Player |
||
4 | * |
||
5 | * This file is licensed under the Affero General Public License version 3 or |
||
6 | * later. See the LICENSE.md file. |
||
7 | * |
||
8 | * @author Marcel Scherello <[email protected]> |
||
9 | * @author Sebastian Doell <[email protected]> |
||
10 | * @copyright 2016-2019 Marcel Scherello |
||
11 | * @copyright 2015 Sebastian Doell |
||
12 | */ |
||
13 | |||
14 | namespace OCA\audioplayer\Controller; |
||
15 | |||
16 | use Doctrine\DBAL\DBALException; |
||
17 | use Exception; |
||
18 | use getID3; |
||
19 | use getid3_exception; |
||
20 | use getid3_lib; |
||
21 | use OC; |
||
22 | use OCP\AppFramework\Controller; |
||
23 | use OCP\AppFramework\Http\JSONResponse; |
||
24 | use OCP\AppFramework\Http\TemplateResponse; |
||
25 | use OCP\Files\NotFoundException; |
||
26 | use OCP\Image; |
||
27 | use OCP\PreconditionNotMetException; |
||
28 | use Symfony\Component\Console\Output\NullOutput; |
||
29 | use Symfony\Component\Console\Output\OutputInterface; |
||
30 | use OCP\IRequest; |
||
31 | use OCP\IConfig; |
||
32 | use OCP\IL10N; |
||
33 | use OCP\L10N\IFactory; |
||
34 | use OCP\IDbConnection; |
||
35 | use OCP\Files\IRootFolder; |
||
36 | use OCP\ILogger; |
||
37 | use OCP\IDateTimeZone; |
||
38 | use OCP\IEventSource; |
||
39 | |||
40 | /** |
||
41 | * Controller class for main page. |
||
42 | */ |
||
43 | class ScannerController extends Controller |
||
44 | { |
||
45 | |||
46 | private $userId; |
||
47 | private $l10n; |
||
48 | private $iDublicate = 0; |
||
49 | private $iAlbumCount = 0; |
||
50 | private $numOfSongs; |
||
51 | private $db; |
||
52 | private $configManager; |
||
53 | private $occJob = false; |
||
54 | private $noFseek = false; |
||
55 | private $languageFactory; |
||
56 | private $rootFolder; |
||
57 | private $ID3Tags; |
||
58 | private $cyrillic; |
||
59 | private $logger; |
||
60 | private $parentIdPrevious = 0; |
||
61 | private $folderPicture = false; |
||
62 | private $DBController; |
||
63 | private $IDateTimeZone; |
||
64 | private $SettingController; |
||
65 | private $eventSource; |
||
66 | private $lastUpdated; |
||
67 | |||
68 | public function __construct( |
||
69 | $appName, |
||
70 | IRequest $request, |
||
71 | $userId, |
||
72 | IL10N $l10n, |
||
73 | IDbConnection $db, |
||
74 | IConfig $configManager, |
||
75 | IFactory $languageFactory, |
||
76 | IRootFolder $rootFolder, |
||
77 | ILogger $logger, |
||
78 | DbController $DBController, |
||
79 | SettingController $SettingController, |
||
80 | IDateTimeZone $IDateTimeZone |
||
81 | ) |
||
82 | { |
||
83 | parent::__construct($appName, $request); |
||
84 | $this->appName = $appName; |
||
85 | $this->userId = $userId; |
||
86 | $this->l10n = $l10n; |
||
87 | $this->db = $db; |
||
88 | $this->configManager = $configManager; |
||
89 | $this->languageFactory = $languageFactory; |
||
90 | $this->rootFolder = $rootFolder; |
||
91 | $this->logger = $logger; |
||
92 | $this->DBController = $DBController; |
||
93 | $this->SettingController = $SettingController; |
||
94 | $this->IDateTimeZone = $IDateTimeZone; |
||
95 | $this->eventSource = OC::$server->createEventSource(); |
||
96 | $this->lastUpdated = time(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @NoAdminRequired |
||
101 | * |
||
102 | */ |
||
103 | public function getImportTpl() |
||
104 | { |
||
105 | $params = []; |
||
106 | return new TemplateResponse('audioplayer', 'part.import', $params, ''); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @NoAdminRequired |
||
111 | * |
||
112 | * @param $userId |
||
113 | * @param $output |
||
114 | * @param $scanstop |
||
115 | * @return bool|JSONResponse |
||
116 | * @throws NotFoundException |
||
117 | * @throws getid3_exception |
||
118 | */ |
||
119 | public function scanForAudios($userId = null, $output = null, $scanstop = null) |
||
120 | { |
||
121 | set_time_limit(0); |
||
122 | if (isset($scanstop)) { |
||
123 | $this->DBController->setSessionValue('scanner_running', 'stopped', $this->userId); |
||
124 | $params = ['status' => 'stopped']; |
||
125 | return new JSONResponse($params); |
||
126 | } |
||
127 | |||
128 | // check if scanner is started from web or occ |
||
129 | if ($userId !== null) { |
||
130 | $this->occJob = true; |
||
131 | $this->userId = $userId; |
||
132 | $languageCode = $this->configManager->getUserValue($userId, 'core', 'lang'); |
||
133 | $this->l10n = $this->languageFactory->get('audioplayer', $languageCode); |
||
134 | } else { |
||
135 | $output = new NullOutput(); |
||
136 | } |
||
137 | |||
138 | $output->writeln("Start processing of <info>audio files</info>"); |
||
139 | |||
140 | $counter = 0; |
||
141 | $error_count = 0; |
||
142 | $duplicate_tracks = ''; |
||
143 | $error_file = ''; |
||
144 | $this->cyrillic = $this->configManager->getUserValue($this->userId, $this->appName, 'cyrillic'); |
||
145 | $this->DBController->setSessionValue('scanner_running', 'active', $this->userId); |
||
146 | |||
147 | $this->setScannerVersion(); |
||
148 | |||
149 | if (!class_exists('getid3_exception')) { |
||
150 | require_once __DIR__ . '/../../3rdparty/getid3/getid3.php'; |
||
151 | } |
||
152 | $getID3 = new getID3; |
||
153 | $getID3->setOption(['encoding' => 'UTF-8', |
||
154 | 'option_tag_id3v1' => false, |
||
155 | 'option_tag_id3v2' => true, |
||
156 | 'option_tag_lyrics3' => false, |
||
157 | 'option_tag_apetag' => false, |
||
158 | 'option_tags_process' => true, |
||
159 | 'option_tags_html' => false |
||
160 | ]); |
||
161 | |||
162 | $audios = $this->getAudioObjects($output); |
||
163 | $streams = $this->getStreamObjects($output); |
||
164 | |||
165 | if ($this->cyrillic === 'checked') $output->writeln("Cyrillic processing activated", OutputInterface::VERBOSITY_VERBOSE); |
||
166 | $output->writeln("Start processing of <info>audio files</info>", OutputInterface::VERBOSITY_VERBOSE); |
||
167 | |||
168 | $commitThreshold = max(200, intdiv(count($audios), 10)); |
||
169 | $this->DBController->beginTransaction(); |
||
170 | try { |
||
171 | foreach ($audios as &$audio) { |
||
172 | if ($this->scanCancelled()) { break; } |
||
173 | |||
174 | $counter++; |
||
175 | try { |
||
176 | $scanResult = $this->scanAudio($audio, $getID3, $output); |
||
177 | if ($scanResult === 'error') { |
||
178 | $error_file .= $audio->getPath() . '<br />'; |
||
179 | $error_count++; |
||
180 | } else if ($scanResult === 'duplicate') { |
||
181 | $duplicate_tracks .= $audio->getPath() . '<br />'; |
||
182 | $this->iDublicate++; |
||
183 | } |
||
184 | } catch (getid3_exception $e) { |
||
185 | $this->logger->error('getID3 error while building library: '. $e); |
||
186 | continue; |
||
187 | } |
||
188 | |||
189 | if ($this->timeForUpdate()) { |
||
190 | $this->updateProgress($counter, $audio->getPath(), $output); |
||
191 | } |
||
192 | if ($counter % $commitThreshold == 0) { |
||
193 | $this->DBController->commit(); |
||
194 | $output->writeln("Status committed to database", OutputInterface::VERBOSITY_VERBOSE); |
||
195 | $this->DBController->beginTransaction(); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $output->writeln("Start processing of <info>stream files</info>", OutputInterface::VERBOSITY_VERBOSE); |
||
200 | foreach ($streams as &$stream) { |
||
201 | if ($this->scanCancelled()) { break; } |
||
202 | |||
203 | $counter++; |
||
204 | $scanResult = $this->scanStream($stream, $output); |
||
205 | if ($scanResult === 'duplicate') { |
||
206 | $duplicate_tracks .= $stream->getPath() . '<br />'; |
||
207 | $this->iDublicate++; |
||
208 | } |
||
209 | |||
210 | if ($this->timeForUpdate()) { |
||
211 | $this->updateProgress($counter, $stream->getPath(), $output); |
||
212 | } |
||
213 | } |
||
214 | $this->setScannerTimestamp(); |
||
215 | $this->DBController->commit(); |
||
216 | } catch (DBALException $e) { |
||
217 | $this->logger->error('DB error while building library: '. $e); |
||
218 | $this->DBController->rollBack(); |
||
219 | } catch (Exception $e) { |
||
220 | $this->logger->error('Error while building library: '. $e); |
||
221 | $this->DBController->commit(); |
||
222 | } |
||
223 | |||
224 | // different outputs when web or occ |
||
225 | if (!$this->occJob) { |
||
226 | $message = $this->composeResponseMessage($counter, $error_count, $duplicate_tracks, $error_file); |
||
227 | $this->DBController->setSessionValue('scanner_running', '', $this->userId); |
||
228 | $response = [ |
||
229 | 'message' => $message |
||
230 | ]; |
||
231 | $response = json_encode($response); |
||
232 | $this->eventSource->send('done', $response); |
||
233 | $this->eventSource->close(); |
||
234 | return new JSONResponse(); |
||
235 | } else { |
||
236 | $output->writeln("Audios found: " . ($counter) . ""); |
||
237 | $output->writeln("Duplicates found: " . ($this->iDublicate) . ""); |
||
238 | $output->writeln("Written to library: " . ($counter - $this->iDublicate - $error_count) . ""); |
||
239 | $output->writeln("Albums found: " . ($this->iAlbumCount) . ""); |
||
240 | $output->writeln("Errors: " . ($error_count) . ""); |
||
241 | return true; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Check whether scan got cancelled by user |
||
247 | * @return bool |
||
248 | */ |
||
249 | private function scanCancelled() { |
||
250 | //check if scan is still supposed to run, or if dialog was closed in web already |
||
251 | if (!$this->occJob) { |
||
252 | $scan_running = $this->DBController->getSessionValue('scanner_running'); |
||
253 | return ($scan_running !== 'active'); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Process audio track and insert it into DB |
||
259 | * @param object $audio audio object to scan |
||
260 | * @param object $getID3 ID3 tag helper from getid3 library |
||
261 | * @param OutputInterface|null $output |
||
262 | * @return string |
||
263 | */ |
||
264 | private function scanAudio($audio, $getID3, $output) { |
||
265 | if ($this->checkFileChanged($audio)) { |
||
266 | $this->DBController->deleteFromDB($audio->getId(), $this->userId); |
||
267 | } |
||
268 | |||
269 | $this->analyze($audio, $getID3, $output); |
||
270 | |||
271 | # catch issue when getID3 does not bring a result in case of corrupt file or fpm-timeout |
||
272 | if (!isset($this->ID3Tags['bitrate']) AND !isset($this->ID3Tags['playtime_string'])) { |
||
273 | $this->logger->debug('Error with getID3. Does not seem to be a valid audio file: ' . $audio->getPath(), array('app' => 'audioplayer')); |
||
274 | $output->writeln(" Error with getID3. Does not seem to be a valid audio file", OutputInterface::VERBOSITY_VERBOSE); |
||
275 | return 'error'; |
||
276 | } |
||
277 | |||
278 | $album = $this->getID3Value(array('album')); |
||
279 | $genre = $this->getID3Value(array('genre')); |
||
280 | $artist = $this->getID3Value(array('artist')); |
||
281 | $name = $this->getID3Value(array('title'), $audio->getName()); |
||
282 | $trackNr = $this->getID3Value(array('track_number'), ''); |
||
283 | $composer = $this->getID3Value(array('composer'), ''); |
||
284 | $year = $this->getID3Value(array('year', 'creation_date', 'date'), 0); |
||
285 | $subtitle = $this->getID3Value(array('subtitle', 'version'), ''); |
||
286 | $disc = $this->getID3Value(array('part_of_a_set', 'discnumber', 'partofset', 'disc_number'), 1); |
||
287 | $isrc = $this->getID3Value(array('isrc'), ''); |
||
288 | $copyright = $this->getID3Value(array('copyright_message', 'copyright'), ''); |
||
289 | |||
290 | $iGenreId = $this->DBController->writeGenreToDB($this->userId, $genre); |
||
291 | $iArtistId = $this->DBController->writeArtistToDB($this->userId, $artist); |
||
292 | |||
293 | # write albumartist if available |
||
294 | # if no albumartist, NO artist is stored on album level |
||
295 | # in DBController loadArtistsToAlbum() takes over deriving the artists from the album tracks |
||
296 | # MP3, FLAC & MP4 have different tags for albumartist |
||
297 | $iAlbumArtistId = NULL; |
||
298 | $album_artist = $this->getID3Value(array('band', 'album_artist', 'albumartist', 'album artist'), '0'); |
||
299 | |||
300 | if ($album_artist !== '0') { |
||
301 | $iAlbumArtistId = $this->DBController->writeArtistToDB($this->userId, $album_artist); |
||
302 | } |
||
303 | |||
304 | $parentId = $audio->getParent()->getId(); |
||
305 | $return = $this->DBController->writeAlbumToDB($this->userId, $album, (int)$year, $iAlbumArtistId, $parentId); |
||
306 | $iAlbumId = $return['id']; |
||
307 | $this->iAlbumCount = $this->iAlbumCount + $return['albumcount']; |
||
308 | |||
309 | $bitrate = 0; |
||
310 | if (isset($this->ID3Tags['bitrate'])) { |
||
311 | $bitrate = $this->ID3Tags['bitrate']; |
||
312 | } |
||
313 | |||
314 | $playTimeString = ''; |
||
315 | if (isset($this->ID3Tags['playtime_string'])) { |
||
316 | $playTimeString = $this->ID3Tags['playtime_string']; |
||
317 | } |
||
318 | |||
319 | $this->getAlbumArt($audio, $iAlbumId, $parentId, $output); |
||
320 | |||
321 | $aTrack = [ |
||
322 | 'title' => $this->truncateStrings($name, '256'), |
||
323 | 'number' => $this->normalizeInteger($trackNr), |
||
324 | 'artist_id' => (int)$iArtistId, |
||
325 | 'album_id' => (int)$iAlbumId, |
||
326 | 'length' => $playTimeString, |
||
327 | 'file_id' => (int)$audio->getId(), |
||
328 | 'bitrate' => (int)$bitrate, |
||
329 | 'mimetype' => $audio->getMimetype(), |
||
330 | 'genre' => (int)$iGenreId, |
||
331 | 'year' => $this->truncateStrings($this->normalizeInteger($year), 4, ''), |
||
332 | 'disc' => $this->normalizeInteger($disc), |
||
333 | 'subtitle' => $this->truncateStrings($subtitle, '256'), |
||
334 | 'composer' => $this->truncateStrings($composer, '256'), |
||
335 | 'folder_id' => $parentId, |
||
336 | 'isrc' => $this->truncateStrings($isrc, '12'), |
||
337 | 'copyright' => $this->truncateStrings($copyright, '256'), |
||
338 | ]; |
||
339 | |||
340 | $return = $this->DBController->writeTrackToDB($this->userId, $aTrack); |
||
341 | if ($return['dublicate'] === 1) { |
||
342 | $this->logger->debug('Duplicate file: ' . $audio->getPath(), array('app' => 'audioplayer')); |
||
343 | $output->writeln(" This title is a duplicate and already existing", OutputInterface::VERBOSITY_VERBOSE); |
||
344 | return 'duplicate'; |
||
345 | } |
||
346 | return 'success'; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Process stream and insert it into DB |
||
351 | * @param object $stream stream object to scan |
||
352 | * @param OutputInterface|null $output |
||
353 | * @return string |
||
354 | */ |
||
355 | private function scanStream($stream, $output) { |
||
356 | $title = $this->truncateStrings($stream->getName(), '256'); |
||
357 | $aStream = [ |
||
358 | 'title' => substr($title, 0, strrpos($title, ".")), |
||
359 | 'artist_id' => 0, |
||
360 | 'album_id' => 0, |
||
361 | 'file_id' => (int)$stream->getId(), |
||
362 | 'bitrate' => 0, |
||
363 | 'mimetype' => $stream->getMimetype(), |
||
364 | ]; |
||
365 | $return = $this->DBController->writeStreamToDB($this->userId, $aStream); |
||
366 | if ($return['dublicate'] === 1) { |
||
367 | $this->logger->debug('Duplicate file: ' . $stream->getPath(), array('app' => 'audioplayer')); |
||
368 | $output->writeln(" This title is a duplicate and already existing", OutputInterface::VERBOSITY_VERBOSE); |
||
369 | return 'duplicate'; |
||
370 | } |
||
371 | return 'success'; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Summarize scan results in a message |
||
376 | * @param $counter number of tracks |
||
377 | * @param integer $error_count number of invalid files |
||
378 | * @param string $duplicate_tracks list of invalid files |
||
379 | * @param $error_file error-file |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
380 | * @return string |
||
381 | */ |
||
382 | private function composeResponseMessage($counter, |
||
383 | $error_count, |
||
384 | $duplicate_tracks, |
||
385 | $error_file) { |
||
386 | $message = (string)$this->l10n->t('Scanning finished!') . '<br />'; |
||
387 | $message .= (string)$this->l10n->t('Audios found: ') . $counter . '<br />'; |
||
388 | $message .= (string)$this->l10n->t('Written to library: ') . ($counter - $this->iDublicate - $error_count) . '<br />'; |
||
389 | $message .= (string)$this->l10n->t('Albums found: ') . $this->iAlbumCount . '<br />'; |
||
390 | if ($error_count > 0) { |
||
391 | $message .= '<br /><b>' . (string)$this->l10n->t('Errors: ') . $error_count . '<br />'; |
||
392 | $message .= (string)$this->l10n->t('If rescan does not solve this problem the files are broken') . '</b>'; |
||
393 | $message .= '<br />' . $error_file . '<br />'; |
||
394 | } |
||
395 | if ($this->iDublicate > 0) { |
||
396 | $message .= '<br /><b>' . (string)$this->l10n->t('Duplicates found: ') . ($this->iDublicate) . '</b>'; |
||
397 | $message .= '<br />' . $duplicate_tracks . '<br />'; |
||
398 | } |
||
399 | return $message; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Give feedback to user via appropriate output |
||
404 | * @param integer $filesProcessed |
||
405 | * @param string $currentFile |
||
406 | * @param OutputInterface|null $output |
||
407 | */ |
||
408 | private function updateProgress($filesProcessed, $currentFile, OutputInterface $output = null) |
||
409 | { |
||
410 | if (!$this->occJob) { |
||
411 | $response = [ |
||
412 | 'filesProcessed' => $filesProcessed, |
||
413 | 'filesTotal' => $this->numOfSongs, |
||
414 | 'currentFile' => $currentFile |
||
415 | ]; |
||
416 | $response = json_encode($response); |
||
417 | $this->eventSource->send('progress', $response); |
||
418 | } else { |
||
419 | $output->writeln(" " . $currentFile . "</info>", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
420 | } |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Prevent flood over the wire |
||
425 | * @return bool |
||
426 | */ |
||
427 | private function timeForUpdate() |
||
428 | { |
||
429 | if ($this->occJob) { |
||
430 | return true; |
||
431 | } |
||
432 | $now = time(); |
||
433 | if ($now - $this->lastUpdated >= 1) { |
||
434 | $this->lastUpdated = $now; |
||
435 | return true; |
||
436 | } |
||
437 | return false; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * if the scanner is started on an empty library, the current app version is stored |
||
442 | * |
||
443 | */ |
||
444 | private function setScannerVersion() |
||
445 | { |
||
446 | $stmt = $this->db->prepare('SELECT COUNT(`id`) AS `TRACKCOUNT` FROM `*PREFIX*audioplayer_tracks` WHERE `user_id` = ? '); |
||
447 | $stmt->execute(array($this->userId)); |
||
448 | $row = $stmt->fetch(); |
||
449 | if ((int)$row['TRACKCOUNT'] === 0) { |
||
450 | $app_version = $this->configManager->getAppValue($this->appName, 'installed_version', '0.0.0'); |
||
451 | $this->configManager->setUserValue($this->userId, $this->appName, 'scanner_version', $app_version); |
||
452 | } |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Add track to db if not exist |
||
457 | * |
||
458 | * @param OutputInterface $output |
||
459 | * @return array |
||
460 | * @throws NotFoundException |
||
461 | */ |
||
462 | private function getAudioObjects(OutputInterface $output = null) |
||
463 | { |
||
464 | $audioPath = $this->configManager->getUserValue($this->userId, $this->appName, 'path'); |
||
465 | $userView = $this->rootFolder->getUserFolder($this->userId); |
||
466 | |||
467 | if ($audioPath !== null && $audioPath !== '/' && $audioPath !== '') { |
||
468 | $userView = $userView->get($audioPath); |
||
469 | } |
||
470 | |||
471 | $audios_mp3 = $userView->searchByMime('audio/mpeg'); |
||
472 | $audios_m4a = $userView->searchByMime('audio/mp4'); |
||
473 | $audios_ogg = $userView->searchByMime('audio/ogg'); |
||
474 | $audios_wav = $userView->searchByMime('audio/wav'); |
||
475 | $audios_flac = $userView->searchByMime('audio/flac'); |
||
476 | $audios = array_merge($audios_mp3, $audios_m4a, $audios_ogg, $audios_wav, $audios_flac); |
||
477 | |||
478 | $output->writeln("Scanned Folder: " . $userView->getPath(), OutputInterface::VERBOSITY_VERBOSE); |
||
479 | $output->writeln("<info>Total audio files:</info> " . count($audios), OutputInterface::VERBOSITY_VERBOSE); |
||
480 | $output->writeln("Checking audio files to be skipped", OutputInterface::VERBOSITY_VERBOSE); |
||
481 | |||
482 | // get all fileids which are in an excluded folder |
||
483 | $stmt = $this->db->prepare('SELECT `fileid` from `*PREFIX*filecache` WHERE `parent` IN (SELECT `parent` FROM `*PREFIX*filecache` WHERE `name` = ? OR `name` = ? ORDER BY `fileid` ASC)'); |
||
484 | $stmt->execute(array('.noAudio', '.noaudio')); |
||
485 | $results = $stmt->fetchAll(); |
||
486 | $resultExclude = array_column($results, 'fileid'); |
||
487 | |||
488 | // get all fileids which are already in the Audio Player Database |
||
489 | $stmt = $this->db->prepare('SELECT `file_id` FROM `*PREFIX*audioplayer_tracks` WHERE `user_id` = ? '); |
||
490 | $stmt->execute(array($this->userId)); |
||
491 | $results = $stmt->fetchAll(); |
||
492 | $resultExisting = array_column($results, 'file_id'); |
||
493 | |||
494 | foreach ($audios as $key => &$audio) { |
||
495 | $current_id = $audio->getID(); |
||
496 | if (in_array($current_id, $resultExclude)) { |
||
497 | $output->writeln(" " . $current_id . " - " . $audio->getPath() . " => excluded", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
498 | unset($audios[$key]); |
||
499 | } elseif (in_array($current_id, $resultExisting)) { |
||
500 | if ($this->checkFileChanged($audio)) { |
||
501 | $output->writeln(" " . $current_id . " - " . $audio->getPath() . " => indexed title changed => reindex", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
502 | } else { |
||
503 | $output->writeln(" " . $current_id . " - " . $audio->getPath() . " => already indexed", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
504 | unset($audios[$key]); |
||
505 | } |
||
506 | } |
||
507 | } |
||
508 | $this->numOfSongs = count($audios); |
||
509 | $output->writeln("Final audio files to be processed: " . $this->numOfSongs, OutputInterface::VERBOSITY_VERBOSE); |
||
510 | return $audios; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * check changed timestamps |
||
515 | * |
||
516 | * @param object $audio |
||
517 | * @return bool |
||
518 | */ |
||
519 | private function checkFileChanged($audio) |
||
520 | { |
||
521 | $modTime = $audio->getMTime(); |
||
522 | $scannerTime = $this->getScannerTimestamp(); |
||
523 | if ($modTime >= $scannerTime - 300) { |
||
524 | return true; |
||
525 | } else { |
||
526 | return false; |
||
527 | } |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * check the timestamp of the last scan to derive changed files |
||
532 | * |
||
533 | */ |
||
534 | private function getScannerTimestamp() |
||
535 | { |
||
536 | return $this->configManager->getUserValue($this->userId, $this->appName, 'scanner_timestamp', 300); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Add track to db if not exist |
||
541 | * |
||
542 | * @param OutputInterface $output |
||
543 | * @return array |
||
544 | * @throws NotFoundException |
||
545 | */ |
||
546 | private function getStreamObjects(OutputInterface $output = null) |
||
547 | { |
||
548 | $audios_clean = array(); |
||
549 | $audioPath = $this->configManager->getUserValue($this->userId, $this->appName, 'path'); |
||
550 | $userView = $this->rootFolder->getUserFolder($this->userId); |
||
551 | |||
552 | if ($audioPath !== null && $audioPath !== '/' && $audioPath !== '') { |
||
553 | $userView = $userView->get($audioPath); |
||
554 | } |
||
555 | |||
556 | $audios_mpegurl = $userView->searchByMime('audio/mpegurl'); |
||
557 | $audios_scpls = $userView->searchByMime('audio/x-scpls'); |
||
558 | $audios_xspf = $userView->searchByMime('application/xspf+xml'); |
||
559 | $audios = array_merge($audios_mpegurl, $audios_scpls, $audios_xspf); |
||
560 | $output->writeln("<info>Total stream files:</info> " . count($audios), OutputInterface::VERBOSITY_VERBOSE); |
||
561 | $output->writeln("Checking stream files to be skipped", OutputInterface::VERBOSITY_VERBOSE); |
||
562 | |||
563 | // get all fileids which are in an excluded folder |
||
564 | $stmt = $this->db->prepare('SELECT `fileid` from `*PREFIX*filecache` WHERE `parent` IN (SELECT `parent` FROM `*PREFIX*filecache` WHERE `name` = ? OR `name` = ? ORDER BY `fileid` ASC)'); |
||
565 | $stmt->execute(array('.noAudio', '.noaudio')); |
||
566 | $results = $stmt->fetchAll(); |
||
567 | $resultExclude = array_column($results, 'fileid'); |
||
568 | |||
569 | // get all fileids which are already in the Audio Player Database |
||
570 | $stmt = $this->db->prepare('SELECT `file_id` FROM `*PREFIX*audioplayer_streams` WHERE `user_id` = ? '); |
||
571 | $stmt->execute(array($this->userId)); |
||
572 | $results = $stmt->fetchAll(); |
||
573 | $resultExisting = array_column($results, 'file_id'); |
||
574 | |||
575 | foreach ($audios as $key => &$audio) { |
||
576 | $current_id = $audio->getID(); |
||
577 | if (in_array($current_id, $resultExclude)) { |
||
578 | $output->writeln(" " . $current_id . " - " . $audio->getPath() . " => excluded", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
579 | unset($audios[$key]); |
||
580 | } elseif (in_array($current_id, $resultExisting)) { |
||
581 | if ($this->checkFileChanged($audio)) { |
||
582 | $output->writeln(" " . $current_id . " - " . $audio->getPath() . " => indexed file changed => reindex", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
583 | } else { |
||
584 | $output->writeln(" " . $current_id . " - " . $audio->getPath() . " => already indexed", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
585 | unset($audios[$key]); |
||
586 | } |
||
587 | } |
||
588 | } |
||
589 | $this->numOfSongs = $this->numOfSongs + count($audios); |
||
590 | $output->writeln("Final stream files to be processed: " . count($audios_clean), OutputInterface::VERBOSITY_VERBOSE); |
||
591 | return $audios; |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * Analyze ID3 Tags |
||
596 | * if fseek is not possible, libsmbclient-php is not installed or an external storage is used which does not support this. |
||
597 | * then fallback to slow extraction via tmpfile |
||
598 | * |
||
599 | * @param $audio object |
||
600 | * @param $getID3 object |
||
601 | * @param OutputInterface $output |
||
602 | */ |
||
603 | private function analyze($audio, $getID3, OutputInterface $output = null) |
||
604 | { |
||
605 | $this->ID3Tags = array(); |
||
606 | $ThisFileInfo = array(); |
||
607 | if ($audio->getMimetype() === 'audio/mpegurl' or $audio->getMimetype() === 'audio/x-scpls' or $audio->getMimetype() === 'application/xspf+xml') { |
||
608 | $ThisFileInfo['comments']['genre'][0] = 'Stream'; |
||
609 | $ThisFileInfo['comments']['artist'][0] = 'Stream'; |
||
610 | $ThisFileInfo['comments']['album'][0] = 'Stream'; |
||
611 | $ThisFileInfo['bitrate'] = 0; |
||
612 | $ThisFileInfo['playtime_string'] = 0; |
||
613 | } else { |
||
614 | $handle = $audio->fopen('rb'); |
||
615 | if (@fseek($handle, -24, SEEK_END) === 0) { |
||
616 | $ThisFileInfo = $getID3->analyze($audio->getPath(), $audio->getSize(), '', $handle); |
||
617 | } else { |
||
618 | if (!$this->noFseek) { |
||
619 | $output->writeln("Attention: Only slow indexing due to server config. See Audio Player wiki on GitHub for details.", OutputInterface::VERBOSITY_VERBOSE); |
||
620 | $this->logger->debug('Attention: Only slow indexing due to server config. See Audio Player wiki on GitHub for details.', array('app' => 'audioplayer')); |
||
621 | $this->noFseek = true; |
||
622 | } |
||
623 | $fileName = $audio->getStorage()->getLocalFile($audio->getInternalPath()); |
||
624 | $ThisFileInfo = $getID3->analyze($fileName); |
||
625 | |||
626 | if (!$audio->getStorage()->isLocal($audio->getInternalPath())) { |
||
627 | unlink($fileName); |
||
628 | } |
||
629 | } |
||
630 | if ($this->cyrillic === 'checked') $ThisFileInfo = $this->convertCyrillic($ThisFileInfo); |
||
631 | getid3_lib::CopyTagsToComments($ThisFileInfo); |
||
632 | } |
||
633 | $this->ID3Tags = $ThisFileInfo; |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Concert cyrillic characters |
||
638 | * |
||
639 | * @param array $ThisFileInfo |
||
640 | * @return array |
||
641 | */ |
||
642 | private function convertCyrillic($ThisFileInfo) |
||
643 | { |
||
644 | //$this->logger->debug('cyrillic handling activated', array('app' => 'audioplayer')); |
||
645 | // Check, if this tag was win1251 before the incorrect "8859->utf" convertion by the getid3 lib |
||
646 | foreach (array('id3v1', 'id3v2') as $ttype) { |
||
647 | $ruTag = 0; |
||
648 | if (isset($ThisFileInfo['tags'][$ttype])) { |
||
649 | // Check, if this tag was win1251 before the incorrect "8859->utf" convertion by the getid3 lib |
||
650 | foreach (array('album', 'artist', 'title', 'band', 'genre') as $tkey) { |
||
651 | if (isset($ThisFileInfo['tags'][$ttype][$tkey])) { |
||
652 | if (preg_match('#[\\xA8\\B8\\x80-\\xFF]{4,}#', iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $ThisFileInfo['tags'][$ttype][$tkey][0]))) { |
||
653 | $ruTag = 1; |
||
654 | break; |
||
655 | } |
||
656 | } |
||
657 | } |
||
658 | // Now make a correct conversion |
||
659 | if ($ruTag === 1) { |
||
660 | foreach (array('album', 'artist', 'title', 'band', 'genre') as $tkey) { |
||
661 | if (isset($ThisFileInfo['tags'][$ttype][$tkey])) { |
||
662 | $ThisFileInfo['tags'][$ttype][$tkey][0] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $ThisFileInfo['tags'][$ttype][$tkey][0]); |
||
663 | $ThisFileInfo['tags'][$ttype][$tkey][0] = iconv('Windows-1251', 'UTF-8', $ThisFileInfo['tags'][$ttype][$tkey][0]); |
||
664 | } |
||
665 | } |
||
666 | } |
||
667 | } |
||
668 | } |
||
669 | return $ThisFileInfo; |
||
670 | } |
||
671 | |||
672 | /** |
||
673 | * Get specific ID3 tags from array |
||
674 | * |
||
675 | * @param string[] $ID3Value |
||
676 | * @param string $defaultValue |
||
677 | * @return string |
||
678 | */ |
||
679 | private function getID3Value($ID3Value, $defaultValue = null) |
||
680 | { |
||
681 | $c = count($ID3Value); |
||
682 | // \OCP\Util::writeLog('audioplayer', 'album: '.$this->ID3Tags['comments']['album'][0], \OCP\Util::DEBUG); |
||
683 | for ($i = 0; $i < $c; $i++) { |
||
684 | if (isset($this->ID3Tags['comments'][$ID3Value[$i]][0]) and rawurlencode($this->ID3Tags['comments'][$ID3Value[$i]][0]) !== '%FF%FE') { |
||
685 | return $this->ID3Tags['comments'][$ID3Value[$i]][0]; |
||
686 | } elseif ($i === $c - 1 AND $defaultValue !== null) { |
||
687 | return $defaultValue; |
||
688 | } elseif ($i === $c - 1) { |
||
689 | return (string)$this->l10n->t('Unknown'); |
||
690 | } |
||
691 | } |
||
692 | } |
||
693 | |||
694 | /** |
||
695 | * extract cover art from folder or from audio file |
||
696 | * folder/cover.jpg/png |
||
697 | * |
||
698 | * @param object $audio |
||
699 | * @param integer $iAlbumId |
||
700 | * @param integer $parentId |
||
701 | * @param OutputInterface|null $output |
||
702 | * @return boolean|null |
||
703 | */ |
||
704 | private function getAlbumArt($audio, $iAlbumId, $parentId, OutputInterface $output = null) |
||
705 | { |
||
706 | if ($parentId === $this->parentIdPrevious) { |
||
707 | if ($this->folderPicture) { |
||
708 | $output->writeln(" Reusing previous folder image", OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
709 | $this->processImageString($iAlbumId, $this->folderPicture->getContent()); |
||
710 | } elseif (isset($this->ID3Tags['comments']['picture'][0]['data'])) { |
||
711 | $data = $this->ID3Tags['comments']['picture'][0]['data']; |
||
712 | $this->processImageString($iAlbumId, $data); |
||
713 | } |
||
714 | } else { |
||
715 | $this->folderPicture = false; |
||
716 | if ($audio->getParent()->nodeExists('cover.jpg')) { |
||
717 | $this->folderPicture = $audio->getParent()->get('cover.jpg'); |
||
718 | } elseif ($audio->getParent()->nodeExists('Cover.jpg')) { |
||
719 | $this->folderPicture = $audio->getParent()->get('Cover.jpg'); |
||
720 | } elseif ($audio->getParent()->nodeExists('cover.png')) { |
||
721 | $this->folderPicture = $audio->getParent()->get('cover.png'); |
||
722 | } elseif ($audio->getParent()->nodeExists('Cover.png')) { |
||
723 | $this->folderPicture = $audio->getParent()->get('Cover.png'); |
||
724 | } elseif ($audio->getParent()->nodeExists('folder.jpg')) { |
||
725 | $this->folderPicture = $audio->getParent()->get('folder.jpg'); |
||
726 | } elseif ($audio->getParent()->nodeExists('Folder.jpg')) { |
||
727 | $this->folderPicture = $audio->getParent()->get('Folder.jpg'); |
||
728 | } elseif ($audio->getParent()->nodeExists('folder.png')) { |
||
729 | $this->folderPicture = $audio->getParent()->get('folder.png'); |
||
730 | } elseif ($audio->getParent()->nodeExists('Folder.png')) { |
||
731 | $this->folderPicture = $audio->getParent()->get('Folder.png'); |
||
732 | } elseif ($audio->getParent()->nodeExists('front.jpg')) { |
||
733 | $this->folderPicture = $audio->getParent()->get('front.jpg'); |
||
734 | } elseif ($audio->getParent()->nodeExists('Front.jpg')) { |
||
735 | $this->folderPicture = $audio->getParent()->get('Front.jpg'); |
||
736 | } elseif ($audio->getParent()->nodeExists('front.png')) { |
||
737 | $this->folderPicture = $audio->getParent()->get('front.png'); |
||
738 | } elseif ($audio->getParent()->nodeExists('Front.png')) { |
||
739 | $this->folderPicture = $audio->getParent()->get('Front.png'); |
||
740 | } |
||
741 | |||
742 | if ($this->folderPicture) { |
||
743 | $output->writeln(" Alternative album art: " . $this->folderPicture->getInternalPath(), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
744 | $this->processImageString($iAlbumId, $this->folderPicture->getContent()); |
||
745 | } elseif (isset($this->ID3Tags['comments']['picture'])) { |
||
746 | $data = $this->ID3Tags['comments']['picture'][0]['data']; |
||
747 | $this->processImageString($iAlbumId, $data); |
||
748 | } |
||
749 | $this->parentIdPrevious = $parentId; |
||
750 | } |
||
751 | return true; |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * create image string from rawdata and store as album cover |
||
756 | * |
||
757 | * @param integer $iAlbumId |
||
758 | * @param $data |
||
759 | * @return boolean |
||
760 | */ |
||
761 | private function processImageString($iAlbumId, $data) |
||
762 | { |
||
763 | $image = new Image(); |
||
764 | if ($image->loadFromdata($data)) { |
||
765 | if (($image->width() <= 250 && $image->height() <= 250) || $image->centerCrop(250)) { |
||
766 | $imgString = $image->__toString(); |
||
767 | $this->DBController->writeCoverToAlbum($this->userId, $iAlbumId, $imgString); |
||
768 | } |
||
769 | } |
||
770 | return true; |
||
771 | } |
||
772 | |||
773 | /** |
||
774 | * truncates fiels do DB-field size |
||
775 | * |
||
776 | * @param $string |
||
777 | * @param $length |
||
778 | * @param $dots |
||
779 | * @return string |
||
780 | */ |
||
781 | private function truncateStrings($string, $length, $dots = "...") |
||
782 | { |
||
783 | return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string; |
||
784 | } |
||
785 | |||
786 | /** |
||
787 | * validate unsigned int values |
||
788 | * |
||
789 | * @param string $value |
||
790 | * @return int value |
||
791 | */ |
||
792 | private function normalizeInteger($value) |
||
793 | { |
||
794 | // convert format '1/10' to '1' and '-1' to null |
||
795 | $tmp = explode('/', $value); |
||
796 | $tmp = explode('-', $tmp[0]); |
||
797 | $value = $tmp[0]; |
||
798 | if (is_numeric($value) && ((int)$value) > 0) { |
||
799 | $value = (int)$value; |
||
800 | } else { |
||
801 | $value = 0; |
||
802 | } |
||
803 | return $value; |
||
804 | } |
||
805 | |||
806 | /** |
||
807 | * set the timestamp of the last scan to derive changed files |
||
808 | * |
||
809 | */ |
||
810 | private function setScannerTimestamp() |
||
811 | { |
||
812 | $this->configManager->setUserValue($this->userId, $this->appName, 'scanner_timestamp', time()); |
||
813 | } |
||
814 | |||
815 | /** |
||
816 | * @NoAdminRequired |
||
817 | * |
||
818 | * @throws NotFoundException |
||
819 | */ |
||
820 | public function checkNewTracks() |
||
821 | { |
||
822 | // get only the relevant audio files |
||
823 | $output = new NullOutput(); |
||
824 | $this->getAudioObjects($output); |
||
825 | $this->getStreamObjects($output); |
||
826 | return ($this->numOfSongs !== 0); |
||
827 | } |
||
828 | } |
||
829 |