Major refactor of photo processing, video processing still to do
This commit is contained in:
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
use App\Jobs\CatalogScan;
|
||||
use App\Traits\Type;
|
||||
@@ -13,14 +16,18 @@ class CatalogScanAll extends Command
|
||||
{
|
||||
use DispatchesJobs,Type;
|
||||
|
||||
private int|bool $depth = true;
|
||||
protected const chunk_size = 5;
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'catalog:scanall'.
|
||||
' {type : Photo | Video }'.
|
||||
' {--scanned : Rescan Scanned Photos}';
|
||||
protected $signature = 'catalog:scanall'
|
||||
.' {type : Photo | Video }'
|
||||
.' {--i|ignore : Ignore missing files}'
|
||||
.' {--s|scan : Force Rescan of all files }';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
@@ -30,40 +37,128 @@ class CatalogScanAll extends Command
|
||||
protected $description = '(re)Scan Media';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
* @return int
|
||||
*/
|
||||
public function __construct()
|
||||
public function handle(): int
|
||||
{
|
||||
parent::__construct();
|
||||
$started = Carbon::now();
|
||||
$class = $this->getModelType($this->argument('type'));
|
||||
|
||||
Log::info('Scanning disk: '.Storage::disk('nas')->path(''));
|
||||
|
||||
$c = 0;
|
||||
// Scan files in dir, and make sure file lives in DB, (touch it if it does), otherwise create it
|
||||
foreach (Storage::disk('nas')->directories($class::dir_prefix()) as $dir) {
|
||||
Log::info(sprintf(' - DIR: %s',$dir));
|
||||
|
||||
// Take x files at a time and check the DB
|
||||
foreach ($this->files($dir,$class::config,$class::dir_prefix())->chunk(self::chunk_size) as $chunk) {
|
||||
$list = $class::whereIn('filename',$chunk)->get();
|
||||
|
||||
// If there is a new file found it wont be in the DB
|
||||
if ($list->count() !== self::chunk_size)
|
||||
foreach ($chunk->diff($list->pluck('filename')) as $file) {
|
||||
Log::info(sprintf('Found new file [%s] - queueing scan',$file));
|
||||
|
||||
$o = new $class;
|
||||
$o->filename = $file;
|
||||
$o->file_signature = $o->getObjectOriginal('file_signature');
|
||||
$o->save();
|
||||
|
||||
CatalogScan::dispatch($o)
|
||||
->onQueue('scan');
|
||||
|
||||
$c++;
|
||||
}
|
||||
|
||||
foreach ($list as $o) {
|
||||
// Check the details are valid
|
||||
if ($o->file_signature === $o->getObjectOriginal('file_signature')) {
|
||||
// For sanity, we'll check a couple of other attrs
|
||||
if (($o->width !== $o->getObjectOriginal('width')) || ($o->height !== $o->getObjectOriginal('height')))
|
||||
Log::alert(sprintf('Dimensions [%s] (%s x %s) mismatch for [%s]',
|
||||
$o->dimensions,
|
||||
$o->getObjectOriginal('width'),
|
||||
$o->getObjectOriginal('height'),
|
||||
$o->file_name(FALSE)));
|
||||
|
||||
} else {
|
||||
Log::alert(sprintf('File Signature [%s] doesnt match [%s] for [%s]',
|
||||
$o->getObjectOriginal('file_signature'),
|
||||
$o->file_signature,
|
||||
$o->file_name(FALSE)));
|
||||
}
|
||||
|
||||
if ($o->signature !== $o->getObjectOriginal('signature')) {
|
||||
Log::notice(sprintf('Updating image signature for [%s] to [%s] was [%s]',$o->filename,$o->signature,$o->getObjectOriginal('signature')));
|
||||
|
||||
$o->signature = $o->getObjectOriginal('signature');
|
||||
}
|
||||
|
||||
if ($o->isDirty())
|
||||
$o->save();
|
||||
else
|
||||
$o->touch();
|
||||
|
||||
if ($this->option('scan')) {
|
||||
Log::info(sprintf('Forcing re-scan of [%s] - queued',$o->filename));
|
||||
|
||||
CatalogScan::dispatch($o)
|
||||
->onQueue('scan');
|
||||
}
|
||||
|
||||
$c++;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Log::info('Checking for missing files');
|
||||
|
||||
// Find DB records before $started, check they exist (they shouldnt), and delete if not
|
||||
if (! $this->option('ignore'))
|
||||
foreach ($class::select(['id','filename'])->where('updated_at','<',$started)->cursor() as $o)
|
||||
Log::error(sprintf('It appears that file [%s] is missing (%d)',$o->filename,$o->id));
|
||||
|
||||
Log::info(sprintf('Processed [%s]',$c));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
* Recursively find files that we should catalog
|
||||
*
|
||||
* @return mixed
|
||||
* @param string $dir Directory to get files from
|
||||
* @param string $type Configuration key to refer to config())
|
||||
* @param string $prefix Remove the prefix from the filename
|
||||
* @return Collection
|
||||
*/
|
||||
public function handle()
|
||||
public function files(string $dir,string $type,string $prefix): Collection
|
||||
{
|
||||
$class = $this->getModelType($this->argument('type'));
|
||||
$files = collect(Storage::disk('nas')->files($dir))
|
||||
->map(fn($item)=>preg_replace('#^'.$prefix.'#','',$item))
|
||||
->filter(function($item) use ($type) {
|
||||
return ((! ($x=strrpos($item,'.')))
|
||||
|| (! in_array(strtolower(substr($item,$x+1)),config($type.'.import.accepted'))))
|
||||
? NULL
|
||||
: $item;
|
||||
});
|
||||
|
||||
if ($this->option('scanned')) {
|
||||
$class::whereNotNull('scanned')
|
||||
->update(['scanned'=>NULL]);
|
||||
}
|
||||
if (! $this->depth)
|
||||
return $files;
|
||||
|
||||
$c = 0;
|
||||
$class::NotScanned()->each(function ($item) use ($c) {
|
||||
if ($item->remove) {
|
||||
Log::warning(sprintf('Not scanning [%s], marked for removal',$item->id));
|
||||
return;
|
||||
}
|
||||
if (is_numeric($this->depth))
|
||||
$this->depth--;
|
||||
|
||||
$this->dispatch((new CatalogScan($item))->onQueue('scan'));
|
||||
$c++;
|
||||
});
|
||||
foreach (Storage::disk('nas')->directories($dir) as $dir)
|
||||
$files = $files->merge($this->files($dir,$type,$prefix));
|
||||
|
||||
Log::info(sprintf('Processed [%s]',$c));
|
||||
if (is_numeric($this->depth))
|
||||
$this->depth++;
|
||||
|
||||
return $files;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user