Rework TIC processing to use Storage::disk(). Implemented handling of replaces and files that already exist
This commit is contained in:
@@ -5,6 +5,8 @@ namespace App\Models;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -20,7 +22,8 @@ class File extends Model
|
||||
private const LOGKEY = 'MF-';
|
||||
private bool $no_export = FALSE;
|
||||
public string $prefix = '';
|
||||
public string $replaces = '';
|
||||
public Collection $set_path;
|
||||
public Collection $set_seenby;
|
||||
|
||||
protected $casts = [
|
||||
'kludges' => CollectionOrNull::class,
|
||||
@@ -37,82 +40,108 @@ class File extends Model
|
||||
'ldesc',
|
||||
];
|
||||
|
||||
public function __set($key,$value)
|
||||
{
|
||||
switch ($key) {
|
||||
case 'fullname':
|
||||
case 'replaces':
|
||||
case 'no_export':
|
||||
case 'set_path':
|
||||
case 'set_seenby':
|
||||
$this->{$key} = $value;
|
||||
break;
|
||||
|
||||
default:
|
||||
parent::__set($key,$value);
|
||||
}
|
||||
}
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function($model) {
|
||||
Log::info(sprintf('%s:- Storing file [%s] in [%s]',self::LOGKEY,$model->fullname,$model->full_storage_path));
|
||||
|
||||
// Store file
|
||||
if (Storage::put($model->full_storage_path,Storage::disk('local')->get($model->fullname),'public')) {
|
||||
unlink(Storage::disk('local')->path($model->fullname));
|
||||
} else {
|
||||
throw new \Exception(sprintf('Unable to move file [%s] to [%s]',$model->fullname,$model->full_storage_path));
|
||||
if (! $model->filearea_id) {
|
||||
Log::alert(sprintf('%s:- File has no filearea, not processing creating [%s]',self::LOGKEY,$model->name));
|
||||
return;
|
||||
}
|
||||
|
||||
Log::info(sprintf('%s:- Storing file [%s] in [%s]',self::LOGKEY,$model->recvd_rel_name,$model->rel_name));
|
||||
|
||||
$srcfs = Storage::disk(config('fido.local_disk'));
|
||||
$tgtfs = Storage::disk(config('fido.file_disk'));
|
||||
|
||||
// Delete anything being replaced
|
||||
// @todo implement replace
|
||||
foreach (self::where('name',$model->name)->where('filearea_id',$model->filearea_id)->get() as $fo) {
|
||||
Log::info(sprintf('%s:%% Deleting old file record [%d] for file [%s]',self::LOGKEY,$fo->id,$fo->rel_name));
|
||||
|
||||
$tgtfs->move($fo->rel_name,$fo->relname.'.'.$fo->id);
|
||||
$fo->delete();
|
||||
}
|
||||
|
||||
// Store file
|
||||
if ($tgtfs->put($model->rel_name,$srcfs->get($model->recvd_rel_name),'public')) {
|
||||
$srcfs->delete($model->recvd_rel_name);
|
||||
|
||||
} else {
|
||||
throw new \Exception(sprintf('Unable to move file [%s] to [%s]',$model->recvd_rel_name,$model->rel_name));
|
||||
}
|
||||
});
|
||||
|
||||
// @todo if the file is updated with new SEEN-BY's from another route, we'll delete the pending export for systems (if there is one)
|
||||
static::created(function($model) {
|
||||
if (! $model->filearea_id) {
|
||||
Log::alert(sprintf('%s:- File has no filearea, not exporting',self::LOGKEY,$model->id));
|
||||
Log::alert(sprintf('%s:- File has no filearea, not exporting [%d]',self::LOGKEY,$model->id));
|
||||
return;
|
||||
}
|
||||
|
||||
$rogue = collect();
|
||||
$seenby = collect();
|
||||
$path = collect();
|
||||
|
||||
$so = Setup::findOrFail(config('app.id'));
|
||||
$zone = $model->fftn->zone;
|
||||
|
||||
// Our address
|
||||
$ftns = $so
|
||||
->system
|
||||
->match($model->fftn->zone);
|
||||
// Parse PATH
|
||||
foreach ($model->set_path as $line) {
|
||||
$matches = [];
|
||||
preg_match(sprintf('#^(%s)\ ?([0-9]+)\ ?(.*)$#',Address::ftn_regex),$line,$matches);
|
||||
|
||||
// Add our address to the seenby;
|
||||
$model->set_seenby = $model->set_seenby->merge($ftns->pluck('id'))->unique();
|
||||
$model->set_path = $model->set_path->merge([[
|
||||
'address'=>$ftns->first(),
|
||||
'datetime'=>$x=Carbon::now(),
|
||||
'extra'=>sprintf('%s %s (%s)',$x->toRfc7231String(),$so::PRODUCT_NAME,$so->version),
|
||||
]]);
|
||||
if ($x=Arr::get($matches,1)) {
|
||||
$ftn = Address::parseFTN($x);
|
||||
|
||||
// Make sure all the path is in the seenby
|
||||
$model->set_seenby = $model->set_seenby->merge($model->set_path->pluck('address.id'))->unique()->filter();
|
||||
// If domain should be flattened, look for node regardless of zone (within the list of zones for the domain)
|
||||
$ao = ($zone->domain->flatten)
|
||||
? Address::findZone($zone->domain,$ftn['n'],$ftn['f'],0)
|
||||
: Address::findFTN($x);
|
||||
|
||||
// Save the seenby
|
||||
$model->seenby()->sync($model->set_seenby);
|
||||
if (! $ao)
|
||||
$ao = Address::createFTN($x,System::createUnknownSystem());
|
||||
|
||||
$path->push(['address'=>$ao,'datetime'=>Carbon::createFromTimestamp($matches[9]),'extra'=>$matches[10]]);
|
||||
}
|
||||
}
|
||||
|
||||
// Save the Path
|
||||
$ppoid = NULL;
|
||||
foreach ($model->set_path as $path) {
|
||||
foreach ($path as $item) {
|
||||
$po = DB::select('INSERT INTO file_path (file_id,address_id,parent_id,datetime,extra) VALUES (?,?,?,?,?) RETURNING id',[
|
||||
$model->id,
|
||||
$path['address']->id,
|
||||
$item['address']->id,
|
||||
$ppoid,
|
||||
Carbon::createFromTimestamp($path['datetime']),
|
||||
$path['extra'],
|
||||
$item['datetime'],
|
||||
$item['extra'],
|
||||
]);
|
||||
|
||||
$ppoid = $po[0]->id;
|
||||
}
|
||||
|
||||
// Make sure all the path is in the seenby
|
||||
// Add zone to seenby
|
||||
$model->set_seenby = $model->set_seenby->merge($path->pluck('address.ftn3d'))->unique()->filter();
|
||||
|
||||
foreach ($model->set_seenby as $sb) {
|
||||
$ftn = Address::parseFTN($x);
|
||||
|
||||
$ao = ($zone->domain->flatten)
|
||||
? Address::findZone($zone->domain,$ftn['n'],$ftn['f'],0)
|
||||
: Address::findFTN($x);
|
||||
|
||||
if ($ao)
|
||||
$seenby->push($ao->id);
|
||||
else
|
||||
$rogue->push($sb);
|
||||
}
|
||||
|
||||
$model->rogue_seenby = $rogue;
|
||||
|
||||
$model->seenby()->sync($seenby);
|
||||
$model->save();
|
||||
|
||||
// See if we need to export this file.
|
||||
if ($model->filearea->sec_read) {
|
||||
$exportto = $model
|
||||
@@ -169,7 +198,7 @@ class File extends Model
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFullStoragePathAttribute(): string
|
||||
public function getRelNameAttribute(): string
|
||||
{
|
||||
return sprintf('%04X/%s',$this->filearea_id,$this->name);
|
||||
}
|
||||
@@ -179,12 +208,17 @@ class File extends Model
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRelNameAttribute(): string
|
||||
public function getRecvdRelNameAttribute(): string
|
||||
{
|
||||
return sprintf('%s/%s',config('fido.dir'),$this->prefix_name);
|
||||
return sprintf('%s/%s',config('fido.dir'),$this->recvd_pref_name);
|
||||
}
|
||||
|
||||
public function getPrefixNameAttribute(): string
|
||||
/**
|
||||
* This is the name of the file, with the sender prefix
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRecvdPrefNameAttribute(): string
|
||||
{
|
||||
return sprintf('%s%s',$this->prefix ? $this->prefix.'-' : '',$this->name);
|
||||
}
|
||||
|
Reference in New Issue
Block a user