Update photo import to laravel 6.4

This commit is contained in:
Deon George
2019-11-09 13:52:04 +11:00
parent f6b456aa3d
commit 83d6d1e055
18 changed files with 274 additions and 253 deletions

View File

@@ -3,7 +3,6 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CatalogScan extends Command
{
@@ -31,56 +30,51 @@ class CatalogScan extends Command
*/
public function handle()
{
$class = 'App\Model\\'.$this->argument('type');
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$this->argument('type')));
$class = 'App\Models\\'.$this->argument('type');
$o = $class::findOrFail($this->argument('id'));
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$this->argument('type')));
if (! is_readable($o->file_path()))
{
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
exit;
}
$o = $class::findOrFail($this->argument('id'));
$o->setDateCreated();
$o->setSubSecTime();
$o->setSignature();
$o->setMakeModel();
$o->setLocation();
$o->setHeightWidth();
$o->setThumbnail();
if (! is_readable($o->file_path())) {
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
exit;
}
// If this is a duplicate
$x = $class::whereIN('id',$o->list_duplicate())->get();
if (count($x)) {
$break = FALSE;
$o->setDateCreated();
$o->setSubSecTime();
$o->setSignature();
$o->setMakeModel();
$o->setLocation();
$o->setHeightWidth();
$o->setThumbnail();
foreach ($x as $oo) {
// 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 this is a duplicate
$x = $class::whereIN('id',$o->list_duplicate())->get();
if (count($x)) {
foreach ($x as $oo) {
// 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';
// 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;
}
break;
}
}
}
$o->scanned = '1';
$o->scanned = '1';
if ($o->getDirty())
$this->warn(sprintf('Image [%s] metadata changed',$o->file_path()));
if ($o->getDirty())
$this->warn(sprintf('Image [%s] metadata changed',$o->file_path()));
$o->save();
$o->save();
}
}

View File

@@ -2,19 +2,17 @@
namespace App\Console\Commands;
use Log;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Model\Person;
use App\Model\PhotoPerson;
use App\Model\Photo;
use App\Model\PhotoTag;
use App\Model\Tag;
use App\Models\{Person,Photo,Tag};
use App\Traits\Files;
class PhotoImport extends Command
{
use DispatchesJobs;
use \App\Traits\Files;
use Files;
/**
* The name and signature of the console command.
@@ -53,7 +51,11 @@ class PhotoImport extends Command
*/
public function handle()
{
$files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')],'photo');
$files = $this->getFiles([
'dir'=>$this->option('dir'),
'file'=>$this->option('file')
],'photo');
if (! count($files))
exit;
@@ -65,22 +67,21 @@ class PhotoImport extends Command
$t = $p = array();
// Tags
if ($this->option('tags'))
{
if ($this->option('tags')) {
$tags = explode(',',$this->option('tags'));
$t = Tag::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $t OR count($t) != count($tags))
{
if (! $t OR count($t) != count($tags)) {
$this->error(sprintf('Tag [%s] dont exist',join('|',$tags)));
abort(501);
}
}
// People
if ($this->option('people'))
{
if ($this->option('people')) {
$tags = explode(',',$this->option('people'));
$p = Person::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $p OR count($p) != count($tags))
{
$this->error(sprintf('People [%s] dont exist',join('|',$tags)));
@@ -89,18 +90,15 @@ class PhotoImport extends Command
}
$c = 0;
foreach ($files as $file)
{
foreach ($files as $file) {
$bar->advance();
if (preg_match('/@__thumb/',$file) OR preg_match('/\/._/',$file))
{
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')))
{
if (! in_array(strtolower(pathinfo($file,PATHINFO_EXTENSION)),config('photo.import.accepted'))) {
$this->warn(sprintf('Ignoring [%s]',$file));
continue;
}
@@ -112,21 +110,21 @@ class PhotoImport extends Command
$o = Photo::where('filename',$file)->first();
if (is_null($o))
{
// The photo doesnt exist
if (is_null($o)) {
$o = new Photo;
$o->filename = $file;
}
if (! is_readable($o->file_path()))
{
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
// Make sure we can read the photo.
if (! is_readable($o->file_path())) {
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
continue;
}
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
$o->save();
if ($o->wasRecentlyCreated)
@@ -145,4 +143,4 @@ class PhotoImport extends Command
return $this->info(sprintf('Photos processed: %s',$c));
}
}
}

View File

@@ -2,14 +2,15 @@
namespace App\Console\Commands;
use DB;
use Log;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use App\Model\Photo;
use App\Traits\Files;
use App\Models\Photo;
class PhotoMove extends Command
{
use \App\Traits\Files;
use Files;
/**
* The name and signature of the console command.
@@ -44,61 +45,54 @@ class PhotoMove extends Command
*/
public function handle()
{
if ($this->option('file'))
{
$po = Photo::notRemove()->notDuplicate()->where('filename',Photo::path($this->option('file')));
}
elseif ($this->option('id'))
{
$po = Photo::notRemove()->notDuplicate()->where('id',$this->option('id'));
}
else
{
$po = Photo::notRemove()->notDuplicate();
}
if ($this->option('file')) {
$po = Photo::notRemove()->notDuplicate()->where('filename',Photo::path($this->option('file')));
if (! $po)
exit;
} elseif ($this->option('id')) {
$po = Photo::notRemove()->notDuplicate()->where('id',$this->option('id'));
// Show a progress bar
$bar = $this->output->createProgressBar($po->count());
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$bar->setRedrawFrequency(100);
$po->chunk(1,function($photo) use ($bar) {
while ($o = $photo->shift())
{
if (($x = $o->moveable()) === TRUE) {
Log::info(sprintf('%s: Moving (%s)[%s]',__METHOD__,$o->id,$o->filename));
if ($this->makeParentDir(dirname($o->file_path(FALSE,TRUE))) AND rename($o->file_path(),$o->file_path(FALSE,TRUE)))
{
// Convert the path to a relative one.
$o->filename = $o->file_path(TRUE,TRUE);
// @todo If the DB update failed, move it back.
if (! $o->save()) # AND ! rename($path,$o->file_path()))
{
$this->error(sprintf('Save after rename failed for (%s)',$o->id));
continue;
}
}
else
{
$this->error(sprintf('Rename failed for (%s)',$o->id));
continue;
}
chmod($o->file_path(),0444);
}
else
{
if ($x > 0)
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], moveable returned (%s)',$o->id,$o->file_path(),$o->file_path(FALSE,TRUE),$x));
}
} else {
$po = Photo::notRemove()->notDuplicate();
}
$bar->advance();
});
if (! $po)
exit;
// Show a progress bar
$bar = $this->output->createProgressBar($po->count());
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$bar->setRedrawFrequency(100);
$po->chunk(1,function($photo) use ($bar) {
while ($o = $photo->shift()) {
if (($x = $o->moveable()) === TRUE) {
Log::info(sprintf('%s: Moving (%s)[%s]',__METHOD__,$o->id,$o->filename));
if ($this->makeParentDir(dirname($o->file_path(FALSE,TRUE))) AND rename($o->file_path(),$o->file_path(FALSE,TRUE))) {
// Convert the path to a relative one.
$o->filename = $o->file_path(TRUE,TRUE);
// @todo If the DB update failed, move it back.
if (! $o->save()) # AND ! rename($path,$o->file_path()))
{
$this->error(sprintf('Save after rename failed for (%s)',$o->id));
continue;
}
} else {
$this->error(sprintf('Rename failed for (%s)',$o->id));
continue;
}
chmod($o->file_path(),0444);
} else {
if ($x > 0)
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], moveable returned (%s)',$o->id,$o->file_path(),$o->file_path(FALSE,TRUE),$x));
}
}
$bar->advance();
});
}
}
}