Rework TIC processing to use Storage::disk(). Implemented handling of replaces and files that already exist

This commit is contained in:
2023-09-23 22:01:18 +10:00
parent 56544b89e1
commit ff04de52b5
10 changed files with 199 additions and 105 deletions

View File

@@ -4,10 +4,11 @@ namespace App\Classes\FTN;
use Carbon\Carbon;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\UnableToReadFile;
use App\Classes\FTN as FTNBase;
use App\Models\{Address,File,Filearea,Setup,System};
@@ -104,6 +105,8 @@ class Tic extends FTNBase
$result->put('SIZE',$fo->size);
if ($fo->description)
$result->put('DESC',$fo->description);
if ($fo->replaces)
$result->put('REPLACES',$fo->replaces);
$result->put('AREA',$fo->filearea->name);
$result->put('AREADESC',$fo->filearea->description);
if ($x=$ao->session('ticpass'))
@@ -117,9 +120,14 @@ class Tic extends FTNBase
foreach ($fo->path as $o)
$out .= sprintf("PATH %s %s %s\r\n",$o->ftn3d,$o->pivot->datetime,$o->pivot->extra);
// Add ourself to the path:
$out .= sprintf("PATH %s %s\r\n",$sysaddress->ftn3d,Carbon::now());
foreach ($fo->seenby as $o)
$out .= sprintf("SEENBY %s\r\n",$o->ftn3d);
$out .= sprintf("SEENBY %s\r\n",$sysaddress->ftn3d);
return $out;
}
@@ -137,33 +145,38 @@ class Tic extends FTNBase
/**
* Load a TIC file from an existing filename
*
* @param string $filename
* @param string $sp
* @param string $filename Relative to filesystem
* @return void
* @throws FileNotFoundException
*/
public function load(string $filename): void
{
Log::info(sprintf('%s:+ Processing TIC file [%s]',self::LOGKEY,$filename));
$fs = Storage::disk(config('fido.local_disk'));
if (str_contains($filename,'-')) {
list($hex,$name) = explode('-',$filename);
$hex = basename($hex);
} else {
$hex = '';
}
if (! file_exists($filename))
if (! $fs->exists($filename))
throw new FileNotFoundException(sprintf('File [%s] doesnt exist',$filename));
if (! is_readable($filename))
throw new UnableToWriteFile(sprintf('File [%s] is not readable',realpath($filename)));
if (! is_readable($fs->path($filename)))
throw new UnableToReadFile(sprintf('File [%s] is not readable',realpath($filename)));
$f = fopen($filename,'rb');
$f = $fs->readStream($filename);
if (! $f) {
Log::error(sprintf('%s:! Unable to open file [%s] for writing',self::LOGKEY,$filename));
Log::error(sprintf('%s:! Unable to open file [%s] for reading',self::LOGKEY,$filename));
return;
}
$ldesc = '';
while (! feof($f)) {
$line = chop(fgets($f));
$matches = [];
@@ -171,12 +184,13 @@ class Tic extends FTNBase
if (! $line)
continue;
preg_match('/([a-zA-Z]+)\ (.*)/',$line,$matches);
preg_match('/([a-zA-Z]+)\ ?(.*)?/',$line,$matches);
if (in_array(strtolower($matches[1]),$this->_kludge)) {
if (in_array(strtolower(Arr::get($matches,1,'-')),$this->_kludge)) {
switch ($k=strtolower($matches[1])) {
case 'area':
$this->{$k} = Filearea::singleOrNew(['name'=>strtoupper($matches[2])]);
break;
case 'origin':
@@ -184,19 +198,26 @@ class Tic extends FTNBase
case 'to':
$this->{$k} = Address::findFTN($matches[2]);
// @todo If $this->{$k} is null, we have discovered the system and it should be created
if (! $this->{$k})
Log::alert(sprintf('%s:! Unable to find an FTN for [%s] for the (%s)',self::LOGKEY,$matches[2],$k));
break;
case 'file':
if (! Storage::disk('local')->exists($x=sprintf('%s/%s-%s',config('app.fido'),$hex,$matches[2])))
throw new FileNotFoundException(sprintf('File not found? [%s]',$x));
$this->fo->name = $matches[2];
$this->fo->fullname = $x;
$this->fo->prefix = $hex;
if (! $fs->exists($this->fo->recvd_rel_name)) {
// @todo Fail this, so that it is rescheduled to try again in 1-24hrs.
throw new FileNotFoundException(sprintf('File not found? [%s]',$fs->path($this->fo->recvd_rel_name)));
}
break;
case 'areadesc':
$areadesc = $matches[2];
break;
case 'created':
@@ -205,10 +226,12 @@ class Tic extends FTNBase
case 'pw':
$pw = $matches[2];
break;
case 'lfile':
$this->fo->lname = $matches[2];
break;
case 'desc':
@@ -216,46 +239,36 @@ class Tic extends FTNBase
case 'replaces':
case 'size':
$this->fo->{$k} = $matches[2];
break;
case 'fullname':
$this->fo->lfile = $matches[2];
break;
case 'date':
$this->fo->datetime = Carbon::create($matches[2]);
$this->fo->datetime = Carbon::createFromTimestamp($matches[2]);
break;
case 'ldesc':
$this->fo->{$k} .= $matches[2];
$ldesc .= ($ldesc ? "\r" : '').$matches[2];
break;
case 'crc':
$this->fo->{$k} = hexdec($matches[2]);
break;
case 'path':
$x = [];
preg_match(sprintf('#^[Pp]ath (%s)\ ?([0-9]+)\ ?(.*)$#',Address::ftn_regex),$line,$x);
$ao = Address::findFTN($x[1]);
if (! $ao)
$ao = Address::createFTN($x[1],System::createUnknownSystem());
$this->fo->set_path->push(['address'=>$ao,'datetime'=>Carbon::createFromTimestamp($x[8]),'extra'=>$x[9]]);
$this->fo->set_path->push($matches[2]);
break;
case 'seenby':
$ao = Address::findFTN($matches[2]);
if (! $ao)
$ao = Address::createFTN($x[1],System::createUnknownSystem());
if (! $ao)
$this->fo->rogue_seenby->push($matches[2]);
else
$this->fo->set_seenby->push($ao->id);
$this->fo->set_seenby->push($matches[2]);
break;
}
@@ -265,19 +278,24 @@ class Tic extends FTNBase
}
}
fclose($f);
if ($ldesc)
$this->fo->ldesc = $ldesc;
$f = fopen($x=Storage::disk('local')->path($this->fo->fullname),'rb');
$stat = fstat($f);
fclose($f);
// @todo Add notifictions back to the system
if ($this->fo->replaces && (! preg_match('/^'.$this->fo->replaces.'$/',$this->fo->name))) {
Log::alert(sprintf('%s:! Regex [%s] doesnt match file name [%s]',self::LOGKEY,$this->fo->replaces,$this->fo->name));
$this->fo->replaces = NULL;
}
// Validate Size
if ($this->fo->size !== ($y=$stat['size']))
if ($this->fo->size !== ($y=$fs->size($this->fo->recvd_rel_name)))
throw new \Exception(sprintf('TIC file size [%d] doesnt match file [%s] (%d)',$this->fo->size,$this->fo->fullname,$y));
// Validate CRC
if (sprintf('%08x',$this->fo->crc) !== ($y=hash_file('crc32b',$x)))
if (sprintf('%08x',$this->fo->crc) !== ($y=$fs->checksum($this->fo->recvd_rel_name,['checksum_algo'=>'crc32b'])))
throw new \Exception(sprintf('TIC file CRC [%08x] doesnt match file [%s] (%s)',$this->fo->crc,$this->fo->fullname,$y));
// Validate Password
@@ -303,7 +321,7 @@ class Tic extends FTNBase
// If the file create time is blank, we'll take the files
if (! $this->fo->datetime)
$this->fo->datetime = Carbon::createFromTimestamp($stat['ctime']);
$this->fo->datetime = Carbon::createFromTimestamp($fs->lastModified($this->fo->recvd_rel_name));
$this->fo->save();
}