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-29 09:41:24 +00:00
|
|
|
use App\Repositories\SettingRepository;
|
|
|
|
use App\Services\FileSynchronizer;
|
2018-08-19 15:26:34 +00:00
|
|
|
use App\Services\MediaSyncService;
|
|
|
|
use Exception;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Console\Command;
|
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.}
|
|
|
|
{--tags= : The comma-separated tags to sync into the database}
|
|
|
|
{--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.';
|
|
|
|
private $ignored = 0;
|
|
|
|
private $invalid = 0;
|
|
|
|
private $synced = 0;
|
|
|
|
private $mediaSyncService;
|
2018-08-29 09:41:24 +00:00
|
|
|
private $settingRepository;
|
2016-08-17 14:26:07 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
2018-08-19 15:26:34 +00:00
|
|
|
* @var ProgressBar
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2018-08-19 15:26:34 +00:00
|
|
|
private $progressBar;
|
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
public function __construct(MediaSyncService $mediaSyncService, SettingRepository $settingRepository)
|
2018-08-19 15:26:34 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->mediaSyncService = $mediaSyncService;
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->settingRepository = $settingRepository;
|
2018-08-19 15:26:34 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-19 15:26:34 +00:00
|
|
|
* @throws Exception
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function handle(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2018-08-29 09:41:24 +00:00
|
|
|
$this->ensureMediaPath();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
if (!$record = $this->argument('record')) {
|
|
|
|
$this->syncAll();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->syngle($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync all files in the configured media path.
|
2018-08-19 15:26:52 +00:00
|
|
|
*
|
2018-08-19 15:26:34 +00:00
|
|
|
* @throws Exception
|
2016-02-02 07:47:00 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
protected function syncAll(): void
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
2018-09-04 05:43:12 +00:00
|
|
|
$this->info('Syncing media from '.Setting::get('media_path').PHP_EOL);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
// Get the tags to sync.
|
|
|
|
// Notice that this is only meaningful for existing records.
|
|
|
|
// New records will have every applicable field sync'ed in.
|
|
|
|
$tags = $this->option('tags') ? explode(',', $this->option('tags')) : [];
|
|
|
|
|
2018-08-19 15:26:34 +00:00
|
|
|
$this->mediaSyncService->sync(null, $tags, $this->option('force'), $this);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-08-17 14:26:07 +00:00
|
|
|
$this->output->writeln(
|
|
|
|
PHP_EOL.PHP_EOL
|
|
|
|
."<info>Completed! {$this->synced} new or updated song(s)</info>, "
|
2015-12-13 04:42:28 +00:00
|
|
|
."{$this->ignored} unchanged song(s), "
|
2016-08-17 14:26:07 +00:00
|
|
|
."and <comment>{$this->invalid} invalid file(s)</comment>."
|
|
|
|
);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
/**
|
|
|
|
* SYNc a sinGLE file or directory. See my awesome pun?
|
|
|
|
*
|
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
|
|
|
*
|
2016-02-15 16:39:13 +00:00
|
|
|
* @link http://man7.org/linux/man-pages/man1/inotifywait.1.html
|
2018-08-19 15:26:52 +00:00
|
|
|
*
|
2018-08-19 15:26:34 +00:00
|
|
|
* @throws Exception
|
2016-02-02 07:47:00 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function syngle(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
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Log a song's sync status to console.
|
|
|
|
*/
|
2018-08-29 07:58:46 +00:00
|
|
|
public function logSyncStatusToConsole(string $path, int $result, ?string $reason = null): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$name = basename($path);
|
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
if ($result === FileSynchronizer::SYNC_RESULT_UNMODIFIED) {
|
2017-11-08 13:11:45 +00:00
|
|
|
$this->ignored++;
|
2018-08-29 09:41:24 +00:00
|
|
|
} elseif ($result === FileSynchronizer::SYNC_RESULT_BAD_FILE) {
|
2015-12-13 04:42:28 +00:00
|
|
|
if ($this->option('verbose')) {
|
2018-08-29 09:42:11 +00:00
|
|
|
$this->error(PHP_EOL."'$name' is not a valid media file: ".$reason);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 13:11:45 +00:00
|
|
|
$this->invalid++;
|
2015-12-13 04:42:28 +00:00
|
|
|
} else {
|
2017-11-08 13:11:45 +00:00
|
|
|
$this->synced++;
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 14:26:07 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function createProgressBar(int $max): void
|
2016-08-17 14:26:07 +00:00
|
|
|
{
|
2018-08-19 15:26:34 +00:00
|
|
|
$this->progressBar = $this->getOutput()->createProgressBar($max);
|
2016-08-17 14:26:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
public function advanceProgressBar(): void
|
2016-08-17 14:26:07 +00:00
|
|
|
{
|
2018-08-19 15:26:34 +00:00
|
|
|
$this->progressBar->advance();
|
2016-08-17 14:26:07 +00:00
|
|
|
}
|
2018-08-29 09:41:24 +00:00
|
|
|
|
|
|
|
private function ensureMediaPath(): void
|
|
|
|
{
|
2018-08-31 13:47:15 +00:00
|
|
|
if (Setting::get('media_path')) {
|
2018-08-29 09:41:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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.');
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|