Support processing nodelists from file repository and assume nodelists are zip fiels
This commit is contained in:
@@ -9,18 +9,23 @@ use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
use App\Models\File;
|
||||
|
||||
trait Import
|
||||
{
|
||||
protected Collection $_columns; // Columns in the Import File
|
||||
|
||||
/**
|
||||
* Count the lines in a file
|
||||
* Assumes $file is compressed with ZIP
|
||||
*/
|
||||
private function getFileLines(string $file): int
|
||||
{
|
||||
$f = fopen($file,'r');
|
||||
$c = 0;
|
||||
|
||||
$f = NULL;
|
||||
$z = $this->openFile($file,$f);
|
||||
|
||||
while (! feof($f)) {
|
||||
fgets($f);
|
||||
$c++;
|
||||
@@ -31,6 +36,28 @@ trait Import
|
||||
return $c;
|
||||
}
|
||||
|
||||
private function openFile(string $file,&$f): \ZipArchive
|
||||
{
|
||||
$z = new \ZipArchive;
|
||||
|
||||
if ($z->open($file)) {
|
||||
if ($z->count() !== 1)
|
||||
throw new \Exception(sprintf('%s:File [%s] has more than 1 file (%d)', self::LOGKEY, $file, $z->count()));
|
||||
|
||||
$zipfile = $z->statIndex(0, \ZipArchive::FL_UNCHANGED);
|
||||
Log::debug(sprintf('%s:Looking at [%s] in archive [%s]', self::LOGKEY,$zipfile['name'],$file));
|
||||
|
||||
$f = $z->getStream($zipfile['name']);
|
||||
if (! $f)
|
||||
throw new \Exception(sprintf('%s:Failed getting ZipArchive::stream (%s)',self::LOGKEY,$z->getStatusString()));
|
||||
else
|
||||
return $z;
|
||||
|
||||
} else {
|
||||
throw new \Exception(sprintf('%s:Failed opening ZipArchive [%s] (%s)',self::LOGKEY,$file,$z->getStatusString()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the columns from the file that we'll work with
|
||||
*
|
||||
@@ -54,29 +81,12 @@ trait Import
|
||||
return ($x=$this->_columns->search(strtoupper($this->columns->get($key)))) !== FALSE ? $x : NULL;
|
||||
}
|
||||
|
||||
private function getFileFromHost(string $host,string $key,string $file): string
|
||||
private function getFileFromHost(string $key,File $file): string
|
||||
{
|
||||
$path = 'import/'.$key;
|
||||
$path = sprintf('import/%s.%d',$key,$file->id);
|
||||
|
||||
// If we are doing it locally, we dont need to retrieve it via curl
|
||||
if (! $host) {
|
||||
return preg_match('/^data/',$file) ? $file : sprintf('storage/app/public/%s/%s',$path,basename($file));
|
||||
}
|
||||
Storage::disk('local')->put($path,Storage::get($file->full_storage_path));
|
||||
|
||||
// Get the file from the host
|
||||
$srcfile = sprintf('http://%s%s',$host,$file);
|
||||
$dstfile = '/tmp/'.basename($host);
|
||||
Log::debug(sprintf('%s:Retrieving [%s] from [%s]',static::LOGKEY,$host,$file));
|
||||
|
||||
$src = fopen($srcfile,'r');
|
||||
$dst = fopen($dstfile,'w');
|
||||
stream_copy_to_stream($src,$dst);
|
||||
fclose($src);
|
||||
fclose($dst);
|
||||
|
||||
// Store the file for later processing
|
||||
Storage::putFileAs($path,$dstfile,basename($file));
|
||||
|
||||
return Storage::path($path.'/'.basename($file));
|
||||
return Storage::disk('local')->path($path);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user