Update photo import to laravel 6.4

This commit is contained in:
Deon George
2019-11-09 13:52:04 +11:00
parent f6b456aa3d
commit 83d6d1e055
18 changed files with 274 additions and 253 deletions

View File

@@ -2,15 +2,15 @@
namespace App\Traits;
use Log;
use App\Model\Photo;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
trait Files
{
/**
* Get a list of files
*/
public function getFiles(array $option,$type)
public function getFiles(array $option,string $type): Collection
{
// Make sure we got a directory or a file to import
if (is_null($option['file']) AND is_null($option['dir']))
@@ -18,38 +18,42 @@ trait Files
Log::info(sprintf('%s: Processing: %s',__METHOD__,($option['file'] ? $option['file'] : $option['dir'])));
$files = [];
$files = collect();
$dir = '';
if ($option['dir'])
{
if ($option['dir'] AND is_dir($option['dir'])) {
// Remove our trailing slash from the directory.
$dir = preg_replace('/\/$/','',$option['dir']);
// Exclude . & .. from the path.
$files = array_diff(scandir($dir),array('.','..'));
$files = $files->merge(array_diff(scandir($dir),array('.','..')));
}
elseif ($option['file'] AND file_exists($option['file']))
{
} elseif ($option['file'] AND ! is_dir($option['file']) AND file_exists($option['file'])) {
$dir = dirname($option['file']);
$files = array(basename($option['file']));
$files->push(basename($option['file']));
}
// Determine if our dir is releative to where we store data
// Determine if our dir is relative to where we store data
$dir = static::path($dir,$type);
// Add our path
if ($dir)
array_walk($files,function(&$value,$key,$path='') {
if ($path) {
$value = sprintf('%s/%s',$path,$value);
}
},$dir);
$files->transform(function($value) use ($dir) {
return sprintf('%s/%s',$dir,$value);
});
Log::info(sprintf('%s: Processing: %s (%s)',__METHOD__,($option['file'] ? $option['file'] : $option['dir']),count($files)));
return $files;
}
/**
* Recursively make a parent dir to hold files.
*
* @param $dir
* @return bool
*/
public function makeParentDir($dir)
{
if (is_dir($dir))
@@ -61,8 +65,15 @@ trait Files
return is_writable(dirname($dir)) AND mkdir($dir);
}
public static function path($path,$type)
/**
* Return if the dir is a sub dir of our config.
*
* @param $path
* @param $type
* @return string
*/
public static function path($path,$type): string
{
return (strpos($path,config($type.'.dir').'/') === 0) ? str_replace(config($type.'.dir').'/','',$path) : $path;
}
}
}