2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-06-30 11:20:40 +00:00
|
|
|
use App\Console\Commands\SyncCommand;
|
2016-02-04 15:48:15 +00:00
|
|
|
use App\Events\LibraryChanged;
|
2016-02-04 15:04:53 +00:00
|
|
|
use App\Libraries\WatchRecord\WatchRecordInterface;
|
2016-04-17 15:38:06 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
2018-08-31 13:47:15 +00:00
|
|
|
use App\Models\Setting;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Models\Song;
|
2018-08-29 09:41:24 +00:00
|
|
|
use App\Repositories\AlbumRepository;
|
|
|
|
use App\Repositories\ArtistRepository;
|
|
|
|
use App\Repositories\SettingRepository;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\SongRepository;
|
2017-12-09 18:34:27 +00:00
|
|
|
use Exception;
|
2019-01-01 11:53:20 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-08-24 15:27:19 +00:00
|
|
|
use SplFileInfo;
|
2016-02-04 15:48:15 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2018-08-19 15:26:34 +00:00
|
|
|
class MediaSyncService
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
/**
|
2016-03-22 08:22:39 +00:00
|
|
|
* All applicable tags in a media file that we cater for.
|
|
|
|
* Note that each isn't necessarily a valid ID3 tag name.
|
|
|
|
*/
|
2020-06-07 20:43:04 +00:00
|
|
|
public const APPLICABLE_TAGS = [
|
2016-04-17 15:38:06 +00:00
|
|
|
'artist',
|
|
|
|
'album',
|
|
|
|
'title',
|
|
|
|
'length',
|
|
|
|
'track',
|
2017-12-03 10:02:31 +00:00
|
|
|
'disc',
|
2016-04-17 15:38:06 +00:00
|
|
|
'lyrics',
|
|
|
|
'cover',
|
|
|
|
'mtime',
|
2016-06-16 10:17:16 +00:00
|
|
|
'compilation',
|
2016-04-17 15:38:06 +00:00
|
|
|
];
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2018-08-19 09:05:33 +00:00
|
|
|
private $mediaMetadataService;
|
2018-08-29 06:15:11 +00:00
|
|
|
private $songRepository;
|
|
|
|
private $helperService;
|
2018-08-29 09:41:24 +00:00
|
|
|
private $fileSynchronizer;
|
|
|
|
private $finder;
|
|
|
|
private $artistRepository;
|
|
|
|
private $albumRepository;
|
|
|
|
private $settingRepository;
|
2018-08-31 13:47:15 +00:00
|
|
|
private $logger;
|
2018-08-29 06:15:11 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
MediaMetadataService $mediaMetadataService,
|
|
|
|
SongRepository $songRepository,
|
2018-08-29 09:41:24 +00:00
|
|
|
ArtistRepository $artistRepository,
|
|
|
|
AlbumRepository $albumRepository,
|
|
|
|
SettingRepository $settingRepository,
|
|
|
|
HelperService $helperService,
|
|
|
|
FileSynchronizer $fileSynchronizer,
|
2018-08-31 13:47:15 +00:00
|
|
|
Finder $finder,
|
2019-01-01 11:53:20 +00:00
|
|
|
LoggerInterface $logger
|
2018-08-29 07:07:44 +00:00
|
|
|
) {
|
2018-08-19 09:05:33 +00:00
|
|
|
$this->mediaMetadataService = $mediaMetadataService;
|
2018-08-29 06:15:11 +00:00
|
|
|
$this->songRepository = $songRepository;
|
|
|
|
$this->helperService = $helperService;
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->fileSynchronizer = $fileSynchronizer;
|
|
|
|
$this->finder = $finder;
|
|
|
|
$this->artistRepository = $artistRepository;
|
|
|
|
$this->albumRepository = $albumRepository;
|
|
|
|
$this->settingRepository = $settingRepository;
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger = $logger;
|
2018-08-19 09:05:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
/**
|
|
|
|
* Tags to be synced.
|
|
|
|
*
|
|
|
|
* @var array
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2016-03-22 08:22:39 +00:00
|
|
|
protected $tags = [];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync the media. Oh sync the media.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string> $tags The tags to sync.
|
|
|
|
* Only taken into account for existing records.
|
|
|
|
* New records will have all tags synced in regardless.
|
|
|
|
* @param bool $force Whether to force syncing even unchanged files
|
2020-09-06 21:20:42 +00:00
|
|
|
* @param SyncCommand $syncCommand the SyncMedia command object, to log to console if executed by artisan
|
2017-12-09 18:34:27 +00:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function sync(
|
|
|
|
?string $mediaPath = null,
|
|
|
|
array $tags = [],
|
|
|
|
bool $force = false,
|
2019-06-30 11:20:40 +00:00
|
|
|
?SyncCommand $syncCommand = null
|
2018-08-24 15:27:19 +00:00
|
|
|
): void {
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->setSystemRequirements();
|
2016-03-22 08:22:39 +00:00
|
|
|
$this->setTags($tags);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$results = [
|
2017-06-03 23:21:50 +00:00
|
|
|
'success' => [],
|
|
|
|
'bad_files' => [],
|
|
|
|
'unmodified' => [],
|
2015-12-13 04:42:28 +00:00
|
|
|
];
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
$songPaths = $this->gatherFiles($mediaPath ?: Setting::get('media_path'));
|
2018-09-03 12:41:49 +00:00
|
|
|
|
|
|
|
if ($syncCommand) {
|
|
|
|
$syncCommand->createProgressBar(count($songPaths));
|
|
|
|
}
|
2017-06-03 23:21:50 +00:00
|
|
|
|
|
|
|
foreach ($songPaths as $path) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$result = $this->fileSynchronizer->setFile($path)->sync($this->tags, $force);
|
2017-06-03 23:21:50 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
switch ($result) {
|
|
|
|
case FileSynchronizer::SYNC_RESULT_SUCCESS:
|
|
|
|
$results['success'][] = $path;
|
2017-06-03 23:21:50 +00:00
|
|
|
break;
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
case FileSynchronizer::SYNC_RESULT_UNMODIFIED:
|
|
|
|
$results['unmodified'][] = $path;
|
2017-06-03 23:21:50 +00:00
|
|
|
break;
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2017-06-03 23:21:50 +00:00
|
|
|
default:
|
2018-08-29 09:41:24 +00:00
|
|
|
$results['bad_files'][] = $path;
|
2017-06-03 23:21:50 +00:00
|
|
|
break;
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($syncCommand) {
|
2018-08-24 15:27:19 +00:00
|
|
|
$syncCommand->advanceProgressBar();
|
2018-08-29 09:41:24 +00:00
|
|
|
$syncCommand->logSyncStatusToConsole($path, $result, $this->fileSynchronizer->getSyncError());
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete non-existing songs.
|
2018-08-29 09:41:24 +00:00
|
|
|
$hashes = array_map(function (string $path): string {
|
|
|
|
return $this->helperService->getFileHash($path);
|
2017-06-03 23:21:50 +00:00
|
|
|
}, array_merge($results['unmodified'], $results['success']));
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-09-26 07:32:16 +00:00
|
|
|
Song::deleteWhereIDsNotIn($hashes);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
// Trigger LibraryChanged, so that TidyLibrary handler is fired to, erm, tidy our library.
|
|
|
|
event(new LibraryChanged());
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
/**
|
|
|
|
* Gather all applicable files in a given directory.
|
|
|
|
*
|
|
|
|
* @param string $path The directory's full path
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @return array<SplFileInfo>
|
2016-02-02 07:47:00 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function gatherFiles(string $path): array
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2017-04-23 16:01:02 +00:00
|
|
|
return iterator_to_array(
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->finder->create()
|
2017-04-23 16:01:02 +00:00
|
|
|
->ignoreUnreadableDirs()
|
|
|
|
->ignoreDotFiles((bool) config('koel.ignore_dot_files')) // https://github.com/phanan/koel/issues/450
|
|
|
|
->files()
|
|
|
|
->followLinks()
|
|
|
|
->name('/\.(mp3|ogg|m4a|flac)$/i')
|
|
|
|
->in($path)
|
|
|
|
);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function syncByWatchRecord(WatchRecordInterface $record): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2018-09-03 12:41:49 +00:00
|
|
|
$this->logger->info("New watch record received: '{$record->getPath()}'");
|
2017-06-03 23:21:50 +00:00
|
|
|
$record->isFile() ? $this->syncFileRecord($record) : $this->syncDirectoryRecord($record);
|
|
|
|
}
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
private function syncFileRecord(WatchRecordInterface $record): void
|
2017-06-03 23:21:50 +00:00
|
|
|
{
|
|
|
|
$path = $record->getPath();
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("'$path' is a file.");
|
2017-01-06 03:04:08 +00:00
|
|
|
|
2017-06-03 23:21:50 +00:00
|
|
|
if ($record->isDeleted()) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->handleDeletedFileRecord($path);
|
2020-12-22 20:11:22 +00:00
|
|
|
} elseif ($record->isNewOrModified()) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->handleNewOrModifiedFileRecord($path);
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
2017-06-03 23:21:50 +00:00
|
|
|
}
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
private function syncDirectoryRecord(WatchRecordInterface $record): void
|
2017-06-03 23:21:50 +00:00
|
|
|
{
|
|
|
|
$path = $record->getPath();
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("'$path' is a directory.");
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2016-02-04 15:04:53 +00:00
|
|
|
if ($record->isDeleted()) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->handleDeletedDirectoryRecord($path);
|
2016-02-04 15:04:53 +00:00
|
|
|
} elseif ($record->isNewOrModified()) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->handleNewOrModifiedDirectoryRecord($path);
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
2016-03-22 08:22:39 +00:00
|
|
|
* Construct an array of tags to be synced into the database from an input array of tags.
|
|
|
|
* If the input array is empty or contains only invalid items, we use all tags.
|
2016-08-02 07:03:19 +00:00
|
|
|
* Otherwise, we only use the valid items in it.
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string> $tags
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function setTags(array $tags = []): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2018-09-03 12:41:49 +00:00
|
|
|
$this->tags = array_intersect($tags, self::APPLICABLE_TAGS) ?: self::APPLICABLE_TAGS;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
// We always keep track of mtime.
|
2016-08-03 10:42:11 +00:00
|
|
|
if (!in_array('mtime', $this->tags, true)) {
|
2016-03-22 08:22:39 +00:00
|
|
|
$this->tags[] = 'mtime';
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function tidy(): void
|
2016-04-17 15:38:06 +00:00
|
|
|
{
|
2018-08-29 09:41:24 +00:00
|
|
|
$inUseAlbums = $this->albumRepository->getNonEmptyAlbumIds();
|
2016-04-17 15:38:06 +00:00
|
|
|
$inUseAlbums[] = Album::UNKNOWN_ID;
|
2016-09-26 07:32:16 +00:00
|
|
|
Album::deleteWhereIDsNotIn($inUseAlbums);
|
2016-04-17 15:38:06 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
$inUseArtists = $this->artistRepository->getNonEmptyArtistIds();
|
2016-04-17 15:38:06 +00:00
|
|
|
$inUseArtists[] = Artist::UNKNOWN_ID;
|
|
|
|
$inUseArtists[] = Artist::VARIOUS_ID;
|
2016-09-26 07:32:16 +00:00
|
|
|
Artist::deleteWhereIDsNotIn(array_filter($inUseArtists));
|
2016-04-17 15:38:06 +00:00
|
|
|
}
|
2018-08-29 09:41:24 +00:00
|
|
|
|
|
|
|
private function setSystemRequirements(): void
|
|
|
|
{
|
|
|
|
if (!app()->runningInConsole()) {
|
|
|
|
set_time_limit(config('koel.sync.timeout'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config('koel.memory_limit')) {
|
2020-12-22 20:11:22 +00:00
|
|
|
ini_set('memory_limit', config('koel.memory_limit') . 'M');
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleDeletedFileRecord(string $path): void
|
|
|
|
{
|
2020-12-22 20:11:22 +00:00
|
|
|
$song = $this->songRepository->getOneByPath($path);
|
|
|
|
|
|
|
|
if ($song) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$song->delete();
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("$path deleted.");
|
2018-08-29 09:41:24 +00:00
|
|
|
event(new LibraryChanged());
|
|
|
|
} else {
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("$path doesn't exist in our database--skipping.");
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleNewOrModifiedFileRecord(string $path): void
|
|
|
|
{
|
|
|
|
$result = $this->fileSynchronizer->setFile($path)->sync($this->tags);
|
|
|
|
|
|
|
|
if ($result === FileSynchronizer::SYNC_RESULT_SUCCESS) {
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("Synchronized $path");
|
2018-08-29 09:41:24 +00:00
|
|
|
} else {
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("Failed to synchronized $path. Maybe an invalid file?");
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event(new LibraryChanged());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleDeletedDirectoryRecord(string $path): void
|
|
|
|
{
|
2020-12-22 20:11:22 +00:00
|
|
|
$count = Song::inDirectory($path)->delete();
|
|
|
|
|
|
|
|
if ($count) {
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("Deleted $count song(s) under $path");
|
2018-08-29 09:41:24 +00:00
|
|
|
|
|
|
|
event(new LibraryChanged());
|
|
|
|
} else {
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("$path is empty--no action needed.");
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleNewOrModifiedDirectoryRecord(string $path): void
|
|
|
|
{
|
|
|
|
foreach ($this->gatherFiles($path) as $file) {
|
|
|
|
$this->fileSynchronizer->setFile($file)->sync($this->tags);
|
|
|
|
}
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->logger->info("Synced all song(s) under $path");
|
2018-08-29 09:41:24 +00:00
|
|
|
|
|
|
|
event(new LibraryChanged());
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|