koel/app/Console/Commands/ScanCommand.php

183 lines
5.8 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Console\Commands;
2016-02-04 15:37:12 +00:00
use App\Libraries\WatchRecord\InotifyWatchRecord;
2015-12-13 04:42:28 +00:00
use App\Models\Setting;
use App\Models\User;
use App\Repositories\UserRepository;
use App\Services\MediaScanner;
use App\Values\ScanConfiguration;
use App\Values\ScanResult;
2015-12-13 04:42:28 +00:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
2022-07-29 10:51:20 +00:00
use RuntimeException;
2018-08-19 15:26:34 +00:00
use Symfony\Component\Console\Helper\ProgressBar;
2015-12-13 04:42:28 +00:00
class ScanCommand extends Command
2015-12-13 04:42:28 +00:00
{
protected $signature = 'koel:scan
2016-03-22 08:22:39 +00:00
{record? : A single watch record. Consult Wiki for more info.}
{--O|owner= : The ID of the user who should own the newly scanned songs. Defaults to the first admin user.}
{--P|private : Whether to make the newly scanned songs private to the user.}
{--V|verbose : Show more details about the scanning process
{--I|ignore=* : The comma-separated tags to ignore (exclude) from scanning}
{--F|force : Force re-scanning even unchanged files}';
2015-12-13 04:42:28 +00:00
protected $description = 'Scan for songs in the configured directory.';
2016-08-17 14:26:07 +00:00
private ?string $mediaPath;
2022-07-29 10:51:20 +00:00
private ProgressBar $progressBar;
2018-08-19 15:26:34 +00:00
public function __construct(private MediaScanner $mediaScanner, private UserRepository $userRepository)
2018-08-19 15:26:34 +00:00
{
parent::__construct();
2022-07-29 10:51:20 +00:00
$this->mediaScanner->on('paths-gathered', function (array $paths): void {
2022-07-29 10:51:20 +00:00
$this->progressBar = new ProgressBar($this->output, count($paths));
});
$this->mediaScanner->on('progress', [$this, 'onScanProgress']);
}
protected function configure(): void
{
parent::configure();
$this->setAliases(['koel:sync']);
2018-08-19 15:26:34 +00:00
}
2015-12-13 04:42:28 +00:00
public function handle(): int
2015-12-13 04:42:28 +00:00
{
$this->mediaPath = $this->getMediaPath();
2024-01-07 12:43:10 +00:00
$config = ScanConfiguration::make(
owner: $this->getOwner(),
// When scanning via CLI, the songs should be public by default, unless explicitly specified otherwise.
makePublic: !$this->option('private'),
ignores: collect($this->option('ignore'))->sort()->values()->all(),
force: $this->option('force')
);
2020-12-22 20:11:22 +00:00
$record = $this->argument('record');
2015-12-13 04:42:28 +00:00
if ($record) {
2024-01-07 12:43:10 +00:00
$this->scanSingleRecord($record, $config);
} else {
2024-01-07 12:43:10 +00:00
$this->scanMediaPath($config);
2016-02-02 07:47:00 +00:00
}
2022-07-29 06:47:10 +00:00
return self::SUCCESS;
2016-02-02 07:47:00 +00:00
}
/**
* Scan all files in the configured media path.
2016-02-02 07:47:00 +00:00
*/
2024-01-07 12:43:10 +00:00
private function scanMediaPath(ScanConfiguration $config): void
2016-02-02 07:47:00 +00:00
{
$this->components->info('Scanning ' . $this->mediaPath);
2022-07-29 10:51:20 +00:00
2024-01-07 12:43:10 +00:00
if ($config->ignores) {
$this->components->info('Ignoring tag(s): ' . implode(', ', $config->ignores));
}
2022-07-29 10:51:20 +00:00
$results = $this->mediaScanner->scan($config);
2016-03-22 08:22:39 +00:00
2022-07-29 10:51:20 +00:00
$this->newLine(2);
$this->components->info('Scanning completed!');
2015-12-13 04:42:28 +00:00
2022-07-29 10:51:20 +00:00
$this->components->bulletList([
"<fg=green>{$results->success()->count()}</> new or updated song(s)",
"<fg=yellow>{$results->skipped()->count()}</> unchanged song(s)",
"<fg=red>{$results->error()->count()}</> invalid file(s)",
]);
2015-12-13 04:42:28 +00:00
}
2016-02-02 07:47:00 +00:00
/**
2016-02-04 15:04:53 +00:00
* @param string $record The watch record.
* As of current we only support inotifywait.
* Some examples:
* - "DELETE /var/www/media/gone.mp3"
* - "CLOSE_WRITE,CLOSE /var/www/media/new.mp3"
* - "MOVED_TO /var/www/media/new_dir"
2016-02-02 07:47:00 +00:00
*
2020-09-06 21:20:42 +00:00
* @see http://man7.org/linux/man-pages/man1/inotifywait.1.html
2016-02-02 07:47:00 +00:00
*/
2024-01-07 12:43:10 +00:00
private function scanSingleRecord(string $record, ScanConfiguration $config): void
2016-02-02 07:47:00 +00:00
{
2024-01-07 12:43:10 +00:00
$this->mediaScanner->scanWatchRecord(new InotifyWatchRecord($record), $config);
2016-02-02 07:47:00 +00:00
}
public function onScanProgress(ScanResult $result): void
2015-12-13 04:42:28 +00:00
{
2022-07-29 10:51:20 +00:00
if (!$this->option('verbose')) {
$this->progressBar->advance();
2015-12-13 04:42:28 +00:00
2022-07-29 10:51:20 +00:00
return;
2015-12-13 04:42:28 +00:00
}
2016-08-17 14:26:07 +00:00
$path = trim(Str::replaceFirst($this->mediaPath, '', $result->path), DIRECTORY_SEPARATOR);
2016-08-17 14:26:07 +00:00
$this->components->twoColumnDetail($path, match (true) {
2022-07-29 10:51:20 +00:00
$result->isSuccess() => "<fg=green>OK</>",
$result->isSkipped() => "<fg=yellow>SKIPPED</>",
$result->isError() => "<fg=red>ERROR</>",
default => throw new RuntimeException("Unknown scan result type: {$result->type}")
2022-07-29 10:51:20 +00:00
});
if ($result->isError()) {
$this->output->writeln("<fg=red>$result->error</>");
}
2016-08-17 14:26:07 +00:00
}
private function getMediaPath(): string
{
$path = Setting::get('media_path');
if ($path) {
return $path;
}
$this->warn("Media path hasn't been configured. Let's set it up.");
while (true) {
2018-09-04 05:42:49 +00:00
$path = $this->ask('Absolute path to your media directory');
if (File::isDirectory($path) && File::isReadable($path)) {
Setting::set('media_path', $path);
break;
}
$this->error('The path does not exist or is not readable. Try again.');
}
return $path;
}
private function getOwner(): User
{
$specifiedOwner = $this->option('owner');
if ($specifiedOwner) {
/** @var User $user */
$user = User::findOr($specifiedOwner, function () use ($specifiedOwner): void {
$this->components->error("User with ID $specifiedOwner does not exist.");
exit(self::INVALID);
});
$this->components->info("Setting owner to $user->name (ID $user->id).");
return $user;
}
$user = $this->userRepository->getDefaultAdminUser();
$this->components->warn(
"No song owner specified. Setting the first admin ($user->name, ID $user->id) as owner."
);
return $user;
}
2015-12-13 04:42:28 +00:00
}