Photo scan and move implemented, and remove redundant files

This commit is contained in:
2024-09-02 22:51:19 +10:00
parent 431ceec69f
commit a3013078e0
13 changed files with 131 additions and 479 deletions

View File

@@ -1,73 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\{PhotoDelete,VideoDelete};
use App\Traits\Type;
class CatalogAutoDelete extends Command
{
use DispatchesJobs,Type;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'catalog:autodelete {type : Photo | Video }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Auto Delete Catalog Items';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$class = $this->getModelType($this->argument('type'));
$class::where('remove',1)->each(function($o) {
foreach ($o->myduplicates()->get() as $oo) {
if (! $oo->signature OR ! $oo->file_signature)
continue;
if ($oo->signature == $o->signature AND $oo->file_signature == $o->file_signature) {
$this->info(sprintf('Removing: %s (%s)',$o->id,$o->filename));
// Dispatch Job to move file.
switch (strtolower($this->argument('type'))) {
case 'photo':
$this->dispatch((new PhotoDelete($o))->onQueue('delete'));
break;
case 'video':
$this->dispatch((new VideoDelete($o))->onQueue('delete'));
break;
default:
$this->error('Dont know how to handle: ',$this->argument('type'));
}
}
}
});
}
}

View File

@@ -3,27 +3,29 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\{PhotoMove,VideoMove};
use App\Jobs\CatalogMove as Job;
use App\Traits\Type;
class CatalogMove extends Command
{
use DispatchesJobs;
use Type;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'catalog:move {type : Photo | Video }';
protected $signature = 'catalog:move'
.' {type : Photo | Video }'
.' {id : Photo ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Trigger Moves';
protected $description = 'Move Photo/Video based on their meta data';
/**
* Execute the console command.
@@ -32,35 +34,10 @@ class CatalogMove extends Command
*/
public function handle()
{
$class = 'App\Models\\'.$this->argument('type');
$class = $this->getModelType($this->argument('type'));
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$this->argument('type')));
$o = $class::findOrFail($this->argument('id'));
foreach ($class::where('filename','LIKE','%INCOMING%')->get() as $o) {
// Catch any files that are already there.
if ($o->moveable() === 1) {
$o->duplicate = TRUE;
$o->save();
continue;
}
$x = NULL;
if (! $o->scanned OR $o->duplicate OR $o->remove OR ($x=$o->moveable()) !== TRUE) {
$this->warn(sprintf('Ignoring (%s)[%s]... [%s]',$o->id,$o->file_path(),$x));
continue;
}
switch (strtolower($this->argument('type'))) {
case 'photo':
$this->dispatch((new PhotoMove($o))->onQueue('move'));
break;
case 'video':
$this->dispatch((new VideoMove($o))->onQueue('move'));
break;
default:
$this->error('Dont know how to handle: ',$this->argument('type'));
}
}
return Job::dispatchSync($o);
}
}
}

View File

@@ -16,10 +16,10 @@ class CatalogScan extends Command
*
* @var string
*/
protected $signature = 'catalog:scan'.
' {type : Photo | Video }'.
' {id : Photo ID}'.
' {--dirty : Show Dirty}';
protected $signature = 'catalog:scan'
.' {type : Photo | Video }'
.' {id : Photo ID}'
.' {--dirty : Show Dirty}';
/**
* The console command description.

View File

@@ -1,149 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Models\{Person,Video,Tag};
use App\Traits\Files;
class VideoImport extends Command
{
use DispatchesJobs;
use Files;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'video:import
{--dir= : Directory to Parse}
{--file= : File to Import}
{--ignoredupe : Ignore duplicate files}
{--deletedupe : Delete duplicate files}
{--dumpid3 : Dump ID3 data}
{--people= : People to reference in video}
{--tags= : Add tag to video}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import videos into the database';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$files = $this->getFiles([
'dir'=>$this->option('dir'),
'file'=>$this->option('file')
],'video');
if (! count($files))
exit;
// 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'));
$t = Tag::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $t OR count($t) != count($tags)) {
$this->error(sprintf('Tag [%s] dont exist',join('|',$tags)));
abort(501);
}
}
// 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)));
abort(501);
}
}
$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('video.import.accepted'))) {
$this->warn(sprintf('Ignoring [%s]',$file));
continue;
}
if ($this->option('verbose'))
$this->info(sprintf('Processing file [%s]',$file));
$c++;
$o = Video::where('filename',$file)->first();
// The video doesnt exist
if (is_null($o)) {
$o = new Video;
$o->filename = $file;
}
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
// Make sure we can read the video.
if (! is_readable($o->file_path())) {
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
continue;
}
$o->save();
if ($o->wasRecentlyCreated)
$this->info(sprintf('%s [%s] stored in DB: %s',$o->objectType(),$file,$o->id));
// Record our people and tags
if ($p)
$o->People()->sync($p);
if ($t)
$o->Tags()->sync($t);
$this->dispatch((new \App\Jobs\CatalogScan($o))->onQueue('scan'));
if ($this->option('dumpid3'))
dd($o->properties());
}
$bar->finish();
return $this->info(sprintf('Videos processed: %s',$c));
}
}

View File

@@ -1,102 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use App\Traits\Files;
use App\Models\Video;
class VideoMove extends Command
{
use Files;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'video:move
{--file= : Video File}
{--id= : Video ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Moves Videos to their new location';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if ($this->option('file')) {
$vo = Video::notRemove()->notDuplicate()->where('filename',Video::path($this->option('file')));
} elseif ($this->option('id')) {
$vo = Video::notRemove()->notDuplicate()->where('id',$this->option('id'));
} else {
$vo = Video::notRemove()->notDuplicate();
}
if (! $vo)
exit;
// Show a progress bar
$bar = $this->output->createProgressBar($vo->count());
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$bar->setRedrawFrequency(100);
$vo->chunk(1,function($video) use ($bar) {
while ($o = $video->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], movable returned (%s)',$o->id,$o->file_path(),$o->file_path(FALSE,TRUE),$x));
if ($x == 1 AND $v = Video::where('filename',$o->file_path(TRUE,TRUE))->first())
$this->warn(sprintf('File is id (%s) [%s]',$v->file_path(),$v->id));
}
}
}
$bar->advance();
});
}
}