Dont process incomplete files

This commit is contained in:
Deon George 2021-08-12 21:59:48 +10:00
parent 8996b3d09b
commit 393600f96f
2 changed files with 39 additions and 33 deletions

View File

@ -32,6 +32,7 @@ class Item
public bool $sent = FALSE; public bool $sent = FALSE;
public bool $received = FALSE; public bool $received = FALSE;
public bool $incomplete = FALSE;
/** /**
* @throws FileNotFoundException * @throws FileNotFoundException

View File

@ -84,8 +84,10 @@ final class Receive extends Item
if (! $this->f) if (! $this->f)
throw new Exception('No file to close'); throw new Exception('No file to close');
if ($this->file_pos != $this->receiving->file_size) 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',__METHOD__,$this->receiving->file_name,$this->receiving->file_size-$this->file_pos));
$this->receiving->incomplete = TRUE;
}
$this->receiving->received = TRUE; $this->receiving->received = TRUE;
@ -96,44 +98,47 @@ final class Receive extends Item
$this->file_pos = 0; $this->file_pos = 0;
$this->f = NULL; $this->f = NULL;
// If the packet has been received but not the right size, dont process it any more.
// If we received a packet, we'll dispatch a job to process it // If we received a packet, we'll dispatch a job to process it
switch ($this->receiving->file_type) { if (! $this->receiving->incomplete)
case self::IS_PKT: switch ($this->receiving->file_type) {
Log::info(sprintf('%s: - Processing mail packet [%s]',__METHOD__,$this->file)); case self::IS_PKT:
Log::info(sprintf('%s: - Processing mail packet [%s]',__METHOD__,$this->file));
try { try {
$po = Packet::open(new File($this->file),$this->ao->zone->domain); $po = Packet::open(new File($this->file),$this->ao->zone->domain);
} catch (InvalidPacketException $e) { } 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',__METHOD__,$this->file));
break;
}
foreach ($po->messages as $msg) {
Log::info(sprintf('%s: - Mail from [%s] to [%s]',__METHOD__,$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.
// Dispatch job.
ProcessPacket::dispatchSync($msg);
}
if ($po->hasErrors) {
Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',__METHOD__,$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));
unlink($this->file);
}
break; break;
}
foreach ($po->messages as $msg) { default:
Log::info(sprintf('%s: - Mail from [%s] to [%s]',__METHOD__,$msg->fftn,$msg->tftn)); Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',__METHOD__,$this->file));
}
// @todo Quick check that the packet should be processed by us.
// @todo validate that the packet's zone is in the domain.
// Dispatch job.
ProcessPacket::dispatchSync($msg);
}
if ($po->hasErrors) {
Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',__METHOD__,$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));
unlink($this->file);
}
break;
default:
Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',__METHOD__,$this->file));
}
$this->receiving = NULL; $this->receiving = NULL;
} }