'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
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function people()
{
return $this->belongsToMany(Person::class);
}
/**
* Software used to create Multimedia Object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function software()
{
return $this->belongsTo(Software::class);
}
/**
* Tags added to Multimedia Object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany(Tag::class);
}
/* SCOPES */
/**
* Find records marked as duplicate
*
* @param $query
* @return mixed
*/
public function scopeDuplicates($query)
{
return $query->notRemove()
->where('duplicate',TRUE)
->where(fn($q)=>$q->where('ignore_duplicate','<>',TRUE)->orWhereNull('ignore_duplicate'));
}
/**
* Search Database for duplicates of this object
*
* @param $query
* @return mixed
*/
public function scopeMyDuplicates($query) {
if (! $this->exists)
return $query;
// Exclude this record
$query->where('id','<>',$this->attributes['id']);
// Skip ignore dups
$query->where(fn($q)=>$q->whereNull('ignore_duplicate')->orWhere('ignore_duplicate',FALSE));
// Exclude those marked as remove
$query->where(fn($q)=>$q->where('remove','<>',TRUE));
$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(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'));
$q->where('software_id','=',$this->attributes['software_id']);
} elseif ($this->attributes['created_manual'] AND $this->software_id) {
$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'));
$q->where('software_id','=',$this->attributes['software_id']);
}
});
});
return $query;
}
/**
* Multimedia NOT duplicate.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotDuplicate($query)
{
return $query->where(
fn($q)=>$q->where('duplicate','<>',TRUE)
->orWhere('duplicate','=',NULL)
->orWhere(fn($q)=>$q->where('duplicate','=',TRUE)->where('ignore_duplicate','=',TRUE))
);
}
/**
* Multimedia NOT pending removal.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotRemove($query)
{
return $query->where(fn($q)=>$q->where('remove','<>',TRUE)->orWhere('remove','=',NULL));
}
/**
* Multimedia NOT scanned.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotScanned($query)
{
return $query->where(fn($q)=>$q->where('scanned','<>',TRUE)->orWhere('scanned','=',NULL));
}
/* ABSTRACTS */
abstract public function getObjectOriginal(string $property): mixed;
/* ATTRIBUTES */
/**
* 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 getCreatedAttribute(string $date=NULL): ?Carbon
{
$result = $this->created_manual ?: ($date ? Carbon::create($date) : NULL);
if ($result && static::$includeSubSecTime)
$result->microseconds($this->subsectime*1000);
return $result ?: $this->getObjectOriginal('creation_date');
}
/**
* What device was the multimedia created on
*
* @return string
*/
public function getDeviceAttribute(): string
{
$result = '';
if ($this->software_id) {
if ($this->software->model_id) {
if ($this->software->model->make_id) {
$result .= $this->software->model->make->name;
}
$result .= ($result ? ' ' : '').$this->software->model->name;
}
$result .= ($result ? ' ' : '').$this->software->name;
}
return $result;
}
/**
* Return item dimensions
*/
public function getDimensionsAttribute(): string
{
return $this->width.'x'.$this->height;
}
/**
* 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 ($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 $file;
} 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(FALSE) to determine the name, and file_name(TRUE) to determine the new name
*/
public function file_path($short=FALSE,$new=FALSE)
{
$file = $this->filename;
if ($new)
$file = $this->file_name(TRUE);
return (($short OR preg_match('/^\//',$file)) ? '' : config($this->type.'.dir').DIRECTORY_SEPARATOR).$file;
}
/**
* 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,'/'),'/');
}
/**
* Return the file size
* @deprecated use $this->getFileSizeAttribute())
*/
public function file_size()
{
Log::alert(__METHOD__.' deprecated');
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 HTML Checkbox for flagged
* @deprecated use a component
*/
public function getFlagCheckboxAttribute()
{
return $this->HTMLCheckbox('flag',$this->id,$this->flag);
}
/**
* Return HTML Checkbox for ignore
* @deprecated use a component
*/
public function getIgnoreCheckboxAttribute()
{
return $this->HTMLCheckbox('ignore_duplicate',$this->id,$this->ignore_duplicate);
}
/**
* @deprecated use a component
*/
public function getDuplicateTextAttribute()
{
return $this->TextTrueFalse($this->duplicate);
}
/**
* @deprecated use a component
*/
public function getFlagTextAttribute()
{
return $this->TextTrueFalse($this->flag);
}
/**
* Return HTML Checkbox for remove
* @deprecated use a component
*/
public function getRemoveCheckboxAttribute()
{
return $this->HTMLCheckbox('remove',$this->id,$this->remove);
}
/**
* Display the GPS coordinates
* @deprecated use getGPSAttribute()
*/
public function gps(): string
{
return $this->getGPSAttribute();
}
/**
* Return an HTML checkbox
* @deprecated use a component
*/
protected function HTMLCheckbox($name,$id,$value)
{
return sprintf('',$name,$id,$value ? ' checked="checked"' : '');
}
/**
* Get ID Info link
* @deprecated use a component
*/
protected function HTMLLinkAttribute($id,$url)
{
return sprintf('%s',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)));
}
/**
* Return if this source file is readable.
*
* @return bool
*/
public function isReadable(): bool
{
return is_readable($this->file_name(FALSE));
}
/**
* Determine if a file is moveable
*
* useID boolean Determine if the path is based on the the ID or date
* @todo Change to boolean and rename isMoveable() Log any FALSE reason.
*/
public function moveable()
{
Log::alert(__METHOD__.' deprecated');
return $this->isMoveable();
}
/**
* Get the id of the next record
*/
public function next(): ?self
{
return static::where('id','>',$this->id)
->orderby('id','ASC')
->first();
}
/**
* Return my class shortname
*/
public function objectType(): string
{
return (new \ReflectionClass($this))->getShortName();
}
/**
* Get the id of the previous record
*/
public function previous(): ?self
{
return static::where('id','<',$this->id)
->orderby('id','DESC')
->first();
}
/**
* Display the media signature
*/
public function signature($short=FALSE)
{
return ($short && $this->signature)
? static::stringtrim($this->signature)
: $this->signature;
}
/**
* Determine if the image should be moved
*/
public function shouldMove(): bool
{
return $this->filename !== $this->file_name();
}
/** @deprecated is this really needed? */
private function TextTrueFalse($value): string
{
return $value ? 'TRUE' : 'FALSE';
}
/**
* @todo Check if this is redundant
*
* @param bool $includeme
* @return mixed
*/
private function list_duplicate($includeme=FALSE)
{
return $this->list_duplicates($includeme)->pluck('id');
}
/**
* Find duplicate images based on some attributes of the current image
* @deprecate Use static::duplicates()
*/
private function list_duplicates($includeme=FALSE)
{
$o = static::select();
if ($this->id AND ! $includeme)
$o->where('id','!=',$this->id);
// Ignore photo's pending removal.
if (! $includeme)
$o->where(function($query)
{
$query->where('remove','<>',TRUE)
->orWhere('remove','=',NULL);
});
// Where the signalist_duplicatesture is the same
$o->where(function($query)
{
$query->where('signature','=',$this->signature);
// Or they have the same time taken with the same camera
if ($this->date_created AND ($this->model OR $this->make))
{
$query->orWhere(function($query)
{
$query->where('date_created','=',$this->date_created ? $this->date_created : NULL);
if (Schema::hasColumn($this->getTable(),'subsectime'))
$query->where('subsectime','=',$this->subsectime ? $this->subsectime : NULL);
if (! is_null($this->model))
$query->where('model','=',$this->model);
if (! is_null($this->make))
$query->where('make','=',$this->make);
});
}
});
return $o->get();
}
}