Move the send DB updates out of the protocol and into Send::class

This commit is contained in:
Deon George 2023-07-19 12:32:41 +10:00
parent 7584e3e44e
commit f4fc6c24a4
6 changed files with 67 additions and 58 deletions

View File

@ -2,9 +2,12 @@
namespace App\Classes\File;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Classes\Node;
use App\Models\File as FileModel;
final class File extends Send
@ -49,11 +52,22 @@ final class File extends Send
}
}
public function close(bool $successful): void
public function close(bool $successful,Node $node): void
{
if ($successful)
if ($successful) {
$this->complete = TRUE;
if (($this->type === Send::T_FILE)
&& ($x=$this->dbids)->count()
&& $node->aka_remote_authed->count())
DB::table('file_seenby')
->whereIn('file_id',$x)
->whereIn('address_id',$node->aka_remote_authed->pluck('id'))
->update([
'sent_at'=>Carbon::now(),
]);
}
fclose($this->fd);
}

View File

@ -4,7 +4,10 @@ namespace App\Classes\File;
use Carbon\Carbon;
use App\Classes\FTN\Packet;
use Illuminate\Support\Facades\DB;
use App\Classes\Node;
use App\Classes\FTN\{Message,Packet};
final class Mail extends Send
{
@ -48,10 +51,35 @@ final class Mail extends Send
}
}
public function close(bool $successful): void
public function close(bool $successful,Node $node): void
{
if ($successful)
if ($successful) {
$this->complete = TRUE;
// Update netmail table
if (($this->type === Send::T_NETMAIL)
&& ($x=$this->dbids)->count())
DB::table('netmails')
->whereIn('id',$x)
->update([
'sent_at'=>Carbon::now(),
'sent_pkt'=>$this->name,
'sent_id'=>$node->address->id,
'flags'=>DB::raw('flags | '.Message::FLAG_SENT),
]);
// Update echomails table
elseif (($this->type === Send::T_ECHOMAIL)
&& ($x=$this->dbids)->count()
&& $node->aka_remote_authed->count())
DB::table('echomail_seenby')
->whereIn('echomail_id',$x)
->whereIn('address_id',$node->aka_remote_authed->pluck('id'))
->update([
'sent_at'=>Carbon::now(),
'sent_pkt'=>$this->name,
]);
}
}
public function feof(): bool

View File

@ -6,6 +6,7 @@ use Exception;
use Illuminate\Support\Facades\Log;
use League\Flysystem\UnreadableFileEncountered;
use App\Classes\Node;
use App\Models\Address;
/**
@ -72,7 +73,6 @@ class Send extends Base
->filter(function($item) { return $item->isType(self::IS_ARC|self::IS_PKT); })
->sum(function($item) { return $item->size; });
case 'dbids':
case 'name':
case 'nameas':
case 'mtime':
@ -112,19 +112,20 @@ class Send extends Base
* Close the file descriptor of the file we are sending
*
* @param bool $successful
* @param Node $node
* @throws Exception
*/
public function close(bool $successful): void
public function close(bool $successful,Node $node): void
{
if (! $this->fd)
throw new Exception('No file to close');
if ($successful) {
$end = time()-$this->start;
Log::debug(sprintf('%s: - Closing [%s], sent in [%d]',self::LOGKEY,$this->sending->nameas,$end));
Log::debug(sprintf('%s: - Closing [%s], sent in [%d] with [%s] items',self::LOGKEY,$this->sending->nameas,$end,$this->sending->dbids->count()));
}
$this->sending->close($successful);
$this->sending->close($successful,$node);
$this->index = NULL;
}

View File

@ -573,7 +573,7 @@ final class Binkp extends BaseProtocol
$buf = $this->send->read(self::BLOCKSIZE);
} catch (UnreadableFileEncountered) {
$this->send->close(FALSE);
$this->send->close(FALSE,$this->node);
$this->sessionClear(self::SE_SENDFILE);
} catch (\Exception $e) {
@ -863,7 +863,7 @@ final class Binkp extends BaseProtocol
$this->msgs(self::BPM_ERR,sprintf('M_FILE: unparsable file info: "%s", what are you on?',$buf));
if ($this->sessionGet(self::SE_SENDFILE))
$this->send->close(FALSE);
$this->send->close(FALSE,$this->node);
$this->rc = self::S_FAILURE;
@ -957,7 +957,7 @@ final class Binkp extends BaseProtocol
Log::error(sprintf('%s:! Cannot send file from requested offset [%d]',self::LOGKEY,$file['offs']));
$this->msgs(self::BPM_ERR,'Can\'t send file from requested offset');
$this->send->close(FALSE);
$this->send->close(FALSE,$this->node);
$this->sessionClear(self::SE_SENDFILE);
} else {
@ -998,45 +998,10 @@ final class Binkp extends BaseProtocol
Log::error(sprintf('%s:! M_got[skip] for unknown file [%s]',self::LOGKEY,$buf));
} else {
Log::info(sprintf('%s:= Packet/File [%s], type [%d] sent with [%d] items.',self::LOGKEY,$this->send->nameas,$this->send->type,$this->send->dbids->count()));
Log::info(sprintf('%s:= Packet/File [%s], type [%d] sent.',self::LOGKEY,$this->send->nameas,$this->send->type));
$this->sessionClear(self::SE_WAITGOT|self::SE_SENDFILE);
// Update netmail table
if (($this->send->type === Send::T_NETMAIL)
&& ($x=$this->send->dbids)->count())
DB::table('netmails')
->whereIn('id',$x)
->update([
'sent_at'=>Carbon::now(),
'sent_pkt'=>$this->send->name,
'sent_id'=>$this->node->address->id,
'flags'=>DB::raw('flags | '.Message::FLAG_SENT),
]);
// Update echomails table
elseif (($this->send->type === Send::T_ECHOMAIL)
&& ($x=$this->send->dbids)->count()
&& $this->node->aka_remote_authed->count())
DB::table('echomail_seenby')
->whereIn('echomail_id',$x)
->whereIn('address_id',$this->node->aka_remote_authed->pluck('id'))
->update([
'sent_at'=>Carbon::now(),
'sent_pkt'=>$this->send->name,
]);
// Update the file seenby
elseif (($this->send->type === Send::T_FILE)
&& ($x=$this->send->dbids)->count()
&& $this->node->aka_remote_authed->count())
DB::table('file_seenby')
->whereIn('file_id',$x)
->whereIn('address_id',$this->node->aka_remote_authed->pluck('id'))
->update([
'sent_at'=>Carbon::now(),
]);
$this->send->close(TRUE);
$this->send->close(TRUE,$this->node);
}
}

View File

@ -1211,7 +1211,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$z = new Zmodem;
if (! $z->zmodem_sendinit($this->client,$zap) && $this->send->togo_count)
$z->zmodem_sendfile($this->send);
$z->zmodem_sendfile($this->send,$this->node);
}
// Send files
@ -1219,7 +1219,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$z = new Zmodem;
if (! $z->zmodem_sendinit($this->client,$zap) && $this->send->togo_count)
$z->zmodem_sendfile($this->send);
$z->zmodem_sendfile($this->send,$this->node);
}
}

View File

@ -4,7 +4,7 @@ namespace App\Classes\Protocol;
use Illuminate\Support\Facades\Log;
use App\Classes\Protocol;
use App\Classes\{Node,Protocol};
use App\Classes\Protocol\Zmodem as ZmodemClass;
use App\Classes\File\{Receive,Send};
use App\Classes\Sock\{SocketClient,SocketException};
@ -509,7 +509,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
* @param Send $send
* @return int
*/
public function zmodem_sendfile(Send $send): int
public function zmodem_sendfile(Send $send,Node $node): int
{
Log::debug(sprintf('%s:+ zmodem_sendfile',self::LOGKEY));
@ -519,16 +519,16 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
switch ($rc) {
case self::OK:
$send->close(TRUE);
case self::ZSKIP:
$send->close(TRUE,$node);
break;
case self::ZSKIP:
case self::ZFERR:
$send->close(FALSE);
$send->close(FALSE,$node);
break;
default:
$send->close(FALSE);
$send->close(FALSE,$node);
$this->ls_zabort();
break;
}
@ -2143,7 +2143,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$trys = 0;
$needack = 0;
switch (($rc = $this->ls_zsendfinfo($send,$sernum,$send->pos,$fileleft,$bytesleft))) {
switch (($rc=$this->ls_zsendfinfo($send,$sernum,$send->pos,$fileleft,$bytesleft))) {
/* Ok, It's OK! */
case self::ZRPOS:
Log::debug(sprintf('%s: - ls_zsendfile ZRPOS to [%d]',self::LOGKEY,$send->pos));
@ -2152,6 +2152,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Skip it */
case self::ZSKIP:
/* Suspend it */
// @todo Should ZFERR be next to ZABORT?
case self::ZFERR:
// @todo Mark the file as skipped
Log::debug(sprintf('%s: - ls_zsendfile ZSKIP/ZFERR',self::LOGKEY));