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;
|
2018-08-19 15:26:34 +00:00
|
|
|
use App\Services\MediaSyncService;
|
2022-07-29 10:51:20 +00:00
|
|
|
use App\Values\SyncResult;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Console\Command;
|
2022-07-31 16:45:34 +00:00
|
|
|
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
|
|
|
|
2019-06-30 11:20:40 +00:00
|
|
|
class SyncCommand extends Command
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2016-02-02 07:47:00 +00:00
|
|
|
protected $signature = 'koel:sync
|
2016-03-22 08:22:39 +00:00
|
|
|
{record? : A single watch record. Consult Wiki for more info.}
|
2022-07-07 10:45:47 +00:00
|
|
|
{--ignore= : The comma-separated tags to ignore (exclude) from syncing}
|
2016-03-22 08:22:39 +00:00
|
|
|
{--force : Force re-syncing even unchanged files}';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2018-08-19 15:26:34 +00:00
|
|
|
protected $description = 'Sync songs found in configured directory against the database.';
|
2016-08-17 14:26:07 +00:00
|
|
|
|
2022-07-31 16:45:34 +00:00
|
|
|
private ?string $mediaPath;
|
2022-07-29 10:51:20 +00:00
|
|
|
private ProgressBar $progressBar;
|
2018-08-19 15:26:34 +00:00
|
|
|
|
2022-07-07 10:45:47 +00:00
|
|
|
public function __construct(private MediaSyncService $mediaSyncService)
|
2018-08-19 15:26:34 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2022-07-29 10:51:20 +00:00
|
|
|
|
|
|
|
$this->mediaSyncService->on('paths-gathered', function (array $paths): void {
|
|
|
|
$this->progressBar = new ProgressBar($this->output, count($paths));
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->mediaSyncService->on('progress', [$this, 'onSyncProgress']);
|
2018-08-19 15:26:34 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-07 10:45:47 +00:00
|
|
|
public function handle(): int
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-31 16:45:34 +00:00
|
|
|
$this->mediaPath = $this->getMediaPath();
|
2022-07-07 10:45:47 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
$record = $this->argument('record');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2022-07-07 10:45:47 +00:00
|
|
|
if ($record) {
|
|
|
|
$this->syncSingleRecord($record);
|
|
|
|
} else {
|
2016-02-02 07:47:00 +00:00
|
|
|
$this->syncAll();
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
return self::SUCCESS;
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync all files in the configured media path.
|
|
|
|
*/
|
2022-07-29 10:51:20 +00:00
|
|
|
private function syncAll(): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2022-07-31 16:45:34 +00:00
|
|
|
$this->components->info('Scanning ' . $this->mediaPath);
|
2022-07-29 10:51:20 +00:00
|
|
|
|
|
|
|
// The tags to ignore from syncing.
|
2016-03-22 08:22:39 +00:00
|
|
|
// Notice that this is only meaningful for existing records.
|
2022-07-05 13:47:26 +00:00
|
|
|
// New records will have every applicable field synced in.
|
2022-07-29 10:51:20 +00:00
|
|
|
$ignores = $this->option('ignore') ? explode(',', $this->option('ignore')) : [];
|
|
|
|
|
|
|
|
$results = $this->mediaSyncService->sync($ignores, $this->option('force'));
|
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
|
|
|
*/
|
2022-07-29 10:51:20 +00:00
|
|
|
private function syncSingleRecord(string $record): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2018-08-19 15:26:34 +00:00
|
|
|
$this->mediaSyncService->syncByWatchRecord(new InotifyWatchRecord($record));
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:51:20 +00:00
|
|
|
public function onSyncProgress(SyncResult $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
|
|
|
|
2022-07-31 16:45:34 +00:00
|
|
|
$path = trim(Str::replaceFirst($this->mediaPath, '', $result->path), DIRECTORY_SEPARATOR);
|
2016-08-17 14:26:07 +00:00
|
|
|
|
2022-07-31 16:45:34 +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 sync result type: {$result->type}")
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($result->isError()) {
|
|
|
|
$this->output->writeln("<fg=red>$result->error</>");
|
|
|
|
}
|
2016-08-17 14:26:07 +00:00
|
|
|
}
|
2018-08-29 09:41:24 +00:00
|
|
|
|
2022-07-31 16:45:34 +00:00
|
|
|
private function getMediaPath(): string
|
2018-08-29 09:41:24 +00:00
|
|
|
{
|
2022-07-31 16:45:34 +00:00
|
|
|
$path = Setting::get('media_path');
|
|
|
|
|
|
|
|
if ($path) {
|
|
|
|
return $path;
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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');
|
2018-08-29 09:41:24 +00:00
|
|
|
|
|
|
|
if (is_dir($path) && is_readable($path)) {
|
|
|
|
Setting::set('media_path', $path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->error('The path does not exist or is not readable. Try again.');
|
|
|
|
}
|
2022-07-31 16:45:34 +00:00
|
|
|
|
|
|
|
return $path;
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|