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

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