diff --git a/app/Console/Commands/Import.php b/app/Console/Commands/Import.php index ec17ae4..adfbcff 100644 --- a/app/Console/Commands/Import.php +++ b/app/Console/Commands/Import.php @@ -12,6 +12,8 @@ use App\Model\Tag; class Import extends Command { + use \App\Traits\Files; + /** * The name and signature of the console command. * @@ -49,39 +51,9 @@ class Import extends Command */ 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); + $files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')]); + if (! count($files)) + exit; // Show a progress bar $bar = $this->output->createProgressBar(count($files)); diff --git a/app/Console/Commands/Move.php b/app/Console/Commands/Move.php index c3ec50b..55bd7d2 100644 --- a/app/Console/Commands/Move.php +++ b/app/Console/Commands/Move.php @@ -9,6 +9,8 @@ use App\Model\Photo; class Move extends Command { + use \App\Traits\Files; + /** * The name and signature of the console command. * @@ -55,6 +57,9 @@ class Move extends Command $po = Photo::notRemove()->notDuplicate(); } + if (! $po) + exit; + // Show a progress bar $bar = $this->output->createProgressBar($po->count()); $bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) "); @@ -66,7 +71,7 @@ class Move extends Command if (($x = $po->moveable()) === TRUE) { Log::info(sprintf('%s: Moving (%s)[%s]',__METHOD__,$po->id,$po->filename)); - if (rename($po->file_path(),$po->file_path(FALSE,TRUE))) + if ($this->makeParentDir(dirname($po->file_path(FALSE,TRUE))) AND rename($po->file_path(),$po->file_path(FALSE,TRUE))) { // Convert the path to a relative one. $po->filename = $po->file_path(TRUE,TRUE); diff --git a/app/Console/Commands/Update.php b/app/Console/Commands/Update.php index 520c883..036427c 100644 --- a/app/Console/Commands/Update.php +++ b/app/Console/Commands/Update.php @@ -8,6 +8,8 @@ use App\Model\Photo; class Update extends Command { + use \App\Traits\Files; + /** * The name and signature of the console command. * @@ -41,39 +43,9 @@ class Update extends Command */ 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); + $files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')]); + if (! count($files)) + exit; // Show a progress bar $bar = $this->output->createProgressBar(count($files)); diff --git a/app/Model/Photo.php b/app/Model/Photo.php index 40a44d3..6590d24 100644 --- a/app/Model/Photo.php +++ b/app/Model/Photo.php @@ -139,6 +139,16 @@ class Photo extends Model return is_null($attr) ? $this->_io : $this->_io->getImageProperty($attr); } + public function isParentWritable($dir) + { + if (file_exists($dir) AND is_writable($dir) AND is_dir($dir)) + return TRUE; + elseif ($dir == dirname($dir) OR file_exists($dir)) + return FALSE; + else + return ($this->isParentWritable(dirname($dir))); + } + /** * Calculate the GPS coordinates */ @@ -189,12 +199,12 @@ class Photo extends Model return 2; // Test if the dir is writable (so we can remove the file) - if (! is_writable(dirname($this->file_path()))) + if (! $this->isParentWritable(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)))) + if (! $this->isParentWritable($this->file_path(FALSE,TRUE))) return 4; return TRUE; diff --git a/app/Traits/Files.php b/app/Traits/Files.php new file mode 100644 index 0000000..7a4b0fb --- /dev/null +++ b/app/Traits/Files.php @@ -0,0 +1,62 @@ +makeParentDir(dirname($dir))) + return FALSE; + else + return is_writable(dirname($dir)) AND mkdir($dir); + } +}