Enable moving inbound files to s3 and logging in DB

This commit is contained in:
Deon George
2022-11-02 21:20:02 +11:00
parent 029a8a9d73
commit 7571a2cd7d
7 changed files with 764 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ use Carbon\Carbon;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\UnableToWriteFile;
use App\Classes\FTN as FTNBase;
@@ -109,7 +110,7 @@ class Tic extends FTNBase
break;
case 'file':
if (! file_exists($x=$this->fullpath(sprintf('%s-%s',$hex,$matches[2]))))
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));
$fo->{$k} = $matches[2];
@@ -178,7 +179,7 @@ class Tic extends FTNBase
fclose($f);
$f = fopen($fo->fullname,'rb');
$f = fopen($x=Storage::disk('local')->path($fo->fullname),'rb');
$stat = fstat($f);
fclose($f);
@@ -187,7 +188,7 @@ class Tic extends FTNBase
throw new \Exception(sprintf('TIC file size [%d] doesnt match file [%s] (%d)',$fo->size,$fo->fullname,$y));
// Validate CRC
if (sprintf('%x',$fo->crc) !== ($y=hash_file('crc32b',$fo->fullname)))
if (sprintf('%x',$fo->crc) !== ($y=hash_file('crc32b',$x)))
throw new \Exception(sprintf('TIC file CRC [%x] doesnt match file [%s] (%s)',$fo->crc,$fo->fullname,$y));
// Validate Password
@@ -220,8 +221,9 @@ class Tic extends FTNBase
$this->fo = $fo;
}
public function fullpath(string $file,string $prefix=NULL): string
public function isNodelist(): bool
{
return sprintf('storage/app/%s/%s',config('app.fido'),($prefix ? $prefix.'-' : '').$file);
return (($this->fo->nodelist_filearea_id === $this->fo->filearea->domain->filearea_id)
&& (preg_match(str_replace(['.','?'],['\.','.'],'#^'.$this->fo->filearea->domain->nodelist_filename.'$#i'),$this->fo->file)));
}
}

View File

@@ -9,6 +9,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Classes\FTN\Tic;
@@ -35,6 +36,10 @@ class TicProcess implements ShouldQueue
*/
public function handle()
{
new Tic($this->file);
$to = new Tic($this->file);
Log::info(sprintf('%s:Processed [%s] storing [%s] as id [%d]',self::LOGKEY,$this->file,$to->fo->file,$to->fo->id));
unlink($to->fullpath($this->file));
}
}

View File

@@ -7,7 +7,9 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File as FileFacade;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Rennokki\QueryCache\Traits\QueryCacheable;
use App\Casts\{CollectionOrNull,CompressedString};
@@ -58,13 +60,17 @@ class File extends Model
parent::boot();
static::creating(function($model) {
Log::debug(sprintf('%s:- Storing file [%s]',self::LOGKEY,$model->file));
Log::debug(sprintf('%s:- Storing file [%s] in [%s]',self::LOGKEY,$model->fullname,$model->full_storage_path));
// Store file
// Delete file from inbound
if (Storage::put($model->full_storage_path,Storage::disk('local')->get($model->fullname),'public')) {
unlink($model->fullname);
} else {
throw new \Exception(sprintf('Unable to move file [%s] to [%s]',$model->fullname,$model->full_storage_path));
};
// Delete anything being replaced
// @todo implement replace
});
// @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)
@@ -148,6 +154,13 @@ class File extends Model
->withPivot(['id','parent_id','extra']);
}
/* ATTRIBUTES */
public function getFullStoragePathAttribute(): string
{
return sprintf('%04X/%s',$this->filearea_id,$this->file);
}
/* METHODS */
public function jsonSerialize(): array