Optimised scanning and importing

This commit is contained in:
Deon George
2018-01-11 23:59:53 +11:00
parent 96fadc5080
commit cfc9bf5d9a
26 changed files with 1542 additions and 759 deletions

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CatalogDump extends Command
{
// Our photo object
private $o = NULL;
/**
* 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 = 'App\Model\\'.$this->argument('type');
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$this->argument('type')));
$o = $class::findOrFail($this->argument('id'));
if (! is_readable($o->file_path()))
{
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
exit;
}
print_r($o->properties());
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CatalogScan extends Command
{
// Our photo object
private $o = NULL;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'catalog:scan {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 = 'App\Model\\'.$this->argument('type');
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$this->argument('type')));
$o = $class::findOrFail($this->argument('id'));
if (! is_readable($o->file_path()))
{
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
exit;
}
$o->setDateCreated();
$o->setSubSecTime();
$o->setSignature();
$o->setMakeModel();
$o->setLocation();
$o->setHeightWidth();
$o->setThumbnail();
// If this is a duplicate
$x = $class::whereIN('id',$o->list_duplicate())->get();
if (count($x)) {
$break = FALSE;
foreach ($x as $oo) {
// If this photo siganture matches another.
if ($o->signature == $oo->signature)
{
// 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->file_path()));
// 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->file_path()));
$o->remove = '1';
}
break;
}
}
}
}
$o->scanned = '1';
if ($o->getDirty())
$this->warn(sprintf('Image [%s] metadata changed',$o->file_path()));
$o->save();
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Console\Commands;
use Log;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Model\Person;
use App\Model\PhotoPerson;
use App\Model\Photo;
@@ -12,6 +13,7 @@ use App\Model\Tag;
class PhotoImport extends Command
{
use DispatchesJobs;
use \App\Traits\Files;
/**
@@ -21,7 +23,7 @@ class PhotoImport extends Command
*/
protected $signature = 'photo:import
{--dir= : Directory to Parse}
{--file= : File to import}
{--file= : File to Import}
{--ignoredupe : Ignore duplicate files}
{--deletedupe : Delete duplicate files}
{--people= : People to reference in photo}
@@ -108,90 +110,39 @@ class PhotoImport extends Command
$c++;
$po = Photo::where('filename',$file)->first();
$o = Photo::where('filename',$file)->first();
if (is_null($po))
if (is_null($o))
{
$po = new Photo;
$po->filename = $file;
$o = new Photo;
$o->filename = $file;
}
if (! is_readable($po->file_path()))
if (! is_readable($o->file_path()))
{
$this->warn(sprintf('Ignoring [%s], it is not readable',$po->file_path()));
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
continue;
}
$po->date_taken = strtotime($po->property('exif:DateTime') ? $po->property('exif:DateTime') : $po->property('exif:DateTimeOriginal'));
$po->subsectime = $po->property('exif:SubSecTimeOriginal');
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
$po->signature = $po->property('signature');
$o->save();
$po->make = $po->property('exif:Make');
$po->model = $po->property('exif:Model');
$po->height = $po->property('height');
$po->width = $po->property('width');
$po->orientation = $po->property('orientation');
$po->gps_lat = Photo::latlon(preg_split('/,\s?/',$po->property('exif:GPSLatitude')),$po->property('exif:GPSLatitudeRef'));
$po->gps_lon = Photo::latlon(preg_split('/,\s?/',$po->property('exif:GPSLongitude')),$po->property('exif:GPSLongitudeRef'));
try {
$po->thumbnail = exif_thumbnail($po->file_path());
} catch (\Exception $e) {
// @todo Couldnt get the thumbnail, so we should create one.
}
// If this is a duplicate
$x = Photo::whereIN('id',$po->list_duplicate())->get();
if (count($x)) {
$skip = FALSE;
foreach ($x as $o) {
// We'll only ignore based on the same signature.
if ($po->signature != $o->signature AND ! $po->exists)
continue;
if ($this->option('ignoredupe'))
{
$skip = TRUE;
$this->warn(sprintf("Ignoring file [%s], it's the same as [%s] with id %s",$po->file_path(),$o->filename,$o->id));
break;
}
elseif ($this->option('deletedupe') AND ($po->filename != $o->filename))
{
$skip = TRUE;
$this->error(sprintf("Deleting file [%s], it's the same as [%s] with id %s and signature [%s]\n",$po->file_path(),$o->filename,$o->id,$po->signature));
unlink($po->file_path());
}
}
if ($skip)
continue;
$po->duplicate = '1';
$this->warn(sprintf('Image [%s] marked as a duplicate',$file));
}
if ($po->exists)
$this->warn(sprintf('Image [%s] already in DB: %s',$file,$po->id));
$po->save();
if ($po->wasRecentlyCreated)
$this->info(sprintf('Image [%s] stored in DB: %s',$file,$po->id));
if ($o->wasRecentlyCreated)
$this->info(sprintf('%s [%s] stored in DB: %s',$o->objectType(),$file,$o->id));
// Record our people and tags
if ($p)
$po->People()->sync($p);
$o->People()->sync($p);
if ($t)
$po->Tags()->sync($t);
$o->Tags()->sync($t);
$this->dispatch((new \App\Jobs\CatalogScan($o))->onQueue('scan'));
}
$bar->finish();
return $this->info(sprintf('Images processed: %s',$c));
return $this->info(sprintf('Photos processed: %s',$c));
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Log;
use App\Model\Photo;
use App\Jobs\CatalogScan;
class PhotoScanAll extends Command
{
use DispatchesJobs;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'photo:scanall';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Photo::NotScanned()->chunk(200,function ($data) {
foreach ($data as $o)
{
if ($o->remove) {
Log::warning(sprintf('Not scanning [%s], marked for removal',$o->id));
continue;
}
$this->dispatch((new CatalogScan($o))->onQueue('scan'));
}
});
}
}

View File

@@ -4,13 +4,14 @@ namespace App\Console\Commands;
use Log;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Model\Video;
use App\Model\Tag;
use App\Model\Person;
class VideoImport extends Command
{
use DispatchesJobs;
use \App\Traits\Files;
/**
@@ -108,91 +109,42 @@ class VideoImport extends Command
$c++;
$vo = Video::where('filename',$file)->first();
$o = Video::where('filename',$file)->first();
if (is_null($vo))
if (is_null($o))
{
$vo = new Video;
$vo->filename = $file;
$o = new Video;
$o->filename = $file;
}
if (! is_readable($vo->file_path()))
if (! is_readable($o->file_path()))
{
$this->warn(sprintf('Ignoring [%s], it is not readable',$vo->file_path()));
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
continue;
}
$vo->date_created = strtotime($vo->property('creationdate'));
$vo->signature = $vo->property('signature');
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
$vo->make = $vo->property('make');
$vo->model = $vo->property('model');
$o->save();
$vo->height = $vo->property('height');
$vo->width = $vo->property('width');
$vo->type = $vo->property('type');
$vo->length = $vo->property('length');
$vo->codec = $vo->property('codec');
$vo->audiochannels = $vo->property('audiochannels');
$vo->channelmode = $vo->property('channelmode');
$vo->samplerate = $vo->property('samplerate');
$vo->gps_lat = $vo->property('gps_lat');
$vo->gps_lon = $vo->property('gps_lon');
$vo->gps_altitude = $vo->property('gps_altitude');
// If this is a duplicate
$x = Video::whereIN('id',$vo->list_duplicate())->get();
if (count($x)) {
$skip = FALSE;
foreach ($x as $o) {
// We'll only ignore based on the same signature.
if ($vo->signature != $o->signature AND ! $vo->exists)
continue;
if ($this->option('ignoredupe'))
{
$skip = TRUE;
$this->warn(sprintf("Ignoring file [%s], it's the same as [%s] with id %s",$vo->file_path(),$o->filename,$o->id));
break;
}
elseif ($this->option('deletedupe') AND ($vo->filename != $o->filename))
{
$skip = TRUE;
$this->error(sprintf("Deleting file [%s], it's the same as [%s] with id %s and signature [%s]\n",$vo->file_path(),$o->filename,$o->id,$vo->signature));
unlink($vo->file_path());
}
}
if ($skip)
continue;
$vo->duplicate = '1';
$this->warn(sprintf('Video [%s] marked as a duplicate',$file));
}
if ($vo->exists)
$this->warn(sprintf('Video [%s] already in DB: %s',$file,$vo->id));
$vo->save();
if ($vo->wasRecentlyCreated)
$this->info(sprintf('Video [%s] stored in DB: %s',$file,$vo->id));
if ($o->wasRecentlyCreated)
$this->info(sprintf('%s [%s] stored in DB: %s',$o->objectType(),$file,$o->id));
// Record our people and tags
if ($p)
$vo->People()->sync($p);
$o->People()->sync($p);
if ($t)
$vo->Tags()->sync($t);
$o->Tags()->sync($t);
$this->dispatch((new \App\Jobs\CatalogScan($o))->onQueue('scan'));
if ($this->option('dumpid3'))
dd($vo->dump());
dd($o->dump());
}
$bar->finish();
return $this->info(sprintf('Video processed: %s',$c));
return $this->info(sprintf('Videos processed: %s',$c));
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Log;
use App\Model\Video;
use App\Jobs\CatalogScan;
class VideoScanAll extends Command
{
use DispatchesJobs;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'video:scanall';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Video::NotScanned()->chunk(200,function ($data) {
foreach ($data as $o)
{
if ($o->remove) {
Log::warning(sprintf('Not scanning [%s], marked for removal',$o->id));
continue;
}
$this->dispatch((new CatalogScan($o))->onQueue('scan'));
}
});
}
}