2021-04-01 10:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\File;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-06-22 07:36:22 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2022-11-01 11:24:36 +00:00
|
|
|
use League\Flysystem\UnreadableFileEncountered;
|
2021-04-01 10:59:15 +00:00
|
|
|
|
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 mail_size
|
|
|
|
* @property-read int total_count
|
|
|
|
* @property-read int total_sent
|
|
|
|
* @property-read int total_sent_bytes
|
|
|
|
*/
|
|
|
|
final class Send extends Item
|
|
|
|
{
|
2021-08-18 12:11:16 +00:00
|
|
|
private const LOGKEY = 'IS-';
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
private Collection $list;
|
|
|
|
private ?Item $sending;
|
2021-07-17 05:48:07 +00:00
|
|
|
private Collection $packets;
|
2021-04-01 10:59:15 +00:00
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
private string $comp_data;
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Initialise our variables
|
|
|
|
$this->list = collect();
|
2021-07-17 05:48:07 +00:00
|
|
|
$this->packets = collect();
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->sending = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'fd':
|
2021-07-18 12:10:21 +00:00
|
|
|
return is_resource($this->f) ?: $this->f;
|
2021-04-01 10:59:15 +00:00
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
case 'files_count':
|
2021-04-01 10:59:15 +00:00
|
|
|
return $this->list
|
2023-06-22 07:36:22 +00:00
|
|
|
->filter(function($item) { return $item->isType(self::IS_FILE|self::IS_TIC); })
|
2021-04-01 10:59:15 +00:00
|
|
|
->count();
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
case 'files_size':
|
2021-04-01 10:59:15 +00:00
|
|
|
return $this->list
|
2023-06-22 07:36:22 +00:00
|
|
|
->filter(function($item) { return $item->isType(self::IS_FILE|self::IS_TIC); })
|
2023-07-02 13:40:08 +00:00
|
|
|
->sum(function($item) { return $item->size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'filepos':
|
|
|
|
return $this->file_pos;
|
|
|
|
|
|
|
|
case 'mail_count':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return $item->isType(self::IS_ARC|self::IS_PKT); })
|
2021-07-17 05:48:07 +00:00
|
|
|
->count()
|
|
|
|
+ $this->packets->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); })
|
2023-07-02 13:40:08 +00:00
|
|
|
->sum(function($item) { return $item->size; })
|
|
|
|
+ $this->packets->sum(function($item) { return $item->size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'sendas':
|
|
|
|
return $this->sending ? $this->sending->{$key} : NULL;
|
|
|
|
|
|
|
|
case 'name':
|
|
|
|
case 'mtime':
|
|
|
|
case 'size':
|
|
|
|
return $this->sending ? $this->sending->{'file_'.$key} : NULL;
|
|
|
|
|
|
|
|
case 'total_sent':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
2021-07-17 05:48:07 +00:00
|
|
|
->count()
|
|
|
|
+ $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
|
|
|
->count();
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'total_sent_bytes':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
2023-07-02 13:40:08 +00:00
|
|
|
->sum(function($item) { return $item->size; })
|
2021-07-17 05:48:07 +00:00
|
|
|
+ $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
2023-07-02 13:40:08 +00:00
|
|
|
->sum(function($item) { return $item->size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'total_count':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
2021-07-17 05:48:07 +00:00
|
|
|
->count()
|
|
|
|
+ $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
|
|
|
->count();
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'total_size':
|
|
|
|
return $this->list
|
2023-07-02 13:40:08 +00:00
|
|
|
->sum(function($item) { return $item->size; })
|
|
|
|
+ $this->packets->sum(function($item) { return $item->size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown key: '.$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a file to the list of files to send
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @throws Exception
|
2021-07-23 14:53:35 +00:00
|
|
|
* @todo Catch if we add the same file twice
|
2021-04-01 10:59:15 +00:00
|
|
|
*/
|
|
|
|
public function add(string $file): void
|
|
|
|
{
|
2021-08-18 12:11:16 +00:00
|
|
|
Log::debug(sprintf('%s:+ add [%s]',self::LOGKEY,$file));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->list->push(new Item($file,self::I_SEND));
|
|
|
|
|
|
|
|
} catch (FileNotFoundException) {
|
2021-08-18 12:11:16 +00:00
|
|
|
Log::error(sprintf('%s:! Item [%s] doesnt exist',self::LOGKEY,$file));
|
2021-04-01 10:59:15 +00:00
|
|
|
return;
|
|
|
|
|
2022-11-01 11:24:36 +00:00
|
|
|
} catch (UnreadableFileEncountered) {
|
2021-08-18 12:11:16 +00:00
|
|
|
Log::error(sprintf('%s:! Item [%s] cannot be read',self::LOGKEY,$file));
|
2021-04-01 10:59:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Uncaught, rethrow the error
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Close the file descriptor of the file we are sending
|
|
|
|
*
|
|
|
|
* @param bool $successful
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function close(bool $successful): void
|
|
|
|
{
|
|
|
|
if (! $this->f)
|
|
|
|
throw new Exception('No file to close');
|
|
|
|
|
|
|
|
if ($successful) {
|
|
|
|
$this->sending->sent = TRUE;
|
|
|
|
$end = time()-$this->start;
|
2023-07-02 13:40:08 +00:00
|
|
|
Log::debug(sprintf('%s: - Closing [%s], sent in [%d]',self::LOGKEY,$this->sending->name,$end));
|
2021-04-01 10:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
// @todo This should be done better isType == file?
|
|
|
|
if ((! $this->sending instanceof Mail) && (! $this->sending->isType(self::IS_TIC)))
|
2021-07-17 05:48:07 +00:00
|
|
|
fclose($this->f);
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->sending = NULL;
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->f = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we are at the end of the file
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function feof(): bool
|
|
|
|
{
|
2023-06-22 07:36:22 +00:00
|
|
|
return (($this->sending instanceof Mail) || ($this->sending->isType(self::IS_TIC)))
|
2023-06-27 07:39:11 +00:00
|
|
|
? ($this->file_pos === $this->size)
|
2023-06-22 07:36:22 +00:00
|
|
|
: feof($this->f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add our mail to the send queue
|
|
|
|
*
|
|
|
|
* @param Address $ao
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
* @todo We need to make this into a transaction, incase the transfer fails.
|
|
|
|
*/
|
|
|
|
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::debug(sprintf('%s:- [%d] Files(s) added for sending to [%s]',self::LOGKEY,$x->count(),$ao->ftn));
|
|
|
|
|
|
|
|
// Add Files
|
2023-07-02 13:40:08 +00:00
|
|
|
foreach ($x as $fo) {
|
|
|
|
$this->list->push(new Item($fo,self::I_SEND));
|
|
|
|
$this->list->push(new Tic($ao,$fo,self::I_SEND));
|
2023-06-22 07:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$file = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
2021-04-01 10:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a file for sending
|
|
|
|
*
|
2023-07-02 13:40:08 +00:00
|
|
|
* @param string $compress
|
2021-04-01 10:59:15 +00:00
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2023-07-02 13:40:08 +00:00
|
|
|
public function open(string $compress=''): bool
|
2021-04-01 10:59:15 +00:00
|
|
|
{
|
2021-08-18 12:11:16 +00:00
|
|
|
Log::debug(sprintf('%s:+ open',self::LOGKEY));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
// If we have mail, we'll send that first
|
|
|
|
if ($this->sending = $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
|
|
|
->first())
|
|
|
|
{
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->start = time();
|
|
|
|
$this->f = TRUE;
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
/*
|
|
|
|
if ($compress)
|
|
|
|
$this->comp_data = $this->compdata($compress);
|
|
|
|
*/
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->sending = $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (! $this->sending)
|
|
|
|
throw new Exception('No files to open');
|
|
|
|
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->start = time();
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
// If sending->file is a string, then we dont need to actually open anything
|
|
|
|
if ($this->sending->isType(self::IS_TIC)) {
|
|
|
|
$this->f = TRUE;
|
|
|
|
return TRUE;
|
2021-04-01 10:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
// If sending file is a File::class, then our file is s3
|
2023-07-02 13:40:08 +00:00
|
|
|
if (! $this->sending->name && $this->sending->filemodel) {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->f = Storage::readStream($this->sending->filemodel->full_storage_path);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
} else {
|
2023-07-02 13:40:08 +00:00
|
|
|
$this->f = fopen($this->sending->name,'rb');
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
if (! $this->f) {
|
2023-07-02 13:40:08 +00:00
|
|
|
Log::error(sprintf('%s:! Unable to open file [%s] for reading',self::LOGKEY,$this->sending->name));
|
2023-06-22 07:36:22 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
Log::info(sprintf('%s:= open - File [%s] opened with size [%d]',self::LOGKEY,$this->sending->name,$this->sending->size));
|
2023-06-22 07:36:22 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
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
|
2022-12-02 14:09:41 +00:00
|
|
|
* @return bool
|
2021-07-17 05:48:07 +00:00
|
|
|
* @throws Exception
|
2022-01-20 11:47:44 +00:00
|
|
|
* @todo We need to make this into a transaction, incase the transfer fails.
|
2021-07-17 05:48:07 +00:00
|
|
|
*/
|
2022-12-02 14:09:41 +00:00
|
|
|
public function mail(Address $ao): bool
|
2021-07-17 05:48:07 +00:00
|
|
|
{
|
2022-12-02 14:09:41 +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-06-22 07:36:22 +00:00
|
|
|
Log::info(sprintf('%s: - System [%d] is marked as hold - not checking for mail.',self::LOGKEY,$ao->system_id));
|
2022-12-02 14:09:41 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2022-01-15 11:38:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
// Netmail
|
2021-07-18 12:10:21 +00:00
|
|
|
if ($x=$ao->getNetmail()) {
|
2021-08-18 12:11:16 +00:00
|
|
|
Log::debug(sprintf('%s: - Netmail(s) added for sending to [%s]',self::LOGKEY,$ao->ftn));
|
2021-07-18 12:10:21 +00:00
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
$this->packets->push(new Mail($x,self::I_SEND));
|
2022-12-02 14:09:41 +00:00
|
|
|
$mail = TRUE;
|
2021-07-18 12:10:21 +00:00
|
|
|
}
|
2021-09-06 13:39:32 +00:00
|
|
|
|
|
|
|
// Echomail
|
|
|
|
if ($x=$ao->getEchomail()) {
|
|
|
|
Log::debug(sprintf('%s: - Echomail(s) added for sending to [%s]',self::LOGKEY,$ao->ftn));
|
|
|
|
|
|
|
|
$this->packets->push(new Mail($x,self::I_SEND));
|
2022-12-02 14:09:41 +00:00
|
|
|
$mail = TRUE;
|
2021-09-06 13:39:32 +00:00
|
|
|
}
|
2022-12-02 14:09:41 +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->f)
|
|
|
|
throw new Exception('No file open for read');
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
// We are sending mail
|
|
|
|
if ($this->sending instanceof Mail) {
|
|
|
|
$data = $this->sending->read($this->file_pos,$length);
|
2023-06-22 07:36:22 +00:00
|
|
|
|
|
|
|
// We are sending a tic file
|
|
|
|
} else if ($this->sending->isType(self::IS_TIC)) {
|
|
|
|
$data = $this->sending->read($this->file_pos,$length);
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
} else {
|
|
|
|
$data = fread($this->f,$length);
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->file_pos += strlen($data);
|
2022-12-02 14:00:45 +00:00
|
|
|
|
|
|
|
if ($this->DEBUG)
|
|
|
|
Log::debug(sprintf('%s: - Read [%d] bytes, file pos now [%d]',self::LOGKEY,strlen($data),$this->file_pos));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
if ($data === FALSE)
|
2023-07-02 13:40:08 +00:00
|
|
|
throw new UnreadableFileEncountered('Error reading file: '.$this->sending->name);
|
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->f)
|
|
|
|
throw new Exception('No file open for seek');
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
if (($this->sending instanceof Mail) || $this->sending->isType(self::IS_TIC)) {
|
2021-07-23 14:53:35 +00:00
|
|
|
$pos = ($pos < $this->size) ? $pos : $this->size;
|
|
|
|
$rc = TRUE;
|
2021-07-17 05:48:07 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$rc = (fseek($this->f,$pos,SEEK_SET) === 0);
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
if ($rc)
|
|
|
|
$this->file_pos = $pos;
|
|
|
|
|
2021-08-18 12:11:16 +00:00
|
|
|
Log::debug(sprintf('%s:= Seeked to [%d]',self::LOGKEY,$this->file_pos));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
}
|