Major refactor of photo processing, video processing still to do

This commit is contained in:
Deon George 2024-08-31 22:23:07 +10:00
parent 2d04c8ccbb
commit 9208ddf779
27 changed files with 3581 additions and 1017 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
class PostgresBytea implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
// For stream resources, we need to fseek in case we've already read it.
if (is_resource($value)) {
rewind($value);
$value = stream_get_contents($value);
}
return hex2bin($value);
}
/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return bin2hex($value);
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Traits\Type;
class CatalogDump extends Command
{
use Type;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'catalog:dump {type : Photo | Video } {id : Photo ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scan Photo for metadata';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$class = $this->getModelType($this->argument('type'));
$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());
}
}

View File

@ -4,6 +4,7 @@ namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Jobs\CatalogScan as Job;
use App\Traits\Type;
class CatalogScan extends Command
@ -25,7 +26,7 @@ class CatalogScan extends Command
*
* @var string
*/
protected $description = 'Scan Photo for metadata';
protected $description = 'Scan Photo/Video for metadata';
/**
* Execute the console command.
@ -38,54 +39,6 @@ class CatalogScan extends Command
$o = $class::findOrFail($this->argument('id'));
if (! $o->isReadable()) {
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
return;
}
$o->setDateCreated();
$o->setSubSecTime();
$o->setSignature();
$o->setMakeModel();
$o->setLocation();
$o->setHeightWidth();
$o->setThumbnail();
// If this is a duplicate
$x = $o->myduplicates()->get();
if (count($x)) {
foreach ($x as $oo) {
// And that photo is not marked as a duplicate
if (! $oo->duplicate) {
$o->duplicate = '1';
$this->warn(sprintf('Image [%s] marked as a duplicate',$o->filename));
// If the file signature also matches, we'll mark it for deletion
if ($oo->file_signature AND $o->file_signature == $oo->file_signature) {
$this->warn(sprintf('Image [%s] marked for deletion',$o->filename));
$o->remove = '1';
}
break;
}
}
}
$o->scanned = '1';
if ($o->getDirty()) {
$this->warn(sprintf('Image [%s] metadata changed',$o->filename));
if ($this->option('dirty'))
dump(['id'=>$o->id,'data'=>$o->getDirty()]);
}
// If the file signature changed, abort the update.
if ($o->getOriginal('file_signature') AND $o->wasChanged('file_signature')) {
dump(['old'=>$o->getOriginal('file_signature'),'new'=>$o->file_signature]);
abort(500,'File Signature Changed?');
}
$o->save();
return Job::dispatchSync($o);
}
}

View File

@ -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;
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\CatalogVerify as Job;
class CatalogVerify extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'catalog:verify'.
' {type : Photo | Video }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Verify media on disk and in the DB';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if ($this->argument('type')) {
Job::dispatch($this->argument('type'))->onQueue('scan');
} else {
}
}
}

View File

@ -1,107 +0,0 @@
<?php
namespace App\Console\Commands;
use Log;
use Illuminate\Console\Command;
use App\Model\Photo;
class PhotoUpdate extends Command
{
use \App\Traits\Files;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'photo:update
{--dir= : Directory to Parse}
{--file= : File to import}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update Signatures';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')]);
if (! count($files))
exit;
// Show a progress bar
$bar = $this->output->createProgressBar(count($files));
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$c = 0;
foreach ($files as $file)
{
$bar->advance();
if (preg_match('/@__thumb/',$file) OR preg_match('/\/._/',$file))
{
$this->warn(sprintf('Ignoring file [%s]',$file));
continue;
}
if (! in_array(strtolower(pathinfo($file,PATHINFO_EXTENSION)),config('photo.import.accepted')))
{
$this->warn(sprintf('Ignoring [%s]',$file));
continue;
}
if ($this->option('verbose'))
$this->info(sprintf('Processing file [%s]',$file));
$c++;
$po = Photo::where('filename',$file)->first();
if (is_null($po))
{
$this->error(sprintf('File is not in the database [%s]?',$file));
Log::error(sprintf('%s: File is not in the database [%s]?',__METHOD__,$file));
continue;
}
$po->signature = $po->property('signature');
try {
$po->thumbnail = exif_thumbnail($po->file_path());
} catch (\Exception $e) {
// @todo Couldnt get the thumbnail, so we should create one.
}
if ($po->isDirty())
{
if (count($po->getDirty()) > 1 OR ! array_key_exists('signature',$po->getDirty()))
$this->error(sprintf('More than the signature changed for [%s] (%s)?',$po->id,join('|',array_keys($po->getDirty()))));
$this->info(sprintf('Signature update for [%s]',$po->id));
$po->save();
}
}
$bar->finish();
return $this->info(sprintf('Images processed: %s',$c));
}
}

View File

@ -38,14 +38,9 @@ class PhotoController extends Controller
]);
}
public function info(Photo $o)
{
return view('photo.view',['o'=>$o]);
}
public function thumbnail(Photo $o)
{
return response($o->thumbnail(TRUE))
return response($o->thumbnail())
->header('Content-Type','image/jpeg');
}
@ -54,9 +49,16 @@ class PhotoController extends Controller
$o->remove = NULL;
$o->save();
return redirect()->action('PhotoController@info',[$o->id]);
return redirect()
->action('PhotoController@info',[$o->id]);
}
/**
* Render the photo to the browser
*
* @param Photo $o
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\Response
*/
public function view(Photo $o)
{
return response($o->image())

View File

@ -2,39 +2,92 @@
namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Artisan;
use App\Models\Abstracted\Catalog;
class CatalogScan extends Job implements ShouldQueue
class CatalogScan implements ShouldQueue, ShouldBeUnique
{
use InteractsWithQueue, SerializesModels;
use InteractsWithQueue,Queueable,SerializesModels;
// Our object
private $o = NULL;
private Catalog $o;
private bool $show_dirty;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Catalog $o) {
$this->o = $o;
public function __construct(Catalog $o,bool $show_dirty=FALSE) {
$this->o = $o->withoutRelations();
$this->show_dirty = $show_dirty;
}
public function xmiddleware(): array
{
return [new WithoutOverlapping($this->o->id)];
}
/**
* Execute the job.
*
* @return void
* @throws \Exception
*/
public function handle()
{
Log::info(sprintf('%s: Scanning [%s|%s]',__METHOD__,$this->o->objecttype(),$this->o->id));
Artisan::call('catalog:scan',['type'=>$this->o->objecttype(),'id'=>$this->o->id]);
if (! $this->o->isReadable()) {
Log::alert(sprintf('Ignoring [%s], it is not readable',$this->o->file_name(FALSE)));
return;
}
$this->o->init();
// If this is a duplicate
$x = $this->o->myduplicates()->get();
if (count($x)) {
foreach ($x as $this->oo) {
// And that photo is not marked as a duplicate
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.
if ($this->o->getOriginal('file_signature') && $this->o->wasChanged('file_signature')) {
dump(['old'=>$this->o->getOriginal('file_signature'),'new'=>$this->o->file_signature]);
abort(500,'File Signature Changed?');
}
$this->o->save();
}
}

View File

@ -1,100 +0,0 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use App\Traits\Files;
use App\Traits\Type;
class CatalogVerify extends Job implements ShouldQueue
{
use Dispatchable, Queueable, InteractsWithQueue, SerializesModels, Type, Files;
// What we should verify
private $type = NULL;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(string $type) {
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(sprintf('%s: Scanning [%s]',__METHOD__,$this->type));
// Go through DB and verify files exist
$class = $this->getModelType($this->type);
$good = $bad = $ugly = 0;
$class::select('*')->each(function($o) use ($good,$bad,$ugly) {
if (! file_exists($o->file_name_current(FALSE))) {
Log::error(sprintf('Media doesnt exist: [%s] (%d:%s)',$this->type,$o->id,$o->file_name_current(FALSE)));
$bad++;
return;
}
if (($x=md5_file($o->file_name_current(FALSE))) !== $o->file_signature) {
Log::error(sprintf('Media MD5 doesnt match DB: [%s] (%d:%s) [%s:%s]',$this->type,$o->id,$o->file_name_current(FALSE),$x,$o->file_signature));
$ugly++;
return;
}
$good++;
});
Log::info(sprintf('DB Media Verify Complete: [%s] Good: [%d], Missing: [%d], Changed: [%d]',$this->type,$good,$bad,$ugly));
// Go through filesystem and see that a record exists in the DB, if not add it.
$parentdir = config($this->type.'.dir');
$good = $bad = 0;
foreach ($this->dirlist($parentdir) as $dir) {
foreach ($this->getFiles(['dir'=>$dir,'file'=>NULL],$this->type) as $file) {
if (! $class::where('filename',$file)->count()) {
$bad++;
Log::error(sprintf('File not in DB: [%s] (%s)',$this->type,$file));
} else {
$good++;
}
}
}
Log::info(sprintf('File Media Verify Complete: [%s] Good: [%d], Not In DB: [%d]',$this->type,$good,$bad));
}
/**
* Recursively get a list of dirs
* @param $path
* @return \Illuminate\Support\Collection
*/
private function dirlist($path)
{
$list = collect();
$list->push($path);
foreach (glob($path.'/*',GLOB_ONLYDIR) as $dir) {
foreach ($this->dirlist($dir) as $subdir)
$list->push($subdir);
}
return $list;
}
}

View File

@ -5,20 +5,56 @@ namespace App\Models\Abstracted;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use App\Models\{Person,Software,Tag};
use App\Casts\PostgresBytea;
use App\Models\{Make,Person,Software,Tag};
abstract class Catalog extends Model
{
protected static $includeSubSecTime = FALSE;
protected $dates = ['created','created_manual'];
protected $casts = [
'created_manual' => 'datetime',
'subsectime' => 'int',
'thumbnail' => PostgresBytea::class,
];
protected const fs = 'nas';
private ?string $move_reason;
protected array $init = [];
/* STATIC */
/**
* Return the prefix for the file path - dependant on the object
*
* @return string
*/
public static function dir_prefix(): string
{
return config(static::config.'.dir').'/';
}
/**
* Trim a string
*
* @param string $string
* @param int $chrs
* @return string
* @todo This should go in as a helper
*/
public static function stringtrim(string $string,int $chrs=6)
{
return sprintf('%s...%s',substr($string,0,$chrs),substr($string,-1*$chrs));
}
/* RELATIONS */
/**
* People in Multimedia Object
*
@ -49,19 +85,19 @@ abstract class Catalog extends Model
return $this->belongsToMany(Tag::class);
}
/* SCOPES */
/**
* Find records marked as duplicate
*
* @param $query
* @return mixed
*/
public function scopeDuplicates($query) {
$query->notRemove()
public function scopeDuplicates($query)
{
return $query->notRemove()
->where('duplicate',TRUE)
->where(function($q) {
$q->Where('ignore_duplicate','<>',TRUE)
->orWhereNull('ignore_duplicate');
});
->where(fn($q)=>$q->where('ignore_duplicate','<>',TRUE)->orWhereNull('ignore_duplicate'));
}
/**
@ -78,29 +114,20 @@ abstract class Catalog extends Model
$query->where('id','<>',$this->attributes['id']);
// Skip ignore dups
$query->where(function($q) {
$q->whereNull('ignore_duplicate')
->orWhere('ignore_duplicate','=',0);
});
$query->where(fn($q)=>$q->whereNull('ignore_duplicate')->orWhere('ignore_duplicate',FALSE));
// Exclude those marked as remove
$query->where(function ($q) {
$q->where('remove','<>',TRUE)
->orWhere('remove','=',NULL);
});
$query->where(fn($q)=>$q->where('remove','<>',TRUE));
$query->where(function ($q) {
$q->where('signature','=',$this->attributes['signature'])
$query->where(function($q) {
$q->when($this->attributes['signature'],fn($q)=>$q->where('signature','=',$this->attributes['signature']))
->orWhere('file_signature','=',$this->attributes['file_signature'])
// Where the signature is the same
->orWhere(function($q) {
// Or they have the same time taken with the same camera
if ($this->attributes['created'] AND $this->software_id) {
$q->where(function ($q) {
$q->where('created','=',$this->attributes['created'])
->orWhere('created_manual','=',$this->attributes['created']);
});
$q->where(fn($q)=>$q->where('created','=',$this->attributes['created'])->orWhere('created_manual','=',$this->attributes['created']));
if (static::$includeSubSecTime)
$q->where('subsectime','=',Arr::get($this->attributes,'subsectime'));
@ -108,10 +135,7 @@ abstract class Catalog extends Model
$q->where('software_id','=',$this->attributes['software_id']);
} elseif ($this->attributes['created_manual'] AND $this->software_id) {
$q->where(function ($q) {
$q->where('created','=',$this->attributes['created_manual'])
->orWhere('created_manual','=',$this->attributes['created_manual']);
});
$q->where(fn($q)=>$q->where('created','=',$this->attributes['created_manual'])->orWhere('created_manual','=',$this->attributes['created_manual']));
if (static::$includeSubSecTime)
$q->where('subsectime','=',Arr::get($this->attributes,'subsectime'));
@ -120,6 +144,8 @@ abstract class Catalog extends Model
}
});
});
return $query;
}
/**
@ -129,15 +155,11 @@ abstract class Catalog extends Model
*/
public function scopeNotDuplicate($query)
{
return $query->where(function($query)
{
$query->where('duplicate','<>',TRUE)
return $query->where(
fn($q)=>$q->where('duplicate','<>',TRUE)
->orWhere('duplicate','=',NULL)
->orWhere(function($q) {
$q->where('duplicate','=',TRUE)
->where('ignore_duplicate','=',TRUE);
});
});
->orWhere(fn($q)=>$q->where('duplicate','=',TRUE)->where('ignore_duplicate','=',TRUE))
);
}
/**
@ -147,11 +169,7 @@ abstract class Catalog extends Model
*/
public function scopeNotRemove($query)
{
return $query->where(function($query)
{
$query->where('remove','<>',TRUE)
->orWhere('remove','=',NULL);
});
return $query->where(fn($q)=>$q->where('remove','<>',TRUE)->orWhere('remove','=',NULL));
}
/**
@ -161,27 +179,33 @@ abstract class Catalog extends Model
*/
public function scopeNotScanned($query)
{
return $query->where(function($query)
{
$query->where('scanned','<>',TRUE)
->orWhere('scanned','=',NULL);
});
return $query->where(fn($q)=>$q->where('scanned','<>',TRUE)->orWhere('scanned','=',NULL));
}
// Children objects must inherit this methods
abstract public function setLocation();
abstract public function setSubSecTime();
abstract public function setThumbnail();
abstract public function getHtmlImageURL();
/* ABSTRACTS */
abstract public function getObjectOriginal(string $property): mixed;
/* ATTRIBUTES */
/**
* Date the multimedia was created
* Return the time the media was created on the device
*
* This will be (in priority order)
* + the value of created_manual (Carbon)
* + the value of created
*
* @param string|null $date
* @return Carbon|null
*/
public function date_taken(): string
public function getCreatedAttribute(string $date=NULL): ?Carbon
{
return $this->created
? $this->created->format('Y-m-d H:i:s').(static::$includeSubSecTime ? sprintf('.%03d',$this->subsectime) : '')
: 'UNKNOWN';
$result = $this->created_manual ?: ($date ? Carbon::create($date) : NULL);
if ($result && static::$includeSubSecTime)
$result->microseconds($this->subsectime*1000);
return $result ?: $this->getObjectOriginal('creation_date');
}
/**
@ -189,7 +213,7 @@ abstract class Catalog extends Model
*
* @return string
*/
public function device(): string
public function getDeviceAttribute(): string
{
$result = '';
@ -209,61 +233,105 @@ abstract class Catalog extends Model
}
/**
* Return the date of the file
* @todo return Carbon date or NULL
* Return item dimensions
*/
public function file_date($type,$format=FALSE)
public function getDimensionsAttribute(): string
{
if (! is_readable($this->file_path()))
return NULL;
switch ($type)
{
case 'a': $t = fileatime($this->file_path());
break;
case 'c': $t = filectime($this->file_path());
break;
case 'm': $t = filemtime($this->file_path());
break;
}
return $format ? date('d-m-Y H:i:s',$t) : $t;
return $this->width.'x'.$this->height;
}
/**
* Return what the filename should be.
* Return the file size
*
* @return int|null
*/
public function getFileSizeAttribute(): ?int
{
return (! $this->isReadable()) ? NULL : filesize($this->file_name(FALSE));
}
public function getGPSAttribute(): ?string
{
return ($this->gps_lat && $this->gps_lon)
? sprintf('%s/%s',$this->gps_lat,$this->gps_lon)
: NULL;
}
/* METHODS */
/**
* Date the multimedia was created
*
* @deprecated use $this->created
*/
public function date_taken(): string
{
Log::alert(__METHOD__.' deprecated');
return $this->created;
}
/** @deprecated use $this->device */
public function device(): string
{
Log::alert(__METHOD__.' deprecated');
return $this->device;
}
/**
* Return the date of the file
*/
public function file_date(string $type): ?Carbon
{
if (! $this->isReadable())
return NULL;
$t = NULL;
switch ($type) {
case 'a': $t = fileatime($this->file_name(FALSE));
break;
case 'c': $t = filectime($this->file_name(FALSE));
break;
case 'm': $t = filemtime($this->file_name(FALSE));
break;
}
return $t ? Carbon::createfromTimestamp($t) : NULL;
}
/**
* Return the filename.
* If short is TRUE, it is the filename that it should be called (and can be compared to $this->filename)
* If short is FALSE, it is the true path of the actual file
*
* @param bool $short
* @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->created)
? sprintf('UNKNOWN/%07s',$this->file_path_id())
: $this->created->format('Y/m/d-His').
((! is_null($this->subsectime)) ? sprintf('_%03d',$this->subsectime) : '' ).
sprintf('-%05s',$this->id))
,$this->type()
);
if ($short || preg_match('#^/#',$this->filename)) {
// 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->created)
? sprintf('UNKNOWN/%07s',$this->file_path_id())
: $this->created->format('Y/m/d-His').
($this->subsectime ? sprintf('_%03d',$this->subsectime) : '' ).
sprintf('-%05s',$this->id)),
$this->type()
);
return (($short OR preg_match('/^\//',$file)) ? '' : config($this->type.'.dir').DIRECTORY_SEPARATOR).$file;
}
return $file;
/**
* Return the current filename
*
* @return string
*/
public function file_name_current(bool $short=TRUE): string
{
return (($short OR preg_match('/^\//',$this->filename)) ? '' : config($this->type.'.dir').DIRECTORY_SEPARATOR).$this->filename;
} else
return Storage::disk(self::fs)
->path(config(static::config.'.dir').DIRECTORY_SEPARATOR.$this->filename);
}
/**
* Determine the new name for the image
* @deprecated use $this->file_name()
* @deprecated use $this->file_name(FALSE) to determine the name, and file_name(TRUE) to determine the new name
*/
public function file_path($short=FALSE,$new=FALSE)
{
@ -277,62 +345,37 @@ abstract class Catalog extends Model
/**
* Calculate a file path ID based on the id of the file
*
* We use this when we cannot determine the create time of the image
*/
public function file_path_id($sep=3,$depth=9): string
{
return trim(chunk_split(sprintf("%0{$depth}s",$this->id),$sep,'/'),'/');
}
/**
* Display the file signature
*/
public function file_signature($short=FALSE): string
{
return $short ? static::stringtrim($this->file_signature) : $this->file_signature;
}
/**
* Return the file size
* @deprecated
* @deprecated use $this->getFileSizeAttribute())
*/
public function file_size()
{
return (! is_readable($this->file_path())) ? NULL : filesize($this->file_path());
}
Log::alert(__METHOD__.' deprecated');
public function getCreatedAttribute()
{
return $this->created_manual ?: ($this->attributes['created'] ? $this->asDateTime($this->attributes['created']) : NULL);
}
/**
* Return item dimensions
*/
public function getDimensionsAttribute(): string
{
return $this->width.'x'.$this->height;
return (! is_readable($this->file_name(FALSE))) ? NULL : filesize($this->file_name(FALSE));
}
/**
* Return HTML Checkbox for duplicate
* @deprecated use a component
*/
public function getDuplicateCheckboxAttribute()
{
return $this->HTMLCheckbox('duplicate',$this->id,$this->duplicate);
}
/**
* Return the file size
*
* @return false|int|null
*/
public function getFileSizeAttribute()
{
return (! is_readable($this->file_path())) ? NULL : filesize($this->file_path());
}
/**
* Return HTML Checkbox for flagged
* @deprecated use a component
*/
public function getFlagCheckboxAttribute()
{
@ -341,6 +384,7 @@ abstract class Catalog extends Model
/**
* Return HTML Checkbox for ignore
* @deprecated use a component
*/
public function getIgnoreCheckboxAttribute()
{
@ -348,7 +392,7 @@ abstract class Catalog extends Model
}
/**
* @deprecated
* @deprecated use a component
*/
public function getDuplicateTextAttribute()
{
@ -356,7 +400,7 @@ abstract class Catalog extends Model
}
/**
* @deprecated
* @deprecated use a component
*/
public function getFlagTextAttribute()
{
@ -365,32 +409,167 @@ abstract class Catalog extends Model
/**
* Return HTML Checkbox for remove
* @deprecated use a component
*/
public function getRemoveCheckboxAttribute()
{
return $this->HTMLCheckbox('remove',$this->id,$this->remove);
}
/**
* Return the object type
* @return string
* @deprecated Use objecttype()
*/
public function getTypeAttribute(): string
{
switch(get_class($this)) {
case 'App\Models\Photo': return 'photo';
case 'App\Models\Video': return 'video';
default: return 'unknown';
}
}
/**
* Display the GPS coordinates
* @deprecated use getGPSAttribute()
*/
public function gps(): string
{
return ($this->gps_lat AND $this->gps_lon) ? sprintf('%s/%s',$this->gps_lat,$this->gps_lon) : 'UNKNOWN';
return $this->getGPSAttribute();
}
/**
* Return an HTML checkbox
* @deprecated use a component
*/
protected function HTMLCheckbox($name,$id,$value)
{
return sprintf('<input type="checkbox" name="%s[%s]" value="1"%s>',$name,$id,$value ? ' checked="checked"' : '');
}
/**
* Get ID Info link
* @deprecated use a component
*/
protected function HTMLLinkAttribute($id,$url)
{
return sprintf('<a href="%s" target="%s">%s</a>',url($url,$id),$id,$id);
}
/**
* Set values from the media object
*
* @return void
* @throws \Exception
*/
public function init(): void
{
foreach ($this->init as $item) {
Log::debug(sprintf('Init item [%s]',$item));
switch ($item) {
case 'creation_date':
$this->created = $this->getObjectOriginal('creation_date');
break;
case 'gps':
$this->gps_lat = $this->getObjectOriginal('gps_lat');
$this->gps_lon = $this->getObjectOriginal('gps_lon');
break;
case 'heightwidth':
$this->height = $this->getObjectOriginal('height');
$this->width = $this->getObjectOriginal('width');
break;
case 'signature':
$this->signature = $this->getObjectOriginal('signature');
$this->file_signature = $this->getObjectOriginal('file_signature');
break;
case 'software':
$ma = NULL;
if ($x=$this->getObjectOriginal('make'))
$ma = Make::firstOrCreate([
'name'=>$x,
]);
$mo = \App\Models\Model::firstOrCreate([
'name'=>$this->getObjectOriginal('model') ?: NULL,
'make_id'=>$ma?->id,
]);
$so = Software::firstOrCreate([
'name'=>$this->getObjectOriginal('software') ?: NULL,
'model_id'=>$mo->id,
]);
$this->software_id = $so->id;
case 'subsectime':
$this->subsectime = $this->getObjectOriginal($item);
break;
default:
throw new \Exception('Unknown init item: '.$item);
}
}
$this->custom_init();
Log::debug('Init result',['dirty'=>$this->getDirty()]);
}
/**
* Does the file require moving
*
* @return bool
*/
public function isMoveable(): bool
{
// No change to the name
$this->move_reason = 'Filenames match already';
if ($this->filename === $this->file_name())
return FALSE;
// If there is already a file in the target.
// @todo If the target file is to be deleted, we could move this file
$this->move_reason = 'Target file exists';
if (Storage::disk(self::fs)->exists($this->file_name()))
return FALSE;
// Test if the source is readable
$this->move_reason = 'Source is not readable';
if (! $this->isReadable())
return FALSE;
// Test if the dir is writable (so we can remove the file)
$this->move_reason = 'Source parent dir not writable';
if (! $this->isParentWritable(dirname($this->file_name(FALSE))))
return FALSE;
// Test if the target dir is writable
// @todo The target dir may not exist yet, so we should check that a parent exists and is writable.
$this->move_reason = 'Target parent dir doesnt is not writable';
if (! $this->isParentWritable(dirname($this->file_name(FALSE))))
return FALSE;
// Otherwise we can move it
$this->move_reason = NULL;
return TRUE;
}
public function isMoveableReason(): ?string
{
return isset($this->move_reason) ? $this->move_reason : NULL;
}
/**
* Determine if the parent dir is writable
*
* @param string $dir
* @return bool
*/
public function isParentWritable(string $dir): bool
{
$path = Storage::disk(self::fs)->path($dir);
if (Storage::disk(self::fs)->exists($dir) && is_dir($path) && is_writable($path))
return TRUE;
elseif ($path === dirname($path))
return FALSE;
else
return ($this->isParentWritable(dirname($dir)));
}
/**
@ -400,41 +579,7 @@ abstract class Catalog extends Model
*/
public function isReadable(): bool
{
return is_readable($this->file_path());
}
/**
* Return an HTML checkbox
*/
protected function HTMLCheckbox($name,$id,$value)
{
return sprintf('<input type="checkbox" name="%s[%s]" value="1"%s>',$name,$id,$value ? ' checked="checked"' : '');
}
/**
* Get ID Info link
*/
protected function HTMLLinkAttribute($id,$url)
{
return sprintf('<a href="%s" target="%s">%s</a>',url($url,$id),$id,$id);
}
/**
* Determine if the parent dir is writable
*
* @param $dir
* @return bool
*/
public function isParentWritable($dir): bool
{
if (file_exists($dir) AND is_writable($dir) AND is_dir($dir))
return TRUE;
elseif ($dir == dirname($dir) OR file_exists($dir))
return FALSE;
else
return ($this->isParentWritable(dirname($dir)));
return is_readable($this->file_name(FALSE));
}
/**
@ -445,44 +590,20 @@ abstract class Catalog extends Model
*/
public function moveable()
{
// If the source and target are the same, we dont need to move it
if ($this->file_path() == $this->file_path(FALSE,TRUE))
return FALSE;
// If there is already a file in the target.
// @todo If the target file is to be deleted, we could move this file
if (file_exists($this->file_path(FALSE,TRUE)))
return 1;
// Test if the source is readable
if (! is_readable($this->file_path()))
return 2;
// Test if the dir is writable (so we can remove the file)
if (! $this->isParentWritable(dirname($this->file_path())))
return 3;
// Test if the target dir is writable
// @todo The target dir may not exist yet, so we should check that a parent exists and is writable.
if (! $this->isParentWritable($this->file_path(FALSE,TRUE)))
return 4;
return TRUE;
Log::alert(__METHOD__.' deprecated');
return $this->isMoveable();
}
/**
* Get the id of the previous record
* Get the id of the next record
*/
public function next()
public function next(): ?self
{
return DB::table($this->getTable())
->where('id','>',$this->id)
return static::where('id','>',$this->id)
->orderby('id','ASC')
->first();
}
abstract public function property(string $property);
/**
* Return my class shortname
*/
@ -494,62 +615,21 @@ abstract class Catalog extends Model
/**
* Get the id of the previous record
*/
public function previous()
public function previous(): ?self
{
return DB::table($this->getTable())
->where('id','<',$this->id)
return static::where('id','<',$this->id)
->orderby('id','DESC')
->first();
}
public function setDateCreated()
{
$this->created = $this->property('creationdate') ?: NULL;
}
public function setHeightWidth()
{
$this->height = $this->property('height');
$this->width = $this->property('width');
$this->orientation = $this->property('orientation');
}
public function setSignature()
{
$this->signature = $this->property('signature');
$this->file_signature = md5_file($this->file_path());
}
/**
* Display the media signature
*/
public function signature($short=FALSE)
{
return ($short AND $this->signature) ? static::stringtrim($this->signature) : $this->signature;
}
/**
* Trim a string
*
* @param string $string
* @param int $chrs
* @return string
*/
public static function stringtrim(string $string,int $chrs=6)
{
return sprintf('%s...%s',substr($string,0,$chrs),substr($string,-1*$chrs));
}
/**
* @deprecated
* @param string $string
* @param int $chrs
* @return string
*/
public static function signaturetrim(string $string,int $chrs=6)
{
return static::stringtrim($string,$chrs);
return ($short && $this->signature)
? static::stringtrim($this->signature)
: $this->signature;
}
/**
@ -560,7 +640,8 @@ abstract class Catalog extends Model
return $this->filename !== $this->file_name();
}
protected function TextTrueFalse($value): string
/** @deprecated is this really needed? */
private function TextTrueFalse($value): string
{
return $value ? 'TRUE' : 'FALSE';
}
@ -595,7 +676,7 @@ abstract class Catalog extends Model
->orWhere('remove','=',NULL);
});
// Where the signature is the same
// Where the signalist_duplicatesture is the same
$o->where(function($query)
{
$query->where('signature','=',$this->signature);

View File

@ -2,54 +2,43 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Traits\ForwardsCalls;
use Imagick;
class Photo extends Abstracted\Catalog
{
protected $table = 'photo';
use ForwardsCalls;
public const config = 'photo';
protected static $includeSubSecTime = TRUE;
// Imagick Object
private $_o;
// Imagick Objectfile_name
private ?Imagick $_o;
protected array $init = [
'creation_date',
'gps',
'heightwidth',
'signature',
'software',
'subsectime',
];
// How should the image be rotated, based on the value of orientation
private $_rotate = [
private array $_rotate = [
3=>180,
6=>90,
8=>-90,
];
public function getIDLinkAttribute()
{
return $this->HTMLLinkAttribute($this->id,'p/info');
}
public function getHtmlImageURL(): string
{
return sprintf('<img class="p-3" src="%s">',url('p/thumbnail',$this->id));
}
/**
* Return the image, rotated, minus exif data
*/
public function image()
{
if (is_null($imo = $this->o()))
return NULL;
if (array_key_exists('exif',$imo->getImageProfiles()))
$imo->removeImageProfile('exif');
return $this->rotate($imo);
}
/**
* Calculate the GPS coordinates
*/
public static function latlon(array $coordinate,$hemisphere)
public static function latlon(array $coordinate,string $hemisphere): ?float
{
if (! $coordinate OR ! $hemisphere)
if ((! $coordinate) || (! $hemisphere))
return NULL;
for ($i=0; $i<3; $i++) {
@ -67,29 +56,172 @@ class Photo extends Abstracted\Catalog
list($degrees,$minutes,$seconds) = $coordinate;
$sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
$sign = ($hemisphere === 'W' || $hemisphere === 'S') ? -1 : 1;
return round($sign*($degrees+$minutes/60+$seconds/3600),($degrees>100 ? 3 : 4));
}
/**
* Return an Imagick object or attribute
* Forward calls to Imagick
*
* @param $method
* @param $parameters
* @return mixed|null
*/
protected function o($attr=NULL)
public function __call($method,$parameters) {
if (str_starts_with($method,'Imagick_')) {
$method = preg_replace('/^Imagick_/','',$method);
return $this->o ? $this->forwardCallTo($this->_o,$method,$parameters) : NULL;
} else
return parent::__call($method,$parameters);
}
public function __get($key): mixed
{
if (! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
return NULL;
if ($key === 'o') {
if (isset($this->_o))
return $this->_o;
if (is_null($this->_o))
$this->_o = new \Imagick($this->file_path());
if ((!file_exists($this->file_name(FALSE))) || (!is_readable($this->file_name(FALSE))))
return $this->_o = NULL;
return is_null($attr) ? $this->_o : $this->_o->getImageProperty($attr);
if (!isset($this->_o))
return $this->_o = new Imagick($this->file_name(FALSE));
}
return parent::__get($key);
}
/* ATTRIBUTES */
public function getFileSignatureAttribute(string $val=NULL): string
{
return $val ?: $this->getObjectOriginal('file_signature');
}
public function getHeightAttribute(string $val=NULL): ?int
{
return $val ?: $this->getObjectOriginal('height');
}
public function getOrientationAttribute(int $val=NULL): ?int
{
return $val ?: $this->getObjectOriginal('orientation');
}
public function getSignatureAttribute(string $val=NULL): ?string
{
return $val ?: $this->getObjectOriginal('signature');
}
public function getWidthAttribute(string $val=NULL): ?int
{
return $val ?: $this->getObjectOriginal('width');
}
/* METHODS */
public function custom_init(): void
{
$this->orientation = $this->getObjectOriginal('orientation');
try {
if ($this->isReadable() && $this->o->thumbnailimage(150,150,true)) {
$this->o->setImageFormat('jpg');
$this->thumbnail = $this->o->getImageBlob();
}
} catch (\Exception $e) {
Log::info(sprintf('Unable to create thumbnail for %s (%s)',$this->id,$e->getMessage()));
}
if ($this->thumbnail === FALSE)
$this->thumbnail = NULL;
}
public function getObjectOriginal(string $property): mixed
{
switch ($property) {
case 'creation_date':
if ($this->Imagick_getImageProperty('exif:DateTimeOriginal') === '0000:00:00 00:00:00'
&& $this->Imagick_getImageProperty('exif:DateTime') === '0000:00:00 00:00:00')
return NULL;
$result = Carbon::create($x=
($this->Imagick_getImageProperty('exif:DateTimeOriginal') && ($this->Imagick_getImageProperty('exif:DateTimeOriginal') !== '0000:00:00 00:00:00'))
? $this->Imagick_getImageProperty('exif:DateTimeOriginal').$this->Imagick_getImageProperty('exif:OffsetTimeOriginal')
: $this->Imagick_getImageProperty('exif:DateTime').$this->Imagick_getImageProperty('exif:OffsetTime'));
return $result ?: NULL;
case 'file_signature':
return md5_file($this->file_name(FALSE));
case 'gps_lat':
return self::latlon(preg_split('/,\s?/',$this->Imagick_getImageProperty('exif:GPSLatitude')),$this->Imagick_getImageProperty('exif:GPSLatitudeRef'));
case 'gps_lon':
return self::latlon(preg_split('/,\s?/',$this->Imagick_getImageProperty('exif:GPSLongitude')),$this->Imagick_getImageProperty('exif:GPSLongitudeRef'));
case 'height':
return $this->Imagick_getImageHeight();
case 'identifier':
return NULL;
case 'make':
return $this->Imagick_getImageProperty('exif:Make');
case 'model':
return $this->Imagick_getImageProperty('exif:Model');
case 'orientation':
return $this->Imagick_getImageOrientation();
case 'signature':
return $this->Imagick_getImageSignature();
case 'software':
return $this->Imagick_getImageProperty('exif:Software');
case 'subsectime':
$this->subsectime = (int)$this->Imagick_getImageProperty('exif:SubSecTimeOriginal');
// In case of an error.
if ($this->subsectime > 32767)
$this->subsectime = 32767;
if ($this->subsectime === FALSE)
$this->subsectime = 0;
return $this->subsectime;
case 'width':
return $this->Imagick_getImageWidth();
default:
throw new \Exception('To implement: '.$property);
}
}
/**
* Return the image, rotated
*/
public function image(): ?string
{
$imo = clone($this->o);
return $imo ? $this->rotate($imo) : NULL;
}
/**
* Display the orientation of a photo
*/
public function orientation() {
public function orientation(): string
{
switch ($this->orientation) {
case 1: return 'None!';
case 3: return 'Upside Down';
@ -102,118 +234,36 @@ class Photo extends Abstracted\Catalog
/**
* Rotate the image
*/
private function rotate(\Imagick $imo)
private function rotate(\Imagick $imo,string $format='jpg'): string
{
if (array_key_exists($this->orientation,$this->_rotate))
$imo->rotateImage(new \ImagickPixel('none'),$this->_rotate[$this->orientation]);
$imo->setImageFormat('jpg');
$imo->setImageFormat($format);
if (array_key_exists('exif',$imo->getImageProfiles()))
$imo->removeImageProfile('exif');
return $imo->getImageBlob();
}
public function property(string $property)
{
if (! $this->o())
return NULL;
switch ($property) {
case 'creationdate':
if ($this->property('exif:DateTimeOriginal') == '0000:00:00 00:00:00'
&& $this->property('exif:DateTimeOriginal') == '0000:00:00 00:00:00')
return NULL;
return strtotime(
$this->property('exif:DateTimeOriginal') && $this->property('exif:DateTimeOriginal') != '0000:00:00 00:00:00'
? $this->property('exif:DateTimeOriginal')
: $this->property('exif:DateTime'));
break;
case 'height': return $this->_o->getImageHeight();
case 'orientation': return $this->_o->getImageOrientation();
case 'signature': return $this->_o->getImageSignature();
case 'width': return $this->_o->getImageWidth();
default:
return $this->_o->getImageProperty($property);
}
}
public function properties()
{
return $this->o() ? $this->_o->getImageProperties() : [];
}
public function setLocation()
{
$this->gps_lat = static::latlon(preg_split('/,\s?/',$this->property('exif:GPSLatitude')),$this->property('exif:GPSLatitudeRef'));
$this->gps_lon = static::latlon(preg_split('/,\s?/',$this->property('exif:GPSLongitude')),$this->property('exif:GPSLongitudeRef'));
}
public function setMakeModel()
{
$ma = NULL;
if ($this->property('exif:Make'))
$ma = Make::firstOrCreate([
'name'=>$this->property('exif:Make'),
]);
$mo = Model::firstOrCreate([
'name'=>$this->property('exif:Model') ?: NULL,
'make_id'=>$ma ? $ma->id : NULL,
]);
$so = Software::firstOrCreate([
'name'=>$this->property('exif:Software') ?: NULL,
'model_id'=>$mo->id,
]);
$this->software_id = $so->id;
}
public function setSubSecTime()
{
$this->subsectime = (int)$this->property('exif:SubSecTimeOriginal');
// In case of an error.
if ($this->subsectime > 32767)
$this->subsectime = 32767;
if ($this->subsectime === FALSE)
$this->subsectime = 0;
}
public function setThumbnail()
{
try {
$this->thumbnail = exif_thumbnail($this->file_path());
} catch (\Exception $e) {
// @todo Couldnt get the thumbnail, so we should create one.
Log::info(sprintf('Unable to create thumbnail for %s (%s)',$this->id,$e->getMessage()));
}
if ($this->thumbnail === FALSE)
$this->thumbnail = NULL;
}
/**
* Return the image's thumbnail
*/
public function thumbnail($rotate=TRUE)
public function thumbnail($rotate=TRUE): ?string
{
if (! $this->thumbnail) {
if ($this->isReadable() AND $this->o()->thumbnailimage(200,200,true,false)) {
$this->_o->setImageFormat('jpg');
return $this->_o->getImageBlob();
if ($this->isReadable() && $this->o->thumbnailimage(150,150,true)) {
$this->o->setImageFormat('jpg');
return $this->o->getImageBlob();
} else {
return NULL;
}
}
if (! $rotate OR ! array_key_exists($this->orientation,$this->_rotate) OR ! extension_loaded('imagick'))
if ((! $rotate) || (! array_key_exists($this->orientation,$this->_rotate)) || (! extension_loaded('imagick')))
return $this->thumbnail;
$imo = new \Imagick();
@ -225,10 +275,9 @@ class Photo extends Abstracted\Catalog
/**
* Return the extension of the image
* @todo mime-by-ext?
*/
public function type($mime=FALSE)
public function type(bool $mime=FALSE): string
{
return strtolower($mime ? 'image/jpeg' : pathinfo($this->filename,PATHINFO_EXTENSION));
return strtolower($mime ? mime_content_type($this->file_name(FALSE)) : pathinfo($this->filename,PATHINFO_EXTENSION));
}
}

View File

@ -3,6 +3,7 @@
namespace App\Providers;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Bus\DispatchesJobs;
@ -30,12 +31,14 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
Route::model('po',Photo::class);
// Any photo saved, queue it to be moved.
Photo::saved(function($photo) {
if ($photo->scanned AND ! $photo->duplicate AND ! $photo->remove AND ($x=$photo->moveable()) === TRUE) {
if ($photo->scanned && (! $photo->duplicate) && (! $photo->remove) && ($x=$photo->moveable()) === TRUE) {
Log::info(sprintf('%s: Need to Move [%s]',__METHOD__,$photo->id.'|'.serialize($x)));
$this->dispatch((new PhotoMove($photo))->onQueue('move'));
PhotoMove::dispatch($photo)->onQueue('move');
}
});

View File

@ -12,7 +12,7 @@ trait Type
{
private function getModelType(string $type): string
{
$class = 'App\Models\\'.$type;
$class = 'App\Models\\'.ucfirst(strtolower($type));
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$type));

5
bootstrap/providers.php Normal file
View File

@ -0,0 +1,5 @@
<?php
return [
App\Providers\AppServiceProvider::class,
];

View File

@ -1,71 +1,73 @@
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.3",
"ext-pdo": "*",
"james-heinrich/getid3": "^1.9",
"laravel/framework": "^11.0",
"laravel/ui": "^4.5",
"leenooks/laravel": "^11.1",
"leenooks/passkey": "^0.2"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.6",
"fakerphp/faker": "^1.23",
"laravel/tinker": "^2.9",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^11.0.1",
"spatie/laravel-ignition": "^2.4"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"repositories": {
"leenooks": {
"type": "vcs",
"url": "https://gitea.dege.au/laravel/leenooks.git"
},
"passkey": {
"type": "vcs",
"url": "https://gitea.dege.au/laravel/passkey.git"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.3",
"ext-pdo": "*",
"ext-imagick": "*",
"ext-pgsql": "*",
"james-heinrich/getid3": "^1.9",
"laravel/framework": "^11.0",
"laravel/ui": "^4.5",
"leenooks/laravel": "^11.1",
"leenooks/passkey": "^0.2"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.6",
"fakerphp/faker": "^1.23",
"laravel/tinker": "^2.9",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^11.0.1",
"spatie/laravel-ignition": "^2.4"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"repositories": {
"leenooks": {
"type": "vcs",
"url": "https://gitea.dege.au/laravel/leenooks.git"
},
"passkey": {
"type": "vcs",
"url": "https://gitea.dege.au/laravel/passkey.git"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}

View File

@ -2,75 +2,81 @@
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application for file storage.
|
*/
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application for file storage.
|
*/
'default' => env('FILESYSTEM_DISK', 'local'),
'default' => env('FILESYSTEM_DISK', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Below you may configure as many filesystem disks as necessary, and you
| may even configure multiple disks for the same driver. Examples for
| most supported storage drivers are configured here for reference.
|
| Supported drivers: "local", "ftp", "sftp", "s3"
|
*/
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Below you may configure as many filesystem disks as necessary, and you
| may even configure multiple disks for the same driver. Examples for
| most supported storage drivers are configured here for reference.
|
| Supported drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
'nas' => [
'driver' => 'local',
'root' => storage_path('nas'),
'throw' => false,
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];

View File

@ -1,7 +1,7 @@
<?php
return [
'dir'=>'/photos',
'dir'=>'Photos',
'import'=>[
'accepted'=>['jpg','jpeg','heic'],
],

View File

@ -1,7 +1,7 @@
<?php
return [
'dir'=>'/videos',
'dir'=>'HomeMovies',
'import'=>[
'accepted'=>['m4v','mov','mp4','avi'],
],

File diff suppressed because it is too large Load Diff

View File

@ -20,14 +20,13 @@
<span class="pagination justify-content-center">{{ $catalog->links() }}</span>
<form action="{{ $return }}" method="POST">
{{ csrf_field() }}
@csrf
<input type="hidden" name="page" value="{{ $catalog->hasMorePages() ? $catalog->currentPage()+1 : NULL }}">
<input type="hidden" name="type" value="{{ $type }}">
@include('catalog.widgets.duplicates')
<div class="pb-2"><button class="btn btn-sm btn-danger">Confirm Delete</button></div>
<input type="hidden" name="page" value="{{ $catalog->hasMorePages() ? $catalog->currentPage()+1 : NULL }}">
</form>
@else
NONE!

View File

@ -20,14 +20,14 @@
<span class="pagination justify-content-center">{{ $catalog->links() }}</span>
<form action="{{ $return }}" method="POST">
{{ csrf_field() }}
@csrf
<input type="hidden" name="type" value="{{ $type }}">
<input type="hidden" name="page" value="{{ $catalog->currentPage() }}">
@include('catalog.widgets.duplicates')
<div class="pb-2"><button class="btn btn-sm btn-primary">Update</button></div>
<input type="hidden" name="page" value="{{ $catalog->currentPage() }}">
</form>
@else
NONE!

View File

@ -1,4 +1,4 @@
<table class="table">
<table class="table table-light">
<thead>
<tr>
<th class="w-50">Remove</th>
@ -6,20 +6,18 @@
</tr>
</thead>
@php
// Remember what we have rendered
$rendered = collect();
@endphp
<!-- Remember what we have rendered -->
@php($rendered = collect())
<tbody>
@foreach ($catalog as $o)
@if($rendered->search($o->id)) @continue @endif
@php($rendered->push($o->id))
@continue($rendered->search($o))
@php($rendered->push($o))
<tbody>
<tr>
<td>
<input type="hidden" name="items[]" value="{{ $o->id }}">
@include($o->type.'.widgets.thumbnail',['o'=>$o])
@include($o::config.'.widgets.thumbnail',['o'=>$o,'reference'=>$o->newInstance()])
</td>
@if (! ($d=$o->myduplicates()->get())->count())
@ -30,15 +28,15 @@
@else
@foreach($d as $item)
@if($rendered->search($item->id)) @continue @endif
@php($rendered->push($item->id))
@continue($rendered->search($item))
@php($rendered->push($item))
<td>
<input type="hidden" name="items[]" value="{{ $item->id }}">
@include($item->type.'.widgets.thumbnail',['o'=>$item])
@include($item::config.'.widgets.thumbnail',['o'=>$item,'reference'=>$o])
</td>
@endforeach
@endif
</tr>
</tbody>
@endforeach
</tbody>
</table>

View File

@ -0,0 +1 @@
<a href="{{ url('p/info',$id) }}" target="{{ $id }}">{{ $id }}</a>

View File

@ -0,0 +1,3 @@
<a href="{{ url('p/view',$id) }}" target="{{ $id }}">
<img class="p-3" src="{{ url('p/thumbnail',$id) }}">
</a>

View File

@ -1,117 +1,137 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Photo - {{ $o->id }}
Photo - {{ $po->id }}
@endsection
@section('contentheader_title')
Photo #{{ $o->id }}
Photo #{{ $po->id }}
@endsection
@section('contentheader_description')
@if(! $o->scanned)<button class="btn btn-sm btn-info">TO SCAN</button>@endif
@if($o->duplicate)<button class="btn btn-sm btn-warning">DUPLICATE</button>@endif
@if($o->ignore_duplicate)<button class="btn btn-sm btn-secondary">DUPLICATE IGNORE</button>@endif
@if($o->remove)<button class="btn btn-sm btn-danger">PENDING DELETE</button>@endif
@endsection
@section('page_title')
#{{ $o->id }}
#{{ $po->id }}
@endsection
@section('main-content')
<div class="row">
<div class="col-3">
<a href="{{ url('p/view',$o->id) }}">{!! $o->getHtmlImageURL() !!}</a>
<x-thumbnail :id="$po->id"/>
<span class="pagination justify-content-center">
<nav>
<div class="pagination justify-content-center">
<ul class="pagination">
<li class="page-item @if(! $x=$o->previous())disabled @endif" aria-disabled="@if(! $x)true @else false @endif" aria-label="&laquo; Previous">
<li class="page-item @disabled(! $x=$po->previous())">
<a class="page-link" href="{{ $x ? url('p/info',$x->id) : '#' }}">&lt;&lt;</a>
</li>
<li class="page-item active" aria-current="page"><span class="page-link">{{ $o->id }}</span></li>
<li class="page-item active" aria-current="page">
<span class="page-link">{{ $po->id }}</span>
</li>
<li class="page-item @if(! $x=$o->next())disabled @endif" aria-disabled="@if(! $x)true @else false @endif" aria-label="&laquo; Previous">
<li class="page-item @disabled(! $x=$po->next())">
<a class="page-link" href="{{ $x ? url('p/info',$x->id) : '#' }}">&gt;&gt;</a>
</li>
</ul>
</nav>
</span>
</div>
</div>
<div class="col-9">
<div class="dl-horizontal">
<dt>Signature</dt><dd>{{ $o->signature(TRUE) }}</dd>
<dt>Filename</dt><dd>{{ $o->filename }}<dd>
<div class="card">
<div class="card-body">
<div class="float-right">
@if(! $po->scanned)<button class="btn btn-sm btn-info">TO SCAN</button>@endif
@if($po->duplicate)<button class="btn btn-sm btn-warning">DUPLICATE</button>@endif
@if($po->ignore_duplicate)<button class="btn btn-sm btn-secondary">DUPLICATE IGNORE</button>@endif
@if($po->remove)<button class="btn btn-sm btn-danger">PENDING DELETE</button>@endif
</div>
<div class="dl-horizontal">
<dt>Signature</dt><dd>{{ $po->signature(TRUE) }}</dd>
<dt>Filename</dt><dd>{{ $po->filename }}<dd>
@if ($o->shouldMove())
<dt>NEW Filename</dt><dd>{{ $o->file_name() }}<dd>
@endif
@if($po->shouldMove())
<dt>NEW Filename</dt><dd>{{ $po->file_name() }}<dd>
@endif
<dt>Size</dt><dd>{{ $o->file_size() }}<dd>
<dt>Dimensions</dt><dd>{{ $o->dimensions }} @ {{ $o->orientation }}<dd>
<hr>
<dt>Date Taken</dt><dd>{{ $o->date_taken() }}<dd>
<dt>Camera</dt><dd>{{ $o->device() }}<dd>
<hr>
<dt>Location</dt>
<dd>
@if($o->gps() == 'UNKNOWN')
UNKNOWN
@else
<div id="map" style="width: 400px; height: 300px"></div>
@endif
</dd>
<hr>
<dt>Exif Data</dt><dd>
<table>
@foreach ($o->properties() as $k => $v)
<tr><th>{{ $k }}<><td>{{ $v }}<td></tr>
@endforeach
</table>
</dd>
<hr>
@if(($x=$o->myduplicates()->get())->count())
<dt>Duplicates</dt>
<dd>
@foreach($x as $oo)
@if(! $loop->first)| @endif
{!! $oo->id_link !!}
@endforeach
</dd>
@endif
<dt>Size</dt><dd>{{ number_format($po->file_size,0) }}<dd>
<dt>Dimensions</dt><dd>{{ $po->dimensions }} @ {{ $po->orientation }}<dd>
<hr>
<dt>Date Taken</dt><dd>{{ $po->created?->format('Y-m-d H:i:s') }}<dd>
@if($po->scanned)
<dt>Camera</dt><dd>{{ $po->device}}<dd>
<hr>
<dt>Location</dt>
<dd>
@if(! $po->gps)
UNKNOWN
@else
<div id="map" class="w-100" style="height: 30em;"></div>
@endif
</dd>
@endif
@if($x=$po->Imagick_getImageProperties())
<hr>
<dt>Exif Data</dt><dd>
<table class="table table-sm table-striped">
@foreach($x as $k => $v)
<tr>
<th>{{ $k }}</th><td>{{ $v }}<td>
</tr>
@endforeach
</table>
</dd>
@endif
@if(($x=$po->myduplicates()->get())->count())
<hr>
<dt>Duplicates</dt>
<dd>
@foreach($x as $oo)
@if(! $loop->first)| @endif
<x-info :id="$oo->id"/>
@endforeach
</dd>
@endif
</div>
</div>
</div>
@if ($o->remove)
<form action="{{ url('p/undelete',$o->id) }}" method="POST">
@if($po->remove)
<form action="{{ url('p/undelete',$po->id) }}" method="POST">
<button class="btn btn-primary">Undelete</button>
@else
<form action="{{ url('p/delete',$o->id) }}" method="POST">
<form action="{{ url('p/delete',$po->id) }}" method="POST">
<button class="btn btn-danger">Delete</button>
@endif
{{ csrf_field() }}
@csrf
</form>
</div>
</div>
@endsection
@section('page-scripts')
@if($o->gps() !== 'UNKNOWN')
@js('//maps.google.com/maps/api/js?sensor=false')
@if($po->gps)
<script type="text/javascript" src="//unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script>
<link type='text/css' href="//unpkg.com/maplibre-gl/dist/maplibre-gl.css" rel='stylesheet'>
<script type="text/javascript">
var myLatLng = {lat: {{ $o->gps_lat }}, lng: {{ $o->gps_lon }}};
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 16,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
map: map,
position: myLatLng,
const key = 'hspQjLANaPwdHUrvUcsf';
var map = new maplibregl.Map({
container: 'map', // container id
style: '/js/maplibre-style.json', // style URL
center: [{{ $po->gps_lon }}, {{ $po->gps_lat }}], // starting position [lng, lat]
zoom: 12 // starting zoom
});
let marker = new maplibregl.Marker()
.setLngLat([{{ $po->gps_lon }}, {{ $po->gps_lat }}])
.addTo(map);
</script>
@endif
@append

View File

@ -1,5 +1,4 @@
<?php $data = [
'ID'=>['id','idlink'],
'Signature'=>['signature','signature'],
'File Signature'=>['file_signature','file_signature'],
'Date Created'=>['created','created'],
@ -15,27 +14,29 @@
<div class="card card-widget">
<div class="card-header">
<div class="user-block">
<i class="float-left fa fa-2x fa-camera"></i><span class="username"><a href="{{ url('p/info',$o->id) }}">#{{ $o->id }} - {{ \Illuminate\Support\Str::limit($o->filename,12).\Illuminate\Support\Str::substr($o->filename,-16) }}</a></span>
<span class="description">{{ $o->created ? $o->created->toDateTimeString() : '-' }} [{{ $o->device() }}]</span>
<i class="fas fa-2x fa-camera float-left"></i><span class="username"><a href="{{ url('p/info',$o->id) }}">#{{ $o->id }} - {{ Str::limit($o->filename,12).Str::substr($o->filename,-16) }}</a></span>
<span class="description">{{ $o->created ? $o->created->toDateTimeString() : '-' }} @if($o->device)[{{ $o->device }}]@else <small><strong>[No Device Info]</strong></small> @endif</span>
</div>
<!-- /.user-block -->
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-tool" data-card-widget="remove"><i class="fa fa-times"></i></button>
</div>
<!-- /.card-tools -->
</div>
<!-- /.card-body -->
<div class="card-body">
<a href="{{ url('p/view',$o->id) }}" target="{{ $o->id }}">{!! $o->getHtmlImageURL() !!}</a>
<x-thumbnail :id="$o->id"/>
</div>
<!-- /.card-body -->
<div class="card-footer card-comments">
<table class="table table-striped table-sm table-hover">
<table class="table table-sm table-striped">
<tr><th>ID</th><td><x-info :id="$o->id"/></td></tr>
@foreach($data as $k=>$v)
<tr><th>{{$k}}</th><td>{!! $o->{$v[1]} !!}</td></tr>
<tr @class(['bg-success'=>($reference->exists && $reference->{$v[1]} === $o->{$v[1]})])>
<th>{{$k}}</th>
<td>{!! $o->{$v[1]} !!}</td>
</tr>
@endforeach
</table>
</div>

View File

@ -15,8 +15,8 @@ Route::get('/p/duplicates/{id?}',[PhotoController::class,'duplicates'])
Route::get('/v/duplicates/{id?}',[VideoController::class,'duplicates'])
->where('id','[0-9]+');
Route::get('/p/info/{o}',[PhotoController::class,'info'])
->where('o','[0-9]+');
Route::view('/p/info/{po}','photo.view')
->where('po','[0-9]+');
Route::get('/v/info/{o}',[VideoController::class,'info'])
->where('o','[0-9]+');
Route::get('/p/thumbnail/{o}',[PhotoController::class,'thumbnail'])