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;
|
2017-06-03 23:21:50 +00:00
|
|
|
|
use App\Models\File;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
|
protected $ignored = 0;
|
|
|
|
|
protected $invalid = 0;
|
|
|
|
|
protected $synced = 0;
|
|
|
|
|
|
2016-08-17 14:26:07 +00:00
|
|
|
|
/**
|
|
|
|
|
* The progress bar.
|
|
|
|
|
*
|
|
|
|
|
* @var \Symfony\Component\Console\Helper\ProgressBar
|
|
|
|
|
*/
|
|
|
|
|
protected $bar;
|
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Sync songs found in configured directory against the database.';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
if (!Setting::get('media_path')) {
|
2017-12-03 12:02:56 +00:00
|
|
|
|
$this->warn("Media path hasn't been configured. Let's set it up.");
|
|
|
|
|
while (true) {
|
|
|
|
|
$path = $this->ask('Absolute path to your media directory:');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
2017-12-03 12:02:56 +00:00
|
|
|
|
if (is_dir($path) && is_readable($path)) {
|
|
|
|
|
Setting::set('media_path', $path);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->error('The path does not exist or not readable. Try again.');
|
|
|
|
|
}
|
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.
|
|
|
|
|
*/
|
|
|
|
|
protected function syncAll()
|
|
|
|
|
{
|
2016-08-17 14:26:07 +00:00
|
|
|
|
$this->info('Koel syncing started.'.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')) : [];
|
|
|
|
|
|
|
|
|
|
Media::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
|
2016-02-02 07:47:00 +00:00
|
|
|
|
*/
|
|
|
|
|
public function syngle($record)
|
|
|
|
|
{
|
2016-02-04 15:37:12 +00:00
|
|
|
|
Media::syncByWatchRecord(new InotifyWatchRecord($record), $this);
|
2016-02-02 07:47:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Log a song's sync status to console.
|
2016-08-03 10:42:11 +00:00
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
2017-06-03 23:22:12 +00:00
|
|
|
|
* @param int $result
|
2016-08-17 14:48:18 +00:00
|
|
|
|
* @param string $reason
|
2015-12-13 04:42:28 +00:00
|
|
|
|
*/
|
2016-08-17 14:48:18 +00:00
|
|
|
|
public function logToConsole($path, $result, $reason = '')
|
2015-12-13 04:42:28 +00:00
|
|
|
|
{
|
|
|
|
|
$name = basename($path);
|
|
|
|
|
|
2017-06-03 23:21:50 +00:00
|
|
|
|
if ($result === File::SYNC_RESULT_UNMODIFIED) {
|
2015-12-13 04:42:28 +00:00
|
|
|
|
if ($this->option('verbose')) {
|
|
|
|
|
$this->line("$name has no changes – ignoring");
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 13:11:45 +00:00
|
|
|
|
$this->ignored++;
|
2017-06-03 23:21:50 +00:00
|
|
|
|
} elseif ($result === File::SYNC_RESULT_BAD_FILE) {
|
2015-12-13 04:42:28 +00:00
|
|
|
|
if ($this->option('verbose')) {
|
2016-08-17 14:48:18 +00:00
|
|
|
|
$this->error("$name is not a valid media file because: ".$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 {
|
|
|
|
|
if ($this->option('verbose')) {
|
|
|
|
|
$this->info("$name synced");
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a progress bar.
|
|
|
|
|
*
|
2016-08-17 14:26:38 +00:00
|
|
|
|
* @param int $max Max steps
|
2016-08-17 14:26:07 +00:00
|
|
|
|
*/
|
|
|
|
|
public function createProgressBar($max)
|
|
|
|
|
{
|
|
|
|
|
$this->bar = $this->getOutput()->createProgressBar($max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the progress bar (advance by 1 step).
|
|
|
|
|
*/
|
|
|
|
|
public function updateProgressBar()
|
|
|
|
|
{
|
|
|
|
|
$this->bar->advance();
|
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
}
|