Added Files Trait, added createParentDirs

This commit is contained in:
Deon George 2016-07-01 12:19:17 +10:00
parent 90c4468c43
commit b8be45b42c
5 changed files with 90 additions and 69 deletions

View File

@ -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));

View File

@ -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);

View File

@ -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));

View File

@ -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;

62
app/Traits/Files.php Normal file
View File

@ -0,0 +1,62 @@
<?php
namespace App\Traits;
use Log;
use App\Model\Photo;
trait Files
{
/**
* Get a list of files
*/
public function getFiles(array $option)
{
// Make sure we got a directory or a file to import
if (is_null($option['file']) AND is_null($option['dir']))
abort(500,'Missing filename, please use --file= OR --dir=');
Log::info(sprintf('%s: Processing: %s',__METHOD__,($option['file'] ? $option['file'] : $option['dir'])));
$files = [];
if ($option['dir'])
{
// Remove our trailing slash from the directory.
$dir = preg_replace('/\/$/','',$option['dir']);
// Exclude . & .. from the path.
$files = array_diff(scandir($dir),array('.','..'));
}
elseif ($option['file'])
{
$dir = dirname($option['file']);
$files = array(basename($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);
return $files;
}
public function makeParentDir($dir)
{
if (is_dir($dir))
return TRUE;
if (! file_exists(dirname($dir)) AND ! $this->makeParentDir(dirname($dir)))
return FALSE;
else
return is_writable(dirname($dir)) AND mkdir($dir);
}
}