From 19cb77060ae779cd8f470abd2438bd827a38c73c Mon Sep 17 00:00:00 2001 From: kaioken Date: Thu, 12 Sep 2024 21:33:59 -0400 Subject: [PATCH] feat: upload file mutation trait --- .../Messages/MessageManagementMutation.php | 24 ++++++------- .../Traits/HasMutationUploadFiles.php | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/Kanvas/Filesystem/Traits/HasMutationUploadFiles.php diff --git a/app/GraphQL/Social/Mutations/Messages/MessageManagementMutation.php b/app/GraphQL/Social/Mutations/Messages/MessageManagementMutation.php index ed343e38c..a84b16550 100644 --- a/app/GraphQL/Social/Mutations/Messages/MessageManagementMutation.php +++ b/app/GraphQL/Social/Mutations/Messages/MessageManagementMutation.php @@ -11,8 +11,7 @@ use Kanvas\Apps\Models\Apps; use Kanvas\Auth\Exceptions\AuthenticationException; use Kanvas\Exceptions\ValidationException; -use Kanvas\Filesystem\Actions\AttachFilesystemAction; -use Kanvas\Filesystem\Services\FilesystemServices; +use Kanvas\Filesystem\Traits\HasMutationUploadFiles; use Kanvas\Social\Messages\Actions\CreateMessageAction; use Kanvas\Social\Messages\Actions\DistributeChannelAction; use Kanvas\Social\Messages\Actions\DistributeToUsers; @@ -27,6 +26,8 @@ class MessageManagementMutation { + use HasMutationUploadFiles; + public function create(mixed $root, array $request): Message { $app = app(Apps::class); @@ -181,23 +182,18 @@ public function detachTopicToMessage(mixed $root, array $request): Message public function attachFileToMessage(mixed $root, array $request): Message { - $message = Message::getById((int)$request['message_id'], app(Apps::class)); + $app = app(Apps::class); + $message = Message::getById((int)$request['message_id'], $app); if (($message->user->getId() !== auth()->user()->getId()) && ! auth()->user()->isAdmin()) { throw new Exception('The message does not belong to the authenticated user'); } - $filesystem = new FilesystemServices(app(Apps::class)); - $file = $request['file']; - in_array($file->extension(), ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp']) ?: throw new Exception('Invalid file format'); - - $filesystemEntity = $filesystem->upload($file, auth()->user()); - $action = new AttachFilesystemAction( - $filesystemEntity, - $message + return $this->uploadFileToEntity( + model: $message, + app: $app, + user: auth()->user(), + request: $request ); - $action->execute('photo'); - - return $message; } } diff --git a/src/Kanvas/Filesystem/Traits/HasMutationUploadFiles.php b/src/Kanvas/Filesystem/Traits/HasMutationUploadFiles.php new file mode 100644 index 000000000..7b1e1b722 --- /dev/null +++ b/src/Kanvas/Filesystem/Traits/HasMutationUploadFiles.php @@ -0,0 +1,36 @@ +extension(), ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp', 'json', 'pdf', 'txt', 'text']) ?: throw new Exception('Invalid file format ' . $file->extension()); + + $filesystemEntity = $filesystem->upload($file, $user); + $action = new AttachFilesystemAction( + $filesystemEntity, + $model + ); + $action->execute($file->getClientOriginalName()); + + return $model; + } +}