69 lines
1.5 KiB
PHP
69 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Log;
|
|
use App\Model\Photo;
|
|
|
|
trait Files
|
|
{
|
|
/**
|
|
* Get a list of files
|
|
*/
|
|
public function getFiles(array $option,$type)
|
|
{
|
|
// 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 = [];
|
|
$dir = '';
|
|
|
|
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'] AND file_exists($option['file']))
|
|
{
|
|
$dir = dirname($option['file']);
|
|
$files = array(basename($option['file']));
|
|
}
|
|
|
|
// Determine if our dir is releative 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);
|
|
|
|
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);
|
|
}
|
|
|
|
public static function path($path,$type)
|
|
{
|
|
return (strpos($path,config($type.'.dir').'/') === 0) ? str_replace(config($type.'.dir').'/','',$path) : $path;
|
|
}
|
|
}
|