From de0057e9c4586a9ddb38abf271cf7a6ac5deb258 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 7 Jun 2024 23:07:38 +0400 Subject: [PATCH] fix(smtp): added frontend attachments endpoint --- src/Sender/Frontend/Message/Attachments.php | 43 +++++++++++++++++++++ src/Sender/Frontend/Service.php | 25 ++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/Sender/Frontend/Message/Attachments.php diff --git a/src/Sender/Frontend/Message/Attachments.php b/src/Sender/Frontend/Message/Attachments.php new file mode 100644 index 00000000..d073a0c3 --- /dev/null +++ b/src/Sender/Frontend/Message/Attachments.php @@ -0,0 +1,43 @@ + $files + * @param array $meta + */ + public function __construct( + Event $event, + array $files, + public readonly array $meta = [], + ) { + foreach ($files as $file) { + $this->data[] = [ + 'uuid' => $file->uuid, + 'name' => $file->file->getClientFilename(), + 'path' => "$event->uuid/attachment/$file->uuid", + 'size' => $file->file->getSize(), + 'mime' => $file->file->getClientMediaType(), + ]; + } + } + + public function jsonSerialize(): array + { + return [ + 'data' => $this->data, + 'meta' => $this->meta + ['grid' => []], + ]; + } +} diff --git a/src/Sender/Frontend/Service.php b/src/Sender/Frontend/Service.php index 1c47e604..ba41cccb 100644 --- a/src/Sender/Frontend/Service.php +++ b/src/Sender/Frontend/Service.php @@ -10,6 +10,8 @@ use Buggregator\Trap\Handler\Router\Attribute\StaticRoute; use Buggregator\Trap\Handler\Router\Method; use Buggregator\Trap\Logger; +use Buggregator\Trap\Sender\Frontend\Event\AttachedFile; +use Buggregator\Trap\Sender\Frontend\Message\Attachments; use Buggregator\Trap\Sender\Frontend\Message\EventCollection; use Buggregator\Trap\Sender\Frontend\Message\Settings; use Buggregator\Trap\Sender\Frontend\Message\Success; @@ -58,6 +60,29 @@ public function eventShow(string $uuid): Event|Success return $event ?? new Success(status: false); } + #[RegexpRoute(Method::Get, '#^api/smtp/(?[a-f0-9-]++)/attachments$#i')] + #[ + AssertSuccess(Method::Get, 'api/smtp/018ff30e-a452-7316-b60f-a2d1c3fe16ab/attachments', [ + 'uuid' => '018ff30e-a452-7316-b60f-a2d1c3fe16ab', + ]), + ] + public function smtpAttachments(string $uuid): Attachments|Success + { + $this->debug('Show SMTP %s attachments', $uuid); + $event = $this->eventsStorage->get($uuid); + + if ($event === null) { + return new Success(status: false); + } + + $attaches = []; + foreach ($event->assets as $attachment) { + $attachment instanceof AttachedFile and $attaches[] = $attachment; + } + + return new Attachments($event, files: $attaches); + } + #[StaticRoute(Method::Delete, 'api/events')] #[ AssertFail(Method::Delete, '/api/events'),