Major refactor of photo processing, video processing still to do

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

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);