2018-01-11 12:59:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
use Carbon\Carbon;
|
2018-01-11 12:59:53 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2024-08-31 12:23:07 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2019-11-09 03:58:15 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-08-31 12:23:07 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-11-09 03:58:15 +00:00
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
use App\Jobs\CatalogScan;
|
2020-01-02 21:21:55 +00:00
|
|
|
use App\Traits\Type;
|
2018-01-11 12:59:53 +00:00
|
|
|
|
2020-01-02 21:04:15 +00:00
|
|
|
class CatalogScanAll extends Command
|
2018-01-11 12:59:53 +00:00
|
|
|
{
|
2020-01-02 21:21:55 +00:00
|
|
|
use DispatchesJobs,Type;
|
2018-01-11 12:59:53 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
private int|bool $depth = true;
|
|
|
|
protected const chunk_size = 5;
|
|
|
|
|
2019-11-09 03:58:15 +00:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2024-08-31 12:23:07 +00:00
|
|
|
protected $signature = 'catalog:scanall'
|
|
|
|
.' {type : Photo | Video }'
|
|
|
|
.' {--i|ignore : Ignore missing files}'
|
|
|
|
.' {--s|scan : Force Rescan of all files }';
|
2019-11-09 03:58:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-01-02 21:04:15 +00:00
|
|
|
protected $description = '(re)Scan Media';
|
2019-11-09 03:58:15 +00:00
|
|
|
|
|
|
|
/**
|
2024-08-31 12:23:07 +00:00
|
|
|
* Execute the console command.
|
2019-11-09 03:58:15 +00:00
|
|
|
*
|
2024-08-31 12:23:07 +00:00
|
|
|
* @return int
|
2019-11-09 03:58:15 +00:00
|
|
|
*/
|
2024-08-31 12:23:07 +00:00
|
|
|
public function handle(): int
|
2019-11-09 03:58:15 +00:00
|
|
|
{
|
2024-08-31 12:23:07 +00:00
|
|
|
$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;
|
2019-11-09 03:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-31 12:23:07 +00:00
|
|
|
* Recursively find files that we should catalog
|
2019-11-09 03:58:15 +00:00
|
|
|
*
|
2024-08-31 12:23:07 +00:00
|
|
|
* @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
|
2019-11-09 03:58:15 +00:00
|
|
|
*/
|
2024-08-31 12:23:07 +00:00
|
|
|
public function files(string $dir,string $type,string $prefix): Collection
|
2019-11-09 03:58:15 +00:00
|
|
|
{
|
2024-08-31 12:23:07 +00:00
|
|
|
$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;
|
|
|
|
});
|
2019-11-09 03:58:15 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
if (! $this->depth)
|
|
|
|
return $files;
|
2020-01-02 21:04:15 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
if (is_numeric($this->depth))
|
|
|
|
$this->depth--;
|
2018-01-11 12:59:53 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
foreach (Storage::disk('nas')->directories($dir) as $dir)
|
|
|
|
$files = $files->merge($this->files($dir,$type,$prefix));
|
2019-11-09 03:58:15 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
if (is_numeric($this->depth))
|
|
|
|
$this->depth++;
|
|
|
|
|
|
|
|
return $files;
|
2019-11-09 03:58:15 +00:00
|
|
|
}
|
|
|
|
}
|