Simplified logging and fix BINKP issue when receiving less than the blksize

This commit is contained in:
Deon George
2021-08-17 23:49:39 +10:00
parent 5bf612e5b4
commit 978843b5e3
7 changed files with 285 additions and 253 deletions

View File

@@ -15,6 +15,8 @@ use League\Flysystem\UnreadableFileException;
*/
class Item
{
private const LOGKEY = 'I--';
protected const IS_PKT = (1<<1);
protected const IS_ARC = (1<<2);
protected const IS_FILE = (1<<3);

View File

@@ -22,6 +22,8 @@ use App\Models\Address;
*/
final class Receive extends Item
{
private const LOGKEY = 'IR-';
private Address $ao;
private Collection $list;
private ?Item $receiving;
@@ -88,14 +90,14 @@ final class Receive extends Item
throw new Exception('No file to close');
if ($this->file_pos != $this->receiving->file_size) {
Log::warning(sprintf('%s: - Closing [%s], but missing [%d] bytes',__METHOD__,$this->receiving->file_name,$this->receiving->file_size-$this->file_pos));
Log::warning(sprintf('%s: - Closing [%s], but missing [%d] bytes',self::LOGKEY,$this->receiving->file_name,$this->receiving->file_size-$this->file_pos));
$this->receiving->incomplete = TRUE;
}
$this->receiving->received = TRUE;
$end = time()-$this->start;
Log::debug(sprintf('%s: - Closing [%s], received in [%d]',__METHOD__,$this->receiving->file_name,$end));
Log::debug(sprintf('%s: - Closing [%s], received in [%d]',self::LOGKEY,$this->receiving->file_name,$end));
fclose($this->f);
$this->file_pos = 0;
@@ -107,19 +109,19 @@ final class Receive extends Item
if (! $this->receiving->incomplete)
switch ($this->receiving->file_type) {
case self::IS_PKT:
Log::info(sprintf('%s: - Processing mail packet [%s]',__METHOD__,$this->file));
Log::info(sprintf('%s: - Processing mail packet [%s]',self::LOGKEY,$this->file));
try {
$po = Packet::open(new File($this->file),$this->ao->zone->domain);
} catch (InvalidPacketException $e) {
Log::error(sprintf('%s: - Not deleting packet [%s], as it generated an exception',__METHOD__,$this->file));
Log::error(sprintf('%s: - Not deleting packet [%s], as it generated an exception',self::LOGKEY,$this->file));
break;
}
foreach ($po->messages as $msg) {
Log::info(sprintf('%s: - Mail from [%s] to [%s]',__METHOD__,$msg->fftn,$msg->tftn));
Log::info(sprintf('%s: - Mail from [%s] to [%s]',self::LOGKEY,$msg->fftn,$msg->tftn));
// @todo Quick check that the packet should be processed by us.
// @todo validate that the packet's zone is in the domain.
@@ -129,18 +131,18 @@ final class Receive extends Item
}
if ($po->errors->count()) {
Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',__METHOD__,$this->file));
Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',self::LOGKEY,$this->file));
// If we want to keep the packet, we could do that logic here
} elseif (! config('app.packet_keep')) {
Log::debug(sprintf('%s: - Deleting processed packet [%s]',__METHOD__,$this->file));
Log::debug(sprintf('%s: - Deleting processed packet [%s]',self::LOGKEY,$this->file));
unlink($this->file);
}
break;
default:
Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',__METHOD__,$this->file));
Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',self::LOGKEY,$this->file));
}
$this->receiving = NULL;
@@ -155,7 +157,7 @@ final class Receive extends Item
*/
public function open(Address $ao,bool $check=FALSE): bool
{
Log::debug(sprintf('%s: + Start [%d]',__METHOD__,$check));
Log::debug(sprintf('%s:+ open [%d]',self::LOGKEY,$check));
// Check we can open this file
// @todo
@@ -173,14 +175,14 @@ final class Receive extends Item
$this->start = time();
$this->file = sprintf('storage/app/%s/%04X-%s',config('app.fido'),$this->ao->id,$this->receiving->recvas);
Log::debug(sprintf('%s: - Opening [%s]',__METHOD__,$this->file));
Log::debug(sprintf('%s: - Opening [%s]',self::LOGKEY,$this->file));
$this->f = fopen($this->file,'wb');
if (! $this->f) {
Log::error(sprintf('%s: ! Unable to open file [%s] for writing',__METHOD__,$this->receiving->file_name));
Log::error(sprintf('%s:! Unable to open file [%s] for writing',self::LOGKEY,$this->receiving->file_name));
return 3; // @todo change to const
}
Log::info(sprintf('%s: = End - File [%s] opened for writing',__METHOD__,$this->receiving->file_name));
Log::info(sprintf('%s:= open - File [%s] opened for writing',self::LOGKEY,$this->receiving->file_name));
return 0; // @todo change to const
}
@@ -192,7 +194,7 @@ final class Receive extends Item
*/
public function new(array $file): void
{
Log::debug(sprintf('%s: + Start',__METHOD__),['file'=>$file]);
Log::debug(sprintf('%s:+ new [%s]',self::LOGKEY,join('|',$file)));
if ($this->receiving)
throw new Exception('Can only have 1 file receiving at a time');
@@ -224,7 +226,7 @@ final class Receive extends Item
throw new FileException('Error while writing to file');
$this->file_pos += $rc;
Log::debug(sprintf('%s: - Write [%d] bytes, file pos now [%d] of [%d]',__METHOD__,$rc,$this->file_pos,$this->receiving->file_size));
Log::debug(sprintf('%s:- Write [%d] bytes, file pos now [%d] of [%d] (%d)',self::LOGKEY,$rc,$this->file_pos,$this->receiving->file_size,strlen($buf)));
return $rc;
}