2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2016-02-04 15:48:15 +00:00
|
|
|
use App\Events\LibraryChanged;
|
2024-01-04 21:51:32 +00:00
|
|
|
use App\Events\MediaScanCompleted;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Models\Song;
|
2022-07-07 10:45:47 +00:00
|
|
|
use App\Repositories\SettingRepository;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\SongRepository;
|
2024-01-04 21:51:32 +00:00
|
|
|
use App\Values\ScanConfiguration;
|
|
|
|
use App\Values\ScanResult;
|
|
|
|
use App\Values\ScanResultCollection;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Values\WatchRecord\Contracts\WatchRecordInterface;
|
2024-06-04 13:35:00 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-08-24 15:27:19 +00:00
|
|
|
use SplFileInfo;
|
2016-02-04 15:48:15 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2023-12-25 17:15:49 +00:00
|
|
|
use Throwable;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
class MediaScanner
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-29 10:51:20 +00:00
|
|
|
/** @var array<array-key, callable> */
|
|
|
|
private array $events = [];
|
|
|
|
|
2018-08-29 06:15:11 +00:00
|
|
|
public function __construct(
|
2024-04-18 14:36:28 +00:00
|
|
|
private readonly SettingRepository $settingRepository,
|
|
|
|
private readonly SongRepository $songRepository,
|
|
|
|
private readonly FileScanner $fileScanner,
|
2024-06-04 13:35:00 +00:00
|
|
|
private readonly Finder $finder
|
2018-08-29 07:07:44 +00:00
|
|
|
) {
|
2018-08-19 09:05:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
public function scan(ScanConfiguration $config): ScanResultCollection
|
2022-07-07 10:45:47 +00:00
|
|
|
{
|
|
|
|
/** @var string $mediaPath */
|
|
|
|
$mediaPath = $this->settingRepository->getByKey('media_path');
|
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->setSystemRequirements();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
$results = ScanResultCollection::create();
|
2022-07-07 10:45:47 +00:00
|
|
|
$songPaths = $this->gatherFiles($mediaPath);
|
2022-07-29 10:51:20 +00:00
|
|
|
|
|
|
|
if (isset($this->events['paths-gathered'])) {
|
|
|
|
$this->events['paths-gathered']($songPaths);
|
|
|
|
}
|
2017-06-03 23:21:50 +00:00
|
|
|
|
|
|
|
foreach ($songPaths as $path) {
|
2023-12-25 17:15:49 +00:00
|
|
|
try {
|
2024-01-04 21:51:32 +00:00
|
|
|
$result = $this->fileScanner->setFile($path)->scan($config);
|
2023-12-25 17:15:49 +00:00
|
|
|
} catch (Throwable) {
|
2024-01-04 21:51:32 +00:00
|
|
|
$result = ScanResult::error($path, 'Possible invalid file');
|
2023-12-25 17:15:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:51:20 +00:00
|
|
|
$results->add($result);
|
2017-06-03 23:21:50 +00:00
|
|
|
|
2022-07-29 10:51:20 +00:00
|
|
|
if (isset($this->events['progress'])) {
|
|
|
|
$this->events['progress']($result);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
event(new MediaScanCompleted($results));
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-12-10 15:27:06 +00:00
|
|
|
// Trigger LibraryChanged, so that PruneLibrary handler is fired to prune the lib.
|
2016-02-02 07:47:00 +00:00
|
|
|
event(new LibraryChanged());
|
2022-07-29 10:51:20 +00:00
|
|
|
|
|
|
|
return $results;
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2023-12-25 17:15:49 +00:00
|
|
|
private 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()
|
2024-04-24 20:44:47 +00:00
|
|
|
->ignoreDotFiles((bool) config('koel.ignore_dot_files')) // https://github.com/koel/koel/issues/450
|
2017-04-23 16:01:02 +00:00
|
|
|
->files()
|
|
|
|
->followLinks()
|
2020-10-03 09:25:19 +00:00
|
|
|
->name('/\.(mp3|wav|ogg|m4a|flac|opus)$/i')
|
2017-04-23 16:01:02 +00:00
|
|
|
->in($path)
|
|
|
|
);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
public function scanWatchRecord(WatchRecordInterface $record, ScanConfiguration $config): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("New watch record received: '{$record->getPath()}'");
|
2024-01-04 21:51:32 +00:00
|
|
|
|
|
|
|
if ($record->isFile()) {
|
|
|
|
$this->scanFileRecord($record, $config);
|
|
|
|
} else {
|
|
|
|
$this->scanDirectoryRecord($record, $config);
|
|
|
|
}
|
2017-06-03 23:21:50 +00:00
|
|
|
}
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
private function scanFileRecord(WatchRecordInterface $record, ScanConfiguration $config): void
|
2017-06-03 23:21:50 +00:00
|
|
|
{
|
|
|
|
$path = $record->getPath();
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::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()) {
|
2024-01-04 21:51:32 +00:00
|
|
|
$this->handleNewOrModifiedFileRecord($path, $config);
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
2017-06-03 23:21:50 +00:00
|
|
|
}
|
2016-02-02 07:47:00 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
private function scanDirectoryRecord(WatchRecordInterface $record, ScanConfiguration $config): void
|
2017-06-03 23:21:50 +00:00
|
|
|
{
|
|
|
|
$path = $record->getPath();
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::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()) {
|
2024-01-04 21:51:32 +00:00
|
|
|
$this->handleNewOrModifiedDirectoryRecord($path, $config);
|
2016-02-02 07:47:00 +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
|
|
|
|
{
|
2024-02-04 20:31:01 +00:00
|
|
|
$song = $this->songRepository->findOneByPath($path);
|
2020-12-22 20:11:22 +00:00
|
|
|
|
|
|
|
if ($song) {
|
2018-08-29 09:41:24 +00:00
|
|
|
$song->delete();
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("$path deleted.");
|
2018-08-29 09:41:24 +00:00
|
|
|
} else {
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("$path doesn't exist in our database--skipping.");
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
private function handleNewOrModifiedFileRecord(string $path, ScanConfiguration $config): void
|
2018-08-29 09:41:24 +00:00
|
|
|
{
|
2024-01-04 21:51:32 +00:00
|
|
|
$result = $this->fileScanner->setFile($path)->scan($config);
|
2018-08-29 09:41:24 +00:00
|
|
|
|
2022-07-29 10:51:20 +00:00
|
|
|
if ($result->isSuccess()) {
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("Scanned $path");
|
2018-08-29 09:41:24 +00:00
|
|
|
} else {
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("Failed to scan $path. Maybe an invalid file?");
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleDeletedDirectoryRecord(string $path): void
|
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
$count = Song::query()->inDirectory($path)->delete();
|
2020-12-22 20:11:22 +00:00
|
|
|
|
|
|
|
if ($count) {
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("Deleted $count song(s) under $path");
|
2018-08-29 09:41:24 +00:00
|
|
|
} else {
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("$path is empty--no action needed.");
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
private function handleNewOrModifiedDirectoryRecord(string $path, ScanConfiguration $config): void
|
2018-08-29 09:41:24 +00:00
|
|
|
{
|
2024-01-04 21:51:32 +00:00
|
|
|
$scanResults = ScanResultCollection::create();
|
2023-12-25 17:15:49 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
foreach ($this->gatherFiles($path) as $file) {
|
2023-12-25 17:15:49 +00:00
|
|
|
try {
|
2024-01-04 21:51:32 +00:00
|
|
|
$scanResults->add($this->fileScanner->setFile($file)->scan($config));
|
2023-12-25 17:15:49 +00:00
|
|
|
} catch (Throwable) {
|
2024-01-04 21:51:32 +00:00
|
|
|
$scanResults->add(ScanResult::error($file->getRealPath(), 'Possible invalid file'));
|
2023-12-25 17:15:49 +00:00
|
|
|
}
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:35:00 +00:00
|
|
|
Log::info("Scanned all song(s) under $path");
|
2018-08-29 09:41:24 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
event(new MediaScanCompleted($scanResults));
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
2022-07-29 10:51:20 +00:00
|
|
|
|
|
|
|
public function on(string $event, callable $callback): void
|
|
|
|
{
|
|
|
|
$this->events[$event] = $callback;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|