2018-01-11 12:59:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
2019-11-09 02:52:04 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2024-08-31 12:23:07 +00:00
|
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2018-01-11 12:59:53 +00:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2019-11-09 02:52:04 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
use App\Models\Abstracted\Catalog;
|
2018-01-11 12:59:53 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
class CatalogScan implements ShouldQueue, ShouldBeUnique
|
2018-01-11 12:59:53 +00:00
|
|
|
{
|
2024-08-31 12:23:07 +00:00
|
|
|
use InteractsWithQueue,Queueable,SerializesModels;
|
2018-01-11 12:59:53 +00:00
|
|
|
|
|
|
|
// Our object
|
2024-08-31 12:23:07 +00:00
|
|
|
private Catalog $o;
|
|
|
|
private bool $show_dirty;
|
2018-01-11 12:59:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-08-31 12:23:07 +00:00
|
|
|
public function __construct(Catalog $o,bool $show_dirty=FALSE) {
|
|
|
|
$this->o = $o->withoutRelations();
|
|
|
|
$this->show_dirty = $show_dirty;
|
|
|
|
}
|
|
|
|
|
2024-09-02 12:51:19 +00:00
|
|
|
public function middleware(): array
|
2024-08-31 12:23:07 +00:00
|
|
|
{
|
2024-09-18 03:30:49 +00:00
|
|
|
return [new WithoutOverlapping($this->o::config.'|'.$this->o->id)];
|
2018-01-11 12:59:53 +00:00
|
|
|
}
|
|
|
|
|
2024-09-02 12:51:19 +00:00
|
|
|
/**
|
|
|
|
* Get the unique ID for the job.
|
|
|
|
*/
|
|
|
|
public function uniqueId(): string
|
|
|
|
{
|
|
|
|
return $this->o->id;
|
|
|
|
}
|
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
2024-08-31 12:23:07 +00:00
|
|
|
* @throws \Exception
|
2018-01-11 12:59:53 +00:00
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-09-18 01:50:10 +00:00
|
|
|
Log::info(sprintf('Scanning [%s] (%d)',$this->o->file_name(FALSE),$this->o->id));
|
2019-11-09 02:52:04 +00:00
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
if (! $this->o->isReadable()) {
|
|
|
|
Log::alert(sprintf('Ignoring [%s], it is not readable',$this->o->file_name(FALSE)));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-18 01:36:42 +00:00
|
|
|
// Check the details are valid
|
|
|
|
if ($this->o->file_signature === $this->o->getObjectOriginal('file_signature')) {
|
|
|
|
// For sanity, we'll check a couple of other attrs
|
|
|
|
if (($this->o->width !== $this->o->getObjectOriginal('width')) || ($this->o->height !== $this->o->getObjectOriginal('height'))) {
|
|
|
|
Log::alert(sprintf('Dimensions [%s] (%s x %s) mismatch for [%s]',
|
|
|
|
$this->o->dimensions,
|
|
|
|
$this->o->getObjectOriginal('width'),
|
|
|
|
$this->o->getObjectOriginal('height'),
|
|
|
|
$this->o->file_name(FALSE)));
|
|
|
|
|
|
|
|
throw new \Exception(sprintf('Dimensions [%s] (%s x %s) mismatch for [%s]',
|
|
|
|
$this->o->dimensions,
|
|
|
|
$this->o->getObjectOriginal('width'),
|
|
|
|
$this->o->getObjectOriginal('height'),
|
|
|
|
$this->o->file_name(FALSE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Log::alert(sprintf('File Signature [%s] doesnt match [%s] for [%s]',
|
|
|
|
$x=$this->o->getObjectOriginal('file_signature'),
|
|
|
|
$this->o->file_signature,
|
|
|
|
$this->o->file_name(FALSE)));
|
|
|
|
|
|
|
|
throw new \Exception(sprintf('File Signature [%s] doesnt match [%s] for [%s]',
|
|
|
|
$x,
|
|
|
|
$this->o->file_signature,
|
|
|
|
$this->o->file_name(FALSE)));
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:23:07 +00:00
|
|
|
$this->o->init();
|
|
|
|
|
|
|
|
// If this is a duplicate
|
|
|
|
$x = $this->o->myduplicates()->get();
|
|
|
|
if (count($x)) {
|
|
|
|
foreach ($x as $this->oo) {
|
2024-09-16 13:17:51 +00:00
|
|
|
// And that catalog item is not marked as a duplicate
|
2024-08-31 12:23:07 +00:00
|
|
|
if (! $this->oo->duplicate) {
|
|
|
|
$this->o->duplicate = TRUE;
|
|
|
|
Log::alert(sprintf('Image [%s] marked as a duplicate',$this->o->filename));
|
|
|
|
|
|
|
|
// If the file signature also matches, we'll mark it for deletion
|
|
|
|
if ($this->oo->file_signature && ($this->o->file_signature == $this->oo->file_signature)) {
|
|
|
|
Log::alert(sprintf('Image [%s] marked for deletion',$this->o->filename));
|
|
|
|
$this->o->remove = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->o->scanned = TRUE;
|
|
|
|
|
|
|
|
if ($this->o->getDirty()) {
|
|
|
|
Log::alert(sprintf('Image [%s] metadata changed',$this->o->filename));
|
|
|
|
|
|
|
|
if ($this->show_dirty)
|
|
|
|
dump(['id'=>$this->o->id,'data'=>$this->o->getDirty()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file signature changed, abort the update.
|
2024-09-16 12:10:19 +00:00
|
|
|
if ($this->o->getOriginal('file_signature') && $this->o->wasChanged('file_signature'))
|
|
|
|
throw new \Exception(sprintf('File Signature Changed for [%s] DB: [%s], File: [%s]?',
|
|
|
|
$this->o->file_name(),
|
|
|
|
$this->o->file_signature,
|
|
|
|
$this->o->getOriginal('file_signature'),
|
|
|
|
));
|
2024-08-31 12:23:07 +00:00
|
|
|
|
|
|
|
$this->o->save();
|
2018-01-11 12:59:53 +00:00
|
|
|
}
|
2019-11-09 02:52:04 +00:00
|
|
|
}
|