2016-06-22 05:49:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Log;
|
|
|
|
use Illuminate\Console\Command;
|
2018-01-11 12:59:53 +00:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2016-06-22 05:49:20 +00:00
|
|
|
use App\Model\Person;
|
|
|
|
use App\Model\PhotoPerson;
|
|
|
|
use App\Model\Photo;
|
|
|
|
use App\Model\PhotoTag;
|
|
|
|
use App\Model\Tag;
|
|
|
|
|
2016-07-04 06:00:33 +00:00
|
|
|
class PhotoImport extends Command
|
2016-06-22 05:49:20 +00:00
|
|
|
{
|
2018-01-11 12:59:53 +00:00
|
|
|
use DispatchesJobs;
|
2016-07-01 02:19:17 +00:00
|
|
|
use \App\Traits\Files;
|
|
|
|
|
2016-06-22 05:49:20 +00:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'photo:import
|
|
|
|
{--dir= : Directory to Parse}
|
2018-01-11 12:59:53 +00:00
|
|
|
{--file= : File to Import}
|
2016-06-22 05:49:20 +00:00
|
|
|
{--ignoredupe : Ignore duplicate files}
|
|
|
|
{--deletedupe : Delete duplicate files}
|
|
|
|
{--people= : People to reference in photo}
|
|
|
|
{--tags= : Add tag to photo}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Import photos into the database';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2016-07-04 06:00:33 +00:00
|
|
|
$files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')],'photo');
|
2016-07-01 02:19:17 +00:00
|
|
|
if (! count($files))
|
|
|
|
exit;
|
2016-06-22 21:07:44 +00:00
|
|
|
|
2016-06-22 05:49:20 +00:00
|
|
|
// Show a progress bar
|
|
|
|
$bar = $this->output->createProgressBar(count($files));
|
|
|
|
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
|
|
|
|
|
|
|
|
$tags = NULL;
|
|
|
|
$t = $p = array();
|
|
|
|
|
|
|
|
// Tags
|
|
|
|
if ($this->option('tags'))
|
|
|
|
{
|
|
|
|
$tags = explode(',',$this->option('tags'));
|
2016-07-01 05:59:22 +00:00
|
|
|
$t = Tag::whereIn('tag',$tags)->pluck('id')->toArray();
|
2018-01-09 21:10:14 +00:00
|
|
|
if (! $t OR count($t) != count($tags))
|
|
|
|
{
|
|
|
|
$this->error(sprintf('Tag [%s] dont exist',join('|',$tags)));
|
|
|
|
abort(501);
|
|
|
|
}
|
2016-06-22 05:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// People
|
|
|
|
if ($this->option('people'))
|
|
|
|
{
|
|
|
|
$tags = explode(',',$this->option('people'));
|
2016-07-01 05:59:22 +00:00
|
|
|
$p = Person::whereIn('tag',$tags)->pluck('id')->toArray();
|
2018-01-09 21:10:14 +00:00
|
|
|
if (! $p OR count($p) != count($tags))
|
|
|
|
{
|
|
|
|
$this->error(sprintf('People [%s] dont exist',join('|',$tags)));
|
|
|
|
abort(501);
|
|
|
|
}
|
2016-06-22 05:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$c = 0;
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
$bar->advance();
|
|
|
|
|
|
|
|
if (preg_match('/@__thumb/',$file) OR preg_match('/\/._/',$file))
|
|
|
|
{
|
|
|
|
$this->warn(sprintf('Ignoring file [%s]',$file));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! in_array(strtolower(pathinfo($file,PATHINFO_EXTENSION)),config('photo.import.accepted')))
|
|
|
|
{
|
|
|
|
$this->warn(sprintf('Ignoring [%s]',$file));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->option('verbose'))
|
|
|
|
$this->info(sprintf('Processing file [%s]',$file));
|
|
|
|
|
|
|
|
$c++;
|
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
$o = Photo::where('filename',$file)->first();
|
2016-06-22 05:49:20 +00:00
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
if (is_null($o))
|
2016-06-22 05:49:20 +00:00
|
|
|
{
|
2018-01-11 12:59:53 +00:00
|
|
|
$o = new Photo;
|
|
|
|
$o->filename = $file;
|
2016-06-22 05:49:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
if (! is_readable($o->file_path()))
|
2016-07-04 06:00:33 +00:00
|
|
|
{
|
2018-01-11 12:59:53 +00:00
|
|
|
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
|
2016-07-04 06:00:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
if ($o->exists)
|
|
|
|
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
|
2016-06-22 05:49:20 +00:00
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
$o->save();
|
2016-06-22 05:49:20 +00:00
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
if ($o->wasRecentlyCreated)
|
|
|
|
$this->info(sprintf('%s [%s] stored in DB: %s',$o->objectType(),$file,$o->id));
|
2016-06-22 05:49:20 +00:00
|
|
|
|
2016-07-01 04:37:55 +00:00
|
|
|
// Record our people and tags
|
2016-07-01 05:59:22 +00:00
|
|
|
if ($p)
|
2018-01-11 12:59:53 +00:00
|
|
|
$o->People()->sync($p);
|
2016-07-01 05:59:22 +00:00
|
|
|
if ($t)
|
2018-01-11 12:59:53 +00:00
|
|
|
$o->Tags()->sync($t);
|
|
|
|
|
|
|
|
$this->dispatch((new \App\Jobs\CatalogScan($o))->onQueue('scan'));
|
2016-06-22 05:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$bar->finish();
|
|
|
|
|
2018-01-11 12:59:53 +00:00
|
|
|
return $this->info(sprintf('Photos processed: %s',$c));
|
2016-06-22 05:49:20 +00:00
|
|
|
}
|
|
|
|
}
|