Enabled move and queue processing

This commit is contained in:
Deon George 2016-06-29 14:04:02 +10:00
parent 4c317a811c
commit 3361427c75
8 changed files with 201 additions and 92 deletions

View File

@ -9,13 +9,9 @@ use App\Model\PhotoPerson;
use App\Model\Photo;
use App\Model\PhotoTag;
use App\Model\Tag;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\PhotoMove;
class Import extends Command
{
use DispatchesJobs;
/**
* The name and signature of the console command.
*
@ -77,7 +73,7 @@ class Import extends Command
}
// Determine if our dir is releative to where we store data
$dir = preg_replace('/^\//','',str_replace(config('photo.dir'),'',$dir));
$dir = Photo::path($dir);
// Add our path
if ($dir)
@ -150,8 +146,8 @@ class Import extends Command
$po->width = $po->io()->getImageWidth();
$po->orientation = $po->io()->getImageOrientation();
$po->gps_lat = $po->latlon(preg_split('/,\s?/',$po->io()->getImageProperty('exif:GPSLatitude')),$po->io()->getImageProperty('exif:GPSLatitudeRef'));
$po->gps_lon = $po->latlon(preg_split('/,\s?/',$po->io()->getImageProperty('exif:GPSLongitude')),$po->io()->getImageProperty('exif:GPSLongitudeRef'));
$po->gps_lat = Photo::latlon(preg_split('/,\s?/',$po->io()->getImageProperty('exif:GPSLatitude')),$po->io()->getImageProperty('exif:GPSLatitudeRef'));
$po->gps_lon = Photo::latlon(preg_split('/,\s?/',$po->io()->getImageProperty('exif:GPSLongitude')),$po->io()->getImageProperty('exif:GPSLongitudeRef'));
try {
$po->thumbnail = exif_thumbnail($po->file_path());
@ -218,8 +214,6 @@ class Import extends Command
$x->photo_id = $po->id;
$x->save();
}
$this->dispatch((new PhotoMove($po))->onQueue('move'));
}
$bar->finish();

View File

@ -0,0 +1,99 @@
<?php
namespace App\Console\Commands;
use DB;
use Log;
use Illuminate\Console\Command;
use App\Model\Photo;
class Move extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'photo:move
{--file= : Photo File}
{--id= : Photo ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Moves Photos 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'))
{
$po = Photo::notRemove()->notDuplicate()->where('filename',Photo::path($this->option('file')));
}
elseif ($this->option('id'))
{
$po = Photo::notRemove()->notDuplicate()->where('id',Photo::path($this->option('id')));
}
else
{
$po = Photo::notRemove()->notDuplicate();
}
// 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 ($po = $photo->shift())
{
if (($x = $po->moveable()) === TRUE) {
Log::info(sprintf('Moving: (%s)[%s]',$po->id,$po->filename));
if (rename($po->file_path(),$po->file_path(FALSE,TRUE)))
{
// Convert the path to a relative one.
$po->filename = $po->file_path(TRUE,TRUE);
// @todo If the DB update failed, move it back.
if (! $po->save()) # AND ! rename($path,$po->file_path()))
{
$this->error(sprintf('Save after rename failed for (%s)',$po->id));
continue;
}
}
else
{
$this->error(sprintf('Rename failed for (%s)',$po->id));
continue;
}
chmod($po->file_path(),0444);
}
else
{
if ($x > 0)
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], moveable returned (%s)',$po->id,$po->file_path(),$po->file_path(FALSE,TRUE),$x));
}
}
$bar->advance();
});
}
}

View File

@ -14,6 +14,7 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
Commands\Import::class,
Commands\Move::class,
];
/**

View File

@ -8,30 +8,32 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Model\Photo;
use Artisan;
class PhotoMove extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
use InteractsWithQueue, SerializesModels;
private $photo;
private $photo;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Photo $photo)
{
$this->photo = $photo;
}
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Photo $photo)
{
$this->photo = $photo;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info('Moving: '.$this->photo->id);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(__METHOD__.' Moving: '.$this->photo->id);
Artisan::call('photo:move',['--id' => $this->photo->id]);
}
}

View File

@ -19,6 +19,32 @@ class Photo extends Model
8=>-90,
];
/**
* Photo's NOT pending removal.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotRemove($query)
{
return $query->where(function($query) {
$query->where('remove','!=',TRUE)
->orWhere('remove','=',NULL);
});
}
/**
* Photo's NOT duplicate.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotDuplicate($query)
{
return $query->where(function($query) {
$query->where('duplicate','!=',TRUE)
->orWhere('duplicate','=',NULL);
});
}
public function date_taken() {
return $this->date_taken ? (date('Y-m-d H:i:s',$this->date_taken).($this->subsectime ? '.'.$this->subsectime : '')) : 'UNKNOWN';
}
@ -65,7 +91,7 @@ class Photo extends Model
/**
* Calculate the GPS coordinates
*/
public function latlon(array $coordinate,$hemisphere) {
public static function latlon(array $coordinate,$hemisphere) {
if (! $coordinate OR ! $hemisphere)
return NULL;
@ -89,6 +115,37 @@ class Photo extends Model
return round($sign*($degrees+$minutes/60+$seconds/3600),$degrees > 100 ? 3 : 4);
}
/**
* Determine if a file is moveable
*
* useID boolean Determine if the path is based on the the ID or date
*/
public function moveable() {
// If the source and target are the same, we dont need to move it
if ($this->file_path() == $this->file_path(FALSE,TRUE))
return FALSE;
// If there is already a file in the target.
// @todo If the target file is to be deleted, we could move this file
if (file_exists($this->file_path(FALSE,TRUE)))
return 1;
// Test if the source is readable
if (! is_readable($this->file_path()))
return 2;
// Test if the dir is writable (so we can remove the file)
if (! is_writable(dirname($this->file_path())))
return 3;
// Test if the target dir is writable
// @todo The target dir may not exist yet, so we should check that a parent exists and is writable.
if (! is_writable(dirname($this->file_path(FALSE,TRUE))))
return 4;
return TRUE;
}
/**
* Rotate the image
*
@ -101,6 +158,10 @@ class Photo extends Model
return $imo->getImageBlob();
}
public static function path($path) {
return preg_replace('/^\//','',str_replace(config('photo.dir'),'',$path));
}
/**
* Determine if the image should be moved
*/

View File

@ -2,10 +2,16 @@
namespace App\Providers;
use Log;
use Illuminate\Support\ServiceProvider;
use App\Model\Photo;
use App\Jobs\PhotoMove;
use Illuminate\Foundation\Bus\DispatchesJobs;
class AppServiceProvider extends ServiceProvider
{
use DispatchesJobs;
/**
* Bootstrap any application services.
*
@ -13,7 +19,14 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
// Any photo saved, queue it to be moved.
Photo::saved(function($photo) {
if (! $photo->duplicate AND ($x=$photo->moveable()) === TRUE)
{
Log::info(__METHOD__.': Need to Move: '.$photo->id.'|'.serialize($x));
$this->dispatch((new PhotoMove($photo))->onQueue('move'));
}
});
}
/**

View File

@ -13,9 +13,6 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
/**

View File

@ -1,58 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Mark all accounts that have no outstanding invoices and active services as disabled.
*
* @package Photo
* @category Tasks
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Task_Photo_Move extends Minion_Task {
protected $_options = array(
'file'=>NULL, // Photo File to Move
'batch'=>NULL, // Number of photos to move in a batch
'useid'=>TRUE, // If date not in photo use ID
'verbose'=>FALSE, // Show some debuggig
);
protected function _execute(array $params) {
if ($params['file']) {
$po = ORM::factory('Photo',array('filename'=>$params['file']));
} else {
$p = ORM::factory('Photo')
->where_open()
->where('remove','!=',TRUE)
->or_where('remove','is',NULL)
->where_close()
->where_open()
->where('duplicate','!=',TRUE)
->or_where('duplicate','is',NULL)
->where_close();
}
$c = 0;
foreach ($p->find_all() as $po) {
if ($po->file_path() == $po->file_path(FALSE,($params['useid'] OR $po->date_taken) ? TRUE : FALSE))
continue;
if ($params['verbose'])
printf("Processing [%s], file [%s] - newpath [%s]\n",$po->id,$po->file_path(),$po->file_path(FALSE,($params['useid'] OR $po->date_taken) ? TRUE : FALSE));
if ($po->move())
printf("Photo [%s] moved to %s.\n",$po->id,$po->file_path());
else
printf("Photo [%s] NOT moved to %s.\n",$po->id,$po->file_path(FALSE,TRUE));
$c++;
if (! is_null($params['batch']) AND $c >= $params['batch'])
break;
}
return sprintf("Images processed [%s]\n",$c);
}
}
?>