2015-12-13 04:42:28 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use App\Models\Setting;
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
use Media;
|
|
|
|
|
|
|
|
|
|
class SyncMedia extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2016-02-02 07:47:00 +00:00
|
|
|
|
protected $signature = 'koel:sync
|
|
|
|
|
{record? : A single fswatch record. Consult Wiki for more info.}';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
|
|
protected $ignored = 0;
|
|
|
|
|
protected $invalid = 0;
|
|
|
|
|
protected $synced = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Sync songs found in configured directory against the database.';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new command instance.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
if (!Setting::get('media_path')) {
|
|
|
|
|
$this->error("Media path hasn't been configured. Exiting.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
protected function syncAll()
|
|
|
|
|
{
|
2015-12-13 04:42:28 +00:00
|
|
|
|
$this->info('Koel syncing started. All we need now is just a little patience…');
|
|
|
|
|
|
|
|
|
|
Media::sync(null, $this);
|
|
|
|
|
|
2015-12-30 07:56:19 +00:00
|
|
|
|
$this->output->writeln("<info>Completed! {$this->synced} new or updated song(s)</info>, "
|
2015-12-13 04:42:28 +00:00
|
|
|
|
."{$this->ignored} unchanged song(s), "
|
|
|
|
|
."and <comment>{$this->invalid} invalid file(s)</comment>.");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* SYNc a sinGLE file or directory. See my awesome pun?
|
|
|
|
|
*
|
|
|
|
|
* @param string|FSWatchRecord $record An fswatch record, in this format:
|
|
|
|
|
* "<changed_path> <event_flag_1>::<event_flag_2>::<event_flag_n>"
|
|
|
|
|
* The fswatch command should look like this:
|
|
|
|
|
* ``` bash
|
|
|
|
|
* $ fswatch -0x --event-flag-separator="::" $MEDIA_PATH \
|
|
|
|
|
* | xargs -0 -n1 -I record php artisan koel:sync record
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @link https://github.com/emcrisostomo/fswatch/wiki/How-to-Use-fswatch
|
|
|
|
|
*/
|
|
|
|
|
public function syngle($record)
|
|
|
|
|
{
|
|
|
|
|
Media::syncFSWatchRecord($record, $this);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Log a song's sync status to console.
|
|
|
|
|
*/
|
|
|
|
|
public function logToConsole($path, $result)
|
|
|
|
|
{
|
|
|
|
|
$name = basename($path);
|
|
|
|
|
|
|
|
|
|
if ($result === true) {
|
|
|
|
|
if ($this->option('verbose')) {
|
|
|
|
|
$this->line("$name has no changes – ignoring");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++$this->ignored;
|
|
|
|
|
} elseif ($result === false) {
|
|
|
|
|
if ($this->option('verbose')) {
|
|
|
|
|
$this->error("$name is not a valid media file");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++$this->invalid;
|
|
|
|
|
} else {
|
|
|
|
|
if ($this->option('verbose')) {
|
|
|
|
|
$this->info("$name synced");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++$this->synced;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|