2022-11-01 11:24:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2023-09-23 12:01:18 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Collection;
|
2022-11-01 11:24:36 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-02 10:20:02 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2024-11-04 07:25:49 +00:00
|
|
|
use App\Models\Casts\{CompressedStringOrNull,CollectionOrNull};
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
class File extends Model
|
|
|
|
{
|
2024-06-01 00:46:02 +00:00
|
|
|
use SoftDeletes;
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
private const LOGKEY = 'MF-';
|
|
|
|
private bool $no_export = FALSE;
|
2023-11-21 23:40:15 +00:00
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
public Collection $set_path;
|
|
|
|
public Collection $set_seenby;
|
2023-11-21 23:40:15 +00:00
|
|
|
public string $src_file = '';
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'kludges' => CollectionOrNull::class,
|
2023-06-26 00:32:38 +00:00
|
|
|
'datetime' => 'datetime:Y-m-d H:i:s',
|
2024-06-01 00:46:02 +00:00
|
|
|
'desc' => CompressedStringOrNull::class,
|
|
|
|
'ldesc' => CompressedStringOrNull::class,
|
2023-06-26 00:32:38 +00:00
|
|
|
'rogue_seenby' => CollectionOrNull::class,
|
|
|
|
'rogue_path' => CollectionOrNull::class,
|
2022-11-01 11:24:36 +00:00
|
|
|
'size' => 'int',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::creating(function($model) {
|
2023-09-23 12:01:18 +00:00
|
|
|
if (! $model->filearea_id) {
|
|
|
|
Log::alert(sprintf('%s:- File has no filearea, not processing creating [%s]',self::LOGKEY,$model->name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-21 23:40:15 +00:00
|
|
|
Log::info(sprintf('%s:- Storing file [%s] in [%s]',self::LOGKEY,$model->src_file,$model->rel_name));
|
2023-09-23 12:01:18 +00:00
|
|
|
|
|
|
|
$srcfs = Storage::disk(config('fido.local_disk'));
|
|
|
|
$tgtfs = Storage::disk(config('fido.file_disk'));
|
|
|
|
|
|
|
|
// Delete anything being replaced
|
|
|
|
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();
|
|
|
|
}
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
// Store file
|
2023-11-21 23:40:15 +00:00
|
|
|
if ($tgtfs->put($model->rel_name,$srcfs->get($model->src_file),'public')) {
|
|
|
|
$srcfs->delete($model->src_file);
|
2023-09-23 12:01:18 +00:00
|
|
|
|
2022-11-02 10:20:02 +00:00
|
|
|
} else {
|
2023-11-21 23:40:15 +00:00
|
|
|
throw new \Exception(sprintf('Unable to move file [%s] to [%s]',$model->src_file,$model->rel_name));
|
2023-06-27 07:39:11 +00:00
|
|
|
}
|
2022-11-01 11:24:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// @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) {
|
2023-09-23 12:01:18 +00:00
|
|
|
Log::alert(sprintf('%s:- File has no filearea, not exporting [%d]',self::LOGKEY,$model->id));
|
2022-11-01 11:24:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
$rogue = collect();
|
|
|
|
$seenby = collect();
|
|
|
|
$path = collect();
|
|
|
|
|
|
|
|
$zone = $model->fftn->zone;
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
// Parse PATH
|
2023-11-23 02:17:02 +00:00
|
|
|
/**
|
|
|
|
* Path 21:4/106.0 @231005001126 PST+7 Foobar
|
|
|
|
* Path 21:1/100 1696489954 Thu Oct 05 07:12:34 2023 UTC htick/lnx 1.9 2022-07-03
|
|
|
|
*/
|
2023-09-23 12:01:18 +00:00
|
|
|
foreach ($model->set_path as $line) {
|
|
|
|
$matches = [];
|
2023-11-23 02:17:02 +00:00
|
|
|
preg_match(sprintf('#^(%s)\ ((@?)(\d+)(\ ([A-Z]{3}([\+\-][0-9]+)))?)\ ?(.*)$#',Address::ftn_regex),$line,$matches);
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2024-11-19 11:53:33 +00:00
|
|
|
// If our domain is blank, get the model's domain
|
|
|
|
if (! Arr::get($matches,6))
|
|
|
|
$matches[1] .= '@'.$model->filearea->domain->name;
|
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
if ($x=Arr::get($matches,1)) {
|
|
|
|
$ftn = Address::parseFTN($x);
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
// 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);
|
2022-11-03 11:05:49 +00:00
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
if (! $ao)
|
|
|
|
$ao = Address::createFTN($x,System::createUnknownSystem());
|
|
|
|
|
2023-11-23 02:17:02 +00:00
|
|
|
$datetime = ($matches[8] === '@')
|
|
|
|
? Carbon::createFromFormat('ymdHis',$matches[9],$matches[12])
|
|
|
|
: Carbon::createFromTimestamp($matches[7]);
|
|
|
|
|
|
|
|
$path->push(['address'=>$ao,'datetime'=>$datetime,'extra'=>$matches[13]]);
|
2023-09-23 12:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
// Save the Path
|
|
|
|
$ppoid = NULL;
|
2023-09-23 12:01:18 +00:00
|
|
|
foreach ($path as $item) {
|
2022-11-01 11:24:36 +00:00
|
|
|
$po = DB::select('INSERT INTO file_path (file_id,address_id,parent_id,datetime,extra) VALUES (?,?,?,?,?) RETURNING id',[
|
|
|
|
$model->id,
|
2023-09-23 12:01:18 +00:00
|
|
|
$item['address']->id,
|
2022-11-01 11:24:36 +00:00
|
|
|
$ppoid,
|
2023-09-23 12:01:18 +00:00
|
|
|
$item['datetime'],
|
|
|
|
$item['extra'],
|
2022-11-01 11:24:36 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$ppoid = $po[0]->id;
|
|
|
|
}
|
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
// 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) {
|
2024-11-19 11:53:33 +00:00
|
|
|
if (! preg_match('/@([a-zA-Z0-9\-_~]{0,8})/',$sb))
|
|
|
|
$sb .= '@'.$model->filearea->domain->name;
|
|
|
|
|
2023-10-29 10:26:57 +00:00
|
|
|
$ftn = Address::parseFTN($sb);
|
2023-09-23 12:01:18 +00:00
|
|
|
|
|
|
|
$ao = ($zone->domain->flatten)
|
|
|
|
? Address::findZone($zone->domain,$ftn['n'],$ftn['f'],0)
|
2023-10-29 10:26:57 +00:00
|
|
|
: Address::findFTN($sb);
|
2023-09-23 12:01:18 +00:00
|
|
|
|
|
|
|
if ($ao)
|
|
|
|
$seenby->push($ao->id);
|
|
|
|
else
|
|
|
|
$rogue->push($sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->rogue_seenby = $rogue;
|
|
|
|
|
|
|
|
$model->seenby()->sync($seenby);
|
|
|
|
$model->save();
|
|
|
|
|
2023-09-05 09:57:34 +00:00
|
|
|
// See if we need to export this file.
|
2023-07-29 03:17:36 +00:00
|
|
|
if ($model->filearea->sec_read) {
|
|
|
|
$exportto = $model
|
|
|
|
->filearea
|
|
|
|
->addresses
|
2024-04-14 11:16:33 +00:00
|
|
|
->filter(function($item) use ($model) { return $model->filearea->can_read($item->security); })
|
2023-07-29 03:17:36 +00:00
|
|
|
->pluck('id')
|
2023-10-29 10:26:57 +00:00
|
|
|
->diff($seenby);
|
2023-07-29 03:17:36 +00:00
|
|
|
|
|
|
|
if ($exportto->count()) {
|
|
|
|
if ($model->no_export) {
|
2023-09-08 11:11:53 +00:00
|
|
|
Log::alert(sprintf('%s:- NOT processing exporting of message by configuration [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
|
2023-07-29 03:17:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-08 11:11:53 +00:00
|
|
|
Log::info(sprintf('%s:- Exporting file [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
|
2023-07-29 03:17:36 +00:00
|
|
|
|
|
|
|
// Save the seenby for the exported systems
|
|
|
|
$model->seenby()->syncWithPivotValues($exportto,['export_at'=>Carbon::now()],FALSE);
|
2022-11-01 11:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* RELATIONS */
|
|
|
|
|
|
|
|
public function filearea()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Filearea::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fftn()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Address::class)
|
|
|
|
->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function seenby()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Address::class,'file_seenby')
|
|
|
|
->ftnOrder();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function path()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Address::class,'file_path')
|
2023-06-22 07:36:22 +00:00
|
|
|
->withPivot(['id','parent_id','datetime','extra']);
|
2022-11-01 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 10:20:02 +00:00
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
2023-11-21 23:40:15 +00:00
|
|
|
public function getOriginAttribute(): Address
|
2023-09-22 04:45:44 +00:00
|
|
|
{
|
2023-12-12 21:58:13 +00:00
|
|
|
return $this->path->where('pivot.parent_id','=',NULL)->pop();
|
2023-09-22 04:45:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
/**
|
2023-11-21 23:40:15 +00:00
|
|
|
* Return the relative path to Storage::disk() in the store
|
2023-09-23 12:01:18 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-11-21 23:40:15 +00:00
|
|
|
public function getRelNameAttribute(): string
|
2023-09-22 04:45:44 +00:00
|
|
|
{
|
2023-11-21 23:40:15 +00:00
|
|
|
return sprintf('%04X/%s',$this->filearea_id,$this->name);
|
2023-09-22 04:45:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 11:24:36 +00:00
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
public function jsonSerialize(): array
|
|
|
|
{
|
|
|
|
return $this->encode();
|
|
|
|
}
|
2023-10-29 10:26:57 +00:00
|
|
|
}
|