Skip to content

Commit

Permalink
feat: upload file mutation trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Sep 13, 2024
1 parent a2e7886 commit 19cb770
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
24 changes: 10 additions & 14 deletions app/GraphQL/Social/Mutations/Messages/MessageManagementMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +26,8 @@

class MessageManagementMutation
{
use HasMutationUploadFiles;

public function create(mixed $root, array $request): Message
{
$app = app(Apps::class);
Expand Down Expand Up @@ -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;
}
}
36 changes: 36 additions & 0 deletions src/Kanvas/Filesystem/Traits/HasMutationUploadFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Kanvas\Filesystem\Traits;

use Baka\Contracts\AppInterface;
use Baka\Users\Contracts\UserInterface;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Kanvas\Filesystem\Actions\AttachFilesystemAction;
use Kanvas\Filesystem\Services\FilesystemServices;

trait HasMutationUploadFiles
{
public function uploadFileToEntity(
Model $model,
AppInterface $app,
UserInterface $user,
array $request
): Model {
$filesystem = new FilesystemServices($app);
$file = $request['file'];

in_array($file->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;
}
}

0 comments on commit 19cb770

Please sign in to comment.