clrghouz/app/Classes/File/Send.php

342 lines
7.7 KiB
PHP
Raw Normal View History

2021-04-01 10:59:15 +00:00
<?php
namespace App\Classes\File;
use Exception;
use Illuminate\Support\Facades\Log;
2022-11-01 11:24:36 +00:00
use League\Flysystem\UnreadableFileEncountered;
2021-04-01 10:59:15 +00:00
use App\Classes\File\Send\Dynamic;
use App\Classes\Node;
2021-07-17 05:48:07 +00:00
use App\Models\Address;
2021-04-01 10:59:15 +00:00
/**
* Object representing the files we are sending
*
* @property-read resource fd
* @property-read int files_size The size of the files waiting to be sent
* @property-read int mail_size The size of the mail waiting to be sent
* @property-read int togo_count The total number of items that havent been sent yet
* @property-read int total_size The size of the items waiting to be sent
2021-04-01 10:59:15 +00:00
* @property-read int total_sent
* @property-read int total_sent_bytes
*/
class Send extends Base
2021-04-01 10:59:15 +00:00
{
2021-08-18 12:11:16 +00:00
private const LOGKEY = 'IS-';
public const T_NONE = 0;
/** @var int This file contains a file from the DB */
public const T_FILE = (1<<0);
/** @var int This file contains a bundle of Netmail */
public const T_NETMAIL = (1<<1);
/** @var int This file contains a bundle of Echomail */
public const T_ECHOMAIL = (1<<2);
2021-04-01 10:59:15 +00:00
private string $comp_data;
protected int $size = 0;
2021-04-01 10:59:15 +00:00
public function __construct()
{
// Initialise our variables
if (get_class($this) === self::class) {
$this->list = collect();
$this->f = NULL;
}
2021-04-01 10:59:15 +00:00
}
public function __get($key)
{
switch ($key) {
case 'completed':
return $this->list
->filter(function($item) { return $item->complete === TRUE; });
2021-04-01 10:59:15 +00:00
case 'fd':
return ! is_null($this->index);
2021-04-01 10:59:15 +00:00
case 'files_count':
2021-04-01 10:59:15 +00:00
return $this->list
->filter(function($item) { return $item->isType(self::IS_FILE|self::IS_TIC); })
2021-04-01 10:59:15 +00:00
->count();
case 'files_size':
2021-04-01 10:59:15 +00:00
return $this->list
->filter(function($item) { return $item->isType(self::IS_FILE|self::IS_TIC); })
->sum(function($item) { return $item->size; });
2021-04-01 10:59:15 +00:00
case 'mail_count':
return $this->list
->filter(function($item) { return $item->isType(self::IS_ARC|self::IS_PKT); })
->count();
2021-04-01 10:59:15 +00:00
case 'mail_size':
return $this->list
->filter(function($item) { return $item->isType(self::IS_ARC|self::IS_PKT); })
->sum(function($item) { return $item->size; });
2021-04-01 10:59:15 +00:00
case 'name':
case 'nameas':
2021-04-01 10:59:15 +00:00
case 'mtime':
case 'size':
case 'type':
return $this->sending->{$key};
case 'pos':
return $this->{$key};
case 'sending':
return $this->list->get($this->index);
2021-04-01 10:59:15 +00:00
case 'togo_count':
2021-04-01 10:59:15 +00:00
return $this->list
->filter(function($item) { return $item->complete === FALSE; })
->count();
2021-04-01 10:59:15 +00:00
case 'total_sent':
return $this->completed
->count();
2021-04-01 10:59:15 +00:00
case 'total_sent_bytes':
return $this->completed
->sum(function($item) { return $item->size; });
2021-04-01 10:59:15 +00:00
case 'total_size':
return $this->list
->sum(function($item) { return $item->size; });
2021-04-01 10:59:15 +00:00
default:
throw new Exception('Unknown key: '.$key);
}
}
/**
* Close the file descriptor of the file we are sending
2021-04-01 10:59:15 +00:00
*
* @param bool $successful
* @param Node $node
2021-04-01 10:59:15 +00:00
* @throws Exception
*/
public function close(bool $successful,Node $node): void
2021-04-01 10:59:15 +00:00
{
if (! $this->fd)
throw new Exception('No file to close');
2021-04-01 10:59:15 +00:00
if ($successful) {
$end = time()-$this->start;
Log::info(sprintf('%s:- Closing [%s], sent in [%d] with [%s] items',self::LOGKEY,$this->sending->nameas,$end,$this->sending->dbids->count()));
} else {
Log::alert(sprintf('%s:- Closing [%s], file NOT SENT successfully',self::LOGKEY,$this->sending->nameas));
2021-04-01 10:59:15 +00:00
}
$this->sending->close($successful,$node);
$this->index = NULL;
2021-04-01 10:59:15 +00:00
}
public function dynamic(Address $ao): bool
{
$file = FALSE;
// If the node is marked as hold - dont send any files.
if ($ao->system->hold) {
Log::info(sprintf('%s:- System [%d] is marked as hold - not checking for files.',self::LOGKEY,$ao->system_id));
return FALSE;
}
// Files
if (($x=$ao->dynamicWaiting())->count()) {
Log::debug(sprintf('%s:- [%d] Dynamic Files(s) added for sending to [%s]',self::LOGKEY,$x->count(),$ao->ftn));
// Add Files
foreach ($x as $do)
$this->list->push(new Dynamic($do,$ao,self::T_FILE));
$file = TRUE;
}
return $file;
}
/*
private function compress(string $comp_mode): void
{
switch ($comp_mode) {
case 'BZ2':
$this->comp_data = bzcompress($buf);
break;
case 'GZ':
$this->comp_data = gzcompress($buf);
break;
}
}
*/
2021-04-01 10:59:15 +00:00
/**
* Check if we are at the end of the file
*
* @return bool
*/
public function feof(): bool
{
return $this->sending->feof();
}
/**
* Add our mail to the send queue
*
* @param Address $ao
* @return bool
* @throws Exception
*/
public function files(Address $ao): bool
{
$file = FALSE;
// If the node is marked as hold - dont send any files.
if ($ao->system->hold) {
Log::info(sprintf('%s:- System [%d] is marked as hold - not checking for files.',self::LOGKEY,$ao->system_id));
return FALSE;
}
// Files
if (($x=$ao->getFiles())->count()) {
Log::info(sprintf('%s:- [%d] Files(s) added for sending to [%s]',self::LOGKEY,$x->count(),$ao->ftn));
// Add Files
foreach ($x as $fo) {
$this->list->push(new File($fo,self::T_FILE));
$this->list->push(new Tic($fo,$ao,self::T_NONE));
}
$file = TRUE;
}
return $file;
2021-04-01 10:59:15 +00:00
}
/**
* Open a file for sending
*
* @param string $compress
2021-04-01 10:59:15 +00:00
* @return bool
* @throws Exception
*/
public function open(string $compress=''): bool
2021-04-01 10:59:15 +00:00
{
Log::debug(sprintf('%s:+ File Open to send',self::LOGKEY));
2021-04-01 10:59:15 +00:00
if ((($this->index=$this->list->search(function($item) { return $item->complete === FALSE; })) !== FALSE)
&& $this->sending->open())
2021-07-17 05:48:07 +00:00
{
Log::info(sprintf('%s:- Content Send item [#%d] (%s) with size [%d]',self::LOGKEY,$this->index,$this->sending->nameas,$this->sending->size));
$this->pos = 0;
2021-07-17 05:48:07 +00:00
$this->start = time();
/*
if ($compress)
$this->comp_data = $this->compdata($compress);
*/
2021-07-17 05:48:07 +00:00
return TRUE;
} else {
Log::error(sprintf('%s:- No files to open',self::LOGKEY));
$this->index = NULL;
return FALSE;
}
2021-04-01 10:59:15 +00:00
}
2021-07-17 05:48:07 +00:00
/**
* Add our mail to the send queue
*
* @param Address $ao
* @return bool
2021-07-17 05:48:07 +00:00
* @throws Exception
*/
public function mail(Address $ao): bool
2021-07-17 05:48:07 +00:00
{
$mail = FALSE;
2022-01-15 11:38:45 +00:00
// If the node is marked as hold - dont send any mail.
if ($ao->system->hold) {
2023-10-04 04:49:44 +00:00
Log::info(sprintf('%s:- System [%d] is marked as hold - not checking for mail.',self::LOGKEY,$ao->system_id));
return FALSE;
2022-01-15 11:38:45 +00:00
}
2021-07-17 05:48:07 +00:00
// Netmail
if ($x=$ao->getNetmail()) {
2023-10-04 04:49:44 +00:00
Log::debug(sprintf('%s:- Netmail(s) added for sending to [%s]',self::LOGKEY,$ao->ftn));
$this->list->push(new Mail($x,self::T_NETMAIL));
$mail = TRUE;
}
2021-09-06 13:39:32 +00:00
// Echomail
if ($x=$ao->getEchomail()) {
2023-10-04 04:49:44 +00:00
Log::debug(sprintf('%s:- Echomail(s) added for sending to [%s]',self::LOGKEY,$ao->ftn));
2021-09-06 13:39:32 +00:00
$this->list->push(new Mail($x,self::T_ECHOMAIL));
$mail = TRUE;
2021-09-06 13:39:32 +00:00
}
return $mail;
2021-07-17 05:48:07 +00:00
}
2021-04-01 10:59:15 +00:00
/**
* Read bytes of the sending file
*
* @param int $length
* @return string|null
2022-11-01 11:24:36 +00:00
* @throws UnreadableFileEncountered
2021-04-01 10:59:15 +00:00
* @throws Exception
*/
public function read(int $length): ?string
{
if (! $this->fd)
2021-04-01 10:59:15 +00:00
throw new Exception('No file open for read');
$data = $this->sending->read($length);
2021-04-01 10:59:15 +00:00
if ($data === FALSE)
throw new UnreadableFileEncountered('Error reading file: '.$this->sending->name);
2021-04-01 10:59:15 +00:00
$this->pos += strlen($data);
Log::debug(sprintf('%s:- Content Read [%d] bytes, pos now [%d]',self::LOGKEY,strlen($data),$this->pos));
2021-04-01 10:59:15 +00:00
return $data;
}
/**
* Seek to a specific position of our file
*
* @param int $pos
* @return bool
* @throws Exception
*/
public function seek(int $pos): bool
{
if (! $this->fd)
2021-04-01 10:59:15 +00:00
throw new Exception('No file open for seek');
if ($this->sending->seek($pos)) {
$this->pos = $pos;
2021-07-17 05:48:07 +00:00
Log::debug(sprintf('%s:= Content Seek to [%d]',self::LOGKEY,$this->pos));
2021-07-17 05:48:07 +00:00
return TRUE;
2021-04-01 10:59:15 +00:00
} else {
Log::error(sprintf('%s:! Failed to seek to [%d]',self::LOGKEY,$pos));
2021-04-01 10:59:15 +00:00
return FALSE;
}
2021-04-01 10:59:15 +00:00
}
}