photo/app/Console/Commands/Update.php

136 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use Log;
use Illuminate\Console\Command;
use App\Model\Photo;
class Update extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'photo:update
{--dir= : Directory to Parse}
{--file= : File to import}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update Signatures';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Make sure we got a directory or a file to import
if (is_null($this->option('file')) AND is_null($this->option('dir')))
abort(500,'Missing filename, please use --file= OR --dir=');
Log::info(sprintf('%s: Processing: %s',__METHOD__,($this->option('file') ? $this->option('file') : $this->option('dir'))));
$files = [];
if ($this->option('dir'))
{
// Remove our trailing slash from the directory.
$dir = preg_replace('/\/$/','',$this->option('dir'));
// Exclude . & .. from the path.
$files = array_diff(scandir($dir),array('.','..'));
}
elseif ($this->option('file'))
{
$dir = dirname($this->option('file'));
$files = array(basename($this->option('file')));
}
// Determine if our dir is releative to where we store data
$dir = Photo::path($dir);
// Add our path
if ($dir)
array_walk($files,function(&$value,$key,$path='') {
if ($path) {
$value = sprintf('%s/%s',$path,$value);
}
},$dir);
// Show a progress bar
$bar = $this->output->createProgressBar(count($files));
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$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++;
$po = Photo::where('filename',$file)->first();
if (is_null($po))
{
$this->error(sprintf('File is not in the database [%s]?',$file));
Log::error(sprintf('%s: File is not in the database [%s]?',__METHOD__,$file));
continue;
}
$po->signature = $po->property('signature');
try {
$po->thumbnail = exif_thumbnail($po->file_path());
} catch (\Exception $e) {
// @todo Couldnt get the thumbnail, so we should create one.
}
if ($po->isDirty())
{
if (count($po->getDirty()) > 1 OR ! array_key_exists('signature',$po->getDirty()))
$this->error(sprintf('More than the signature changed for [%s] (%s)?',$po->id,join('|',array_keys($po->getDirty()))));
$this->info(sprintf('Signature update for [%s]',$po->id));
$po->save();
}
}
$bar->finish();
return $this->info(sprintf('Images processed: %s',$c));
}
}