Initial welcome page

This commit is contained in:
Deon George
2019-11-08 23:51:47 +11:00
parent a91ddbe66e
commit f6b456aa3d
10 changed files with 79 additions and 35 deletions

View File

@@ -0,0 +1,371 @@
<?php
namespace App\Models\Abstracted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use DB;
use App\Models\{Person,Tag};
abstract class Catalog extends Model
{
public function People()
{
return $this->belongsToMany(Person::class);
}
public function Tags()
{
return $this->belongsToMany(Tag::class);
}
/**
* Multimedia NOT duplicate.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotDuplicate($query)
{
return $query->where(function($query)
{
$query->where('duplicate','!=',TRUE)
->orWhere('duplicate','=',NULL);
});
}
/**
* Multimedia NOT pending removal.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotRemove($query)
{
return $query->where(function($query)
{
$query->where('remove','!=',TRUE)
->orWhere('remove','=',NULL);
});
}
/**
* Multimedia NOT scanned.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotScanned($query)
{
return $query->where(function($query)
{
$query->where('scanned','!=',TRUE)
->orWhere('scanned','=',NULL);
});
}
abstract public function setDateCreated();
abstract public function setLocation();
abstract public function setMakeModel();
abstract public function setSignature();
abstract public function setSubSecTime();
abstract public function setThumbnail();
abstract public function view();
/**
* Date the multimedia was created
*/
public function date_taken()
{
return $this->date_created ? date('Y-m-d H:i:s',$this->date_created) : 'UNKNOWN';
}
/**
* Return the date of the file
*/
public function file_date($type,$format=FALSE)
{
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;
}
/**
* Determine the new name for the image
*/
public function file_path($short=FALSE,$new=FALSE)
{
$file = $this->filename;
if ($new)
$file = sprintf('%s.%s',((is_null($this->date_created) OR ! $this->date_created)
? sprintf('UNKNOWN/%07s',$this->file_path_id())
: date('Y/m/d-His',$this->date_created)),$this->type());
return (($short OR preg_match('/^\//',$file)) ? '' : config('video.dir').DIRECTORY_SEPARATOR).$file;
}
/**
* Calculate a file path ID based on the id of the file
*/
public function file_path_id($sep=3,$depth=9)
{
return trim(chunk_split(sprintf("%0{$depth}s",$this->id),$sep,'/'),'/');
}
/**
* Display the file signature
*/
public function file_signature($short=FALSE)
{
return $short ? static::signaturetrim($this->file_signature) : $this->file_signature;
}
/**
* Return the photo size
*/
public function file_size()
{
return (! is_readable($this->file_path())) ? NULL : filesize($this->file_path());
}
public function getFileSizeAttribute()
{
return (! is_readable($this->file_path())) ? NULL : filesize($this->file_path());
}
public function getDateCreatedTextAttribute()
{
if ($this->date_created)
return date('d-m-Y H:i:s',$this->date_created);
}
public function getDuplicateTextAttribute()
{
return $this->TextTrueFalse($this->duplicate);
}
public function getFlagTextAttribute()
{
return $this->TextTrueFalse($this->flag);
}
/**
* Return item dimensions
*/
public function getDimensionsAttribute()
{
return $this->width.'x'.$this->height;
}
/**
* Return HTML Checkbox for duplicate
*/
public function getDuplicateCheckboxAttribute()
{
return $this->HTMLCheckbox('duplicate',$this->id,$this->duplicate);
}
/**
* Return HTML Checkbox for flagged
*/
public function getFlagCheckboxAttribute()
{
return $this->HTMLCheckbox('flag',$this->id,$this->flag);
}
/**
* Return HTML Checkbox for remove
*/
public function getRemoveCheckboxAttribute()
{
return $this->HTMLCheckbox('remove',$this->id,$this->remove);
}
/**
* Display the GPS coordinates
*/
public function gps()
{
return ($this->gps_lat AND $this->gps_lon) ? sprintf('%s/%s',$this->gps_lat,$this->gps_lon) : 'UNKNOWN';
}
/**
* Get ID Info link
*/
protected function HTMLLinkAttribute($id,$url)
{
return sprintf('<a href="%s" target="%s">%s</a>',url($url.$id),$id,$id);
}
/**
* 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"' : '');
}
public function isParentWritable($dir)
{
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)));
}
/**
* Determine if a file is moveable
*
* useID boolean Determine if the path is based on the the ID or date
*/
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;
}
/**
* Get the id of the previous record
*/
public function next()
{
return DB::table($this->getTable())
->where('id','>',$this->id)
->orderby('id','ASC')
->first();
}
/**
* Return my class shortname
*/
public function objectType()
{
return (new \ReflectionClass($this))->getShortName();
}
/**
* Get the id of the previous record
*/
public function previous()
{
return DB::table($this->getTable())
->where('id','<',$this->id)
->orderby('id','DEC')
->first();
}
public function setHeightWidth()
{
$this->height = $this->property('height');
$this->width = $this->property('width');
$this->orientation = $this->property('orientation');
}
/**
* Display the media signature
*/
public function signature($short=FALSE)
{
return $short ? static::signaturetrim($this->signature) : $this->signature;
}
public static function signaturetrim($signature,$chars=6)
{
return sprintf('%s...%s',substr($signature,0,$chars),substr($signature,-1*$chars));
}
/**
* Determine if the image should be moved
*/
public function shouldMove()
{
return ($this->filename != $this->file_path(TRUE,TRUE));
}
protected function TextTrueFalse($value)
{
return $value ? 'TRUE' : 'FALSE';
}
public function list_duplicate($includeme=FALSE)
{
return $this->list_duplicates($includeme)->pluck('id');
}
/**
* Find duplicate images based on some attributes of the current image
*/
public 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 signature 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();
}
}

9
app/Models/Person.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
}

230
app/Models/Photo.php Normal file
View File

@@ -0,0 +1,230 @@
<?php
namespace App\Models;
use DB;
class Photo extends Abstracted\Catalog
{
protected $table = 'photo';
// Imagick Object
private $_o;
// How should the image be rotated, based on the value of orientation
private $_rotate = [
3=>180,
6=>90,
8=>-90,
];
/**
* Date the photo was taken
*/
public function date_taken()
{
return $this->date_created ? (date('Y-m-d H:i:s',$this->date_created).($this->subsectime ? '.'.$this->subsectime : '')) : 'UNKNOWN';
}
/**
* Determine the new name for the image
*/
public function file_path($short=FALSE,$new=FALSE)
{
$file = $this->filename;
if ($new)
$file = sprintf('%s.%s',((is_null($this->date_created) OR ! $this->date_created)
? sprintf('UNKNOWN/%07s',$this->file_path_id())
: sprintf('%s_%03s',date('Y/m/d-His',$this->date_created),$this->subsectime).
($this->subsectime ? '' : sprintf('-%05s',$this->id))),$this->type());
return (($short OR preg_match('/^\//',$file)) ? '' : config('photo.dir').DIRECTORY_SEPARATOR).$file;
}
public function getIDLinkAttribute()
{
return $this->HTMLLinkAttribute($this->id,url('p/info').'/');
}
/**
* 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');
$this->rotate($imo);
return $imo->getImageBlob();
}
/**
* Calculate the GPS coordinates
*/
public static function latlon(array $coordinate,$hemisphere)
{
if (! $coordinate OR ! $hemisphere)
return NULL;
for ($i=0; $i<3; $i++)
{
$part = explode('/', $coordinate[$i]);
if (count($part) == 1)
$coordinate[$i] = $part[0];
elseif (count($part) == 2)
$coordinate[$i] = floatval($part[0])/floatval($part[1]);
else
$coordinate[$i] = 0;
}
list($degrees, $minutes, $seconds) = $coordinate;
$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
*
*/
protected function o($attr=NULL)
{
if (! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
return NULL;
if (is_null($this->_o))
$this->_o = new \Imagick($this->file_path());
return is_null($attr) ? $this->_o : $this->_o->getImageProperty($attr);
}
/**
* Display the orientation of a photo
*/
public function orientation() {
switch ($this->orientation) {
case 1: return 'None!';
case 3: return 'Upside Down';
case 6: return 'Rotate Right';
case 8: return 'Rotate Left';
default:
return 'unknown?';
}
}
/**
* Rotate the image
*/
private function rotate(\Imagick $imo)
{
if (array_key_exists($this->orientation,$this->_rotate))
$imo->rotateImage(new \ImagickPixel('none'),$this->_rotate[$this->orientation]);
return $imo->getImageBlob();
}
public function property($property)
{
if (! $this->o())
return NULL;
switch ($property)
{
case 'creationdate': return strtotime($this->property('exif:DateTimeOriginal') ? $this->property('exif:DateTimeOriginal') : $this->property('exif:DateTime')); break;
case 'height': return $this->_o->getImageHeight(); break;
case 'orientation': return $this->_o->getImageOrientation(); break;
case 'signature': return $this->_o->getImageSignature(); break;
case 'width': return $this->_o->getImageWidth(); break;
default:
return $this->_o->getImageProperty($property);
}
}
public function properties()
{
return $this->o() ? $this->_o->getImageProperties() : [];
}
public function setDateCreated()
{
if ($this->property('creationdate'))
$this->date_created = $this->property('creationdate');
}
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'));
#$this->gps_altitude = $this->property('gps_altitude');
}
public function setMakeModel()
{
$this->make = $this->property('exif:Make') ? $this->property('exif:Make') : NULL;
$this->model = $this->property('exif:Model') ? $this->property('exif:Model') : NULL;
$this->software = $this->property('exif:Software') ? $this->property('exif:Software') : NULL;
}
public function setSignature()
{
$this->signature = $this->property('signature');
$this->file_signature = md5_file($this->file_path());
#$this->identifier = $this->property('identifier');
}
public function setSubSecTime()
{
$this->subsectime = $this->property('exif:SubSecTimeOriginal');
}
public function setThumbnail()
{
try {
$this->thumbnail = exif_thumbnail($this->file_path());
} catch (\Exception $e) {
// @todo Couldnt get the thumbnail, so we should create one.
}
}
/**
* Return the image's thumbnail
*/
public function thumbnail($rotate=TRUE)
{
if (! $this->thumbnail)
{
return $this->o()->thumbnailimage(200,200,true,true) ? $this->_o->getImageBlob() : NULL;
}
if (! $rotate OR ! array_key_exists($this->orientation,$this->_rotate) OR ! extension_loaded('imagick'))
return $this->thumbnail;
$imo = new \Imagick();
$imo->readImageBlob($this->thumbnail);
return $this->rotate($imo);
}
/**
* Return the extension of the image
* @todo mime-by-ext?
*/
public function type($mime=FALSE)
{
return strtolower($mime ? 'image/jpeg' : pathinfo($this->filename,PATHINFO_EXTENSION));
}
public function view()
{
return sprintf('<img height="240" src="%s"></img>',url('/p/thumbnail/'.$this->id));
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PhotoPerson extends Model
{
}

15
app/Models/PhotoTag.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PhotoTag extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'photo_tag';
}

9
app/Models/Tag.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
}

177
app/Models/Video.php Normal file
View File

@@ -0,0 +1,177 @@
<?php
namespace App\Models;
use DB;
class Video extends Abstracted\Catalog
{
// ID3 Object
private $_o = NULL;
public function getIDLinkAttribute()
{
return $this->HTMLLinkAttribute($this->id,url('v/info').'/');
}
/**
* Return an GetID3 object or attribute
*
*/
protected function o($attr=NULL)
{
if (! $this->filename OR ! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
return FALSE;
if (is_null($this->_o))
{
$this->_o = new \getID3;
$this->_o->analyze($this->file_path());
$this->_o->getHashdata('sha1');
}
return is_null($attr) ? $this->_o : (array_key_exists($attr,$this->_o->info) ? $this->_o->info[$attr] : NULL);
}
public function property($property)
{
if (! $this->o())
return NULL;
switch ($property)
{
case 'creationdate':
// Try creation_Data
$x = array_get($this->_o->info,'quicktime.comments.creation_date.0');
// Try creation_Data
if (is_null($x))
$x = array_get($this->_o->info,'quicktime.comments.creationdate.0');
return $x;
case 'make': return array_get($this->_o->info,'quicktime.comments.make.0');
case 'model': return array_get($this->_o->info,'quicktime.comments.model.0');
case 'software': return array_get($this->_o->info,'quicktime.comments.software.0');
case 'signature': return array_get($this->_o->info,'sha1_data');
#case 'height': return $this->subatomsearch('quicktime.moov.subatoms',['trak','tkhd'],'height'); break;
#case 'width': return $this->subatomsearch('quicktime.moov.subatoms',['trak','tkhd'],'width'); break;
case 'height': return array_get($this->_o->info,'video.resolution_y');
case 'width': return array_get($this->_o->info,'video.resolution_x');
case 'length': return array_get($this->_o->info,'playtime_seconds');
case 'type': return array_get($this->_o->info,'video.dataformat');
case 'codec': return array_get($this->_o->info,'audio.codec');
case 'audiochannels': return array_get($this->_o->info,'audio.channels');
case 'samplerate': return array_get($this->_o->info,'audio.sample_rate');
case 'channelmode': return array_get($this->_o->info,'audio.channelmode');
case 'gps_lat': return array_get($this->_o->info,'quicktime.comments.gps_latitude.0');
case 'gps_lon': return array_get($this->_o->info,'quicktime.comments.gps_longitude.0');
case 'gps_altitude': return array_get($this->_o->info,'quicktime.comments.gps_altitude.0');
case 'identifier': if ($x=array_get($this->_o->info,'quicktime.comments')) {
return array_get(array_flatten(array_only($x,'content.identifier')),0);
}
break;
default:
return NULL;
}
}
public function properties()
{
return $this->o() ? $this->_o->info : [];
}
/**
* Navigate through ID3 data to find the value.
*/
private function subatomsearch($atom,array $paths,$key,array $data=[]) {
if (! $data AND is_null($data = array_get($this->_o->info,$atom)))
{
// Didnt find it.
return NULL;
}
foreach ($paths as $path)
{
$found = FALSE;
foreach ($data as $array)
{
if ($array['name'] === $path)
{
$found = TRUE;
if ($path != last($paths))
$data = $array['subatoms'];
else
$data = $array;
break;
}
}
if (! $found)
break;
}
return isset($data[$key]) ? $data[$key] : NULL;
}
public function setDateCreated()
{
if ($this->property('creationdate'))
$this->date_created = $this->property('creationdate');
}
public function setDateCreatedAttribute($value)
{
$this->attributes['date_created'] = strtotime($value);
}
public function setLocation()
{
$this->gps_lat = $this->property('gps_lat');
$this->gps_lon = $this->property('gps_lon');
$this->gps_altitude = $this->property('gps_altitude');
}
public function setMakeModel()
{
$this->make = $this->property('make');
$this->model = $this->property('model');
$this->software = $this->property('software');
$this->type = $this->property('type');
$this->length = $this->property('length');
$this->codec = $this->property('codec');
$this->audiochannels = $this->property('audiochannels');
$this->channelmode = $this->property('channelmode');
$this->samplerate = $this->property('samplerate');
}
public function setSignature()
{
$this->signature = $this->property('signature');
$this->file_signature = md5_file($this->file_path());
$this->identifier = $this->property('identifier');
}
public function setSubSecTime()
{
// NOOP
}
public function setThumbnail()
{
// NOOP
}
/**
* Return the extension of the image
*/
public function type($mime=FALSE)
{
return strtolower($mime ? File::mime_by_ext(pathinfo($this->filename,PATHINFO_EXTENSION)) : pathinfo($this->filename,PATHINFO_EXTENSION));
}
public function view()
{
return sprintf('<video width="320" height="240" src="%s" controls></video>',url('/v/view/'.$this->id));
}
}