From cf7c9317eb0390902dae2e062da78b3e44f3956a Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 3 Jan 2020 08:04:15 +1100 Subject: [PATCH] Start of console commands enhancements --- app/Console/Commands/CatalogDump.php | 64 +++++++++---------- app/Console/Commands/CatalogScan.php | 53 +++++++-------- .../{PhotoScanAll.php => CatalogScanAll.php} | 19 ++++-- app/Console/Commands/VideoScanAll.php | 62 ------------------ app/Models/Abstracted/Catalog.php | 38 +++++++++++ app/Traits/Type.php | 22 +++++++ 6 files changed, 131 insertions(+), 127 deletions(-) rename app/Console/Commands/{PhotoScanAll.php => CatalogScanAll.php} (65%) delete mode 100644 app/Console/Commands/VideoScanAll.php create mode 100644 app/Traits/Type.php diff --git a/app/Console/Commands/CatalogDump.php b/app/Console/Commands/CatalogDump.php index 2e80eaa..c8d3a29 100644 --- a/app/Console/Commands/CatalogDump.php +++ b/app/Console/Commands/CatalogDump.php @@ -4,42 +4,42 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use App\Traits\Type; + class CatalogDump extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'catalog:dump {type : Photo | Video } {id : Photo ID}'; + use Type; - /** - * The console command description. - * - * @var string - */ - protected $description = 'Scan Photo for metadata'; + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'catalog:dump {type : Photo | Video } {id : Photo ID}'; - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $class = 'App\Model\\'.$this->argument('type'); + /** + * The console command description. + * + * @var string + */ + protected $description = 'Scan Photo for metadata'; - if (! class_exists($class)) - abort(500,sprintf('No class [%s]',$this->argument('type'))); - - $o = $class::findOrFail($this->argument('id')); - - if (! is_readable($o->file_path())) + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() { - $this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path())); - exit; - } + $class = $this->getModelType($this->argument('type')); - print_r($o->properties()); - } -} + $o = $class::findOrFail($this->argument('id')); + + if (! $o->isReadable()) { + $this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path())); + exit; + } + + dump($o->properties()); + } +} \ No newline at end of file diff --git a/app/Console/Commands/CatalogScan.php b/app/Console/Commands/CatalogScan.php index 2558ebe..1b9a899 100644 --- a/app/Console/Commands/CatalogScan.php +++ b/app/Console/Commands/CatalogScan.php @@ -4,37 +4,38 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use App\Traits\Type; + class CatalogScan extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'catalog:scan {type : Photo | Video } {id : Photo ID}'; + use Type; - /** - * The console command description. - * - * @var string - */ - protected $description = 'Scan Photo for metadata'; + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'catalog:scan {type : Photo | Video } {id : Photo ID}'; - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $class = 'App\Models\\'.$this->argument('type'); + /** + * The console command description. + * + * @var string + */ + protected $description = 'Scan Photo for metadata'; - if (! class_exists($class)) - abort(500,sprintf('No class [%s]',$this->argument('type'))); + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $class = $this->getModelType($this->argument('type')); $o = $class::findOrFail($this->argument('id')); - if (! is_readable($o->file_path())) { + if (! $o->isReadable()) { $this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path())); return; } @@ -70,10 +71,10 @@ class CatalogScan extends Command $o->scanned = '1'; if ($o->getDirty()) { - $this->warn(sprintf('Image [%s] metadata changed',$o->file_path())); + $this->warn(sprintf('Image [%s] metadata changed',$o->filename)); dump(['id'=>$o->id,'data'=>$o->getDirty()]); } $o->save(); - } -} + } +} \ No newline at end of file diff --git a/app/Console/Commands/PhotoScanAll.php b/app/Console/Commands/CatalogScanAll.php similarity index 65% rename from app/Console/Commands/PhotoScanAll.php rename to app/Console/Commands/CatalogScanAll.php index ea6d1e1..52e87fb 100644 --- a/app/Console/Commands/PhotoScanAll.php +++ b/app/Console/Commands/CatalogScanAll.php @@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Log; use App\Models\Photo; use App\Jobs\CatalogScan; -class PhotoScanAll extends Command +class CatalogScanAll extends Command { use DispatchesJobs; @@ -18,15 +18,16 @@ class PhotoScanAll extends Command * * @var string */ - protected $signature = 'photo:scanall '. - '{--scanned : Rescan Scanned Photos}'; + protected $signature = 'catalog:scanall'. + ' {type : Photo | Video }'. + ' {--scanned : Rescan Scanned Photos}'; /** * The console command description. * * @var string */ - protected $description = 'Scan Photos'; + protected $description = '(re)Scan Media'; /** * Create a new command instance. @@ -45,15 +46,19 @@ class PhotoScanAll extends Command */ public function handle() { - $o = ($this->option('scanned') ? Photo::NotRemove() : Photo::NotScanned()); + $class = $this->getModelType($this->argument('type')); - $o->each(function ($item) { + if ($this->option('scanned')) { + $class::update(['scanned'=>NULL]); + } + + $class::NotScanned()->each(function ($item) { if ($item->remove) { Log::warning(sprintf('Not scanning [%s], marked for removal',$item->id)); return; } - Log::info(sprintf('%s: Rescanning [%s]',__METHOD__,$item->id)); + Log::info(sprintf('%s: Rescanning [%s]',$item->type,$item->id)); $this->dispatch((new CatalogScan($item))->onQueue('scan')); }); diff --git a/app/Console/Commands/VideoScanAll.php b/app/Console/Commands/VideoScanAll.php deleted file mode 100644 index 294716d..0000000 --- a/app/Console/Commands/VideoScanAll.php +++ /dev/null @@ -1,62 +0,0 @@ -option('scanned') ? Video::NotRemove() : Video::NotScanned()); - - $o->each(function ($item) { - if ($item->remove) { - Log::warning(sprintf('Not scanning [%s], marked for removal',$item->id)); - return; - } - - Log::info(sprintf('%s: Rescanning [%s]',__METHOD__,$item->id)); - $this->dispatch((new CatalogScan($item))->onQueue('scan')); - }); - - Log::info(sprintf('Processed [%s]',$o->count())); - } -} \ No newline at end of file diff --git a/app/Models/Abstracted/Catalog.php b/app/Models/Abstracted/Catalog.php index 88169b6..9eb578a 100644 --- a/app/Models/Abstracted/Catalog.php +++ b/app/Models/Abstracted/Catalog.php @@ -180,8 +180,25 @@ abstract class Catalog extends Model return $format ? date('d-m-Y H:i:s',$t) : $t; } + /** + * Return what the filename should be. + * + * @return string + */ + public function file_name(bool $short=TRUE): string + { + // If the date created is not set, the file name will be based on the ID of the file. + $file = sprintf('%s.%s',((is_null($this->date_created) OR ! $this->date_created) + ? sprintf('UNKNOWN/%07s',$this->file_path_id()) + : sprintf('%s_%03s',$this->date_created->format('Y/m/d-His'),$this->subsectime). + ((! static::$includeSubSecTime OR $this->subsectime) ? '' : sprintf('-%05s',$this->id))),$this->type()); + + return (($short OR preg_match('/^\//',$file)) ? '' : config('photo.dir').DIRECTORY_SEPARATOR).$file; + } + /** * Determine the new name for the image + * @deprecated */ public function file_path($short=FALSE,$new=FALSE) { @@ -304,6 +321,16 @@ abstract class Catalog extends Model return ($this->gps_lat AND $this->gps_lon) ? sprintf('%s/%s',$this->gps_lat,$this->gps_lon) : 'UNKNOWN'; } + /** + * Return if this source file is readable. + * + * @return bool + */ + public function isReadable(): bool + { + return is_readable($this->file_path()); + } + /** * Return an HTML checkbox */ @@ -371,6 +398,17 @@ abstract class Catalog extends Model return TRUE; } + /** + * Flag to indicate if a file should be moved. + * + * @return bool + */ + public function mustMove(): bool + { + dump(['f'=>$this->filename,'fn'=>$this->file_name(),'test'=>($this->filename == $this->file_name())]); + return $this->filename !== $this->file_name(); + } + /** * Get the id of the previous record */ diff --git a/app/Traits/Type.php b/app/Traits/Type.php new file mode 100644 index 0000000..b0c72b4 --- /dev/null +++ b/app/Traits/Type.php @@ -0,0 +1,22 @@ +