add icon uploads
This commit is contained in:
parent
0f3bb6de6b
commit
97ecbebb9e
8 changed files with 286 additions and 1 deletions
|
|
@ -5,18 +5,24 @@ namespace App\Controllers;
|
||||||
use App\Element\Element;
|
use App\Element\Element;
|
||||||
use App\Element\UseCases\GetElement\GetElement;
|
use App\Element\UseCases\GetElement\GetElement;
|
||||||
use App\Element\UseCases\GetElement\GetElementRequest;
|
use App\Element\UseCases\GetElement\GetElementRequest;
|
||||||
|
use App\Element\UseCases\UpdateElement\UpdateElementIconImage;
|
||||||
|
use App\Element\UseCases\UpdateElement\UpdateElementIconImageRequest;
|
||||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
use App\Element\UseCases\UpdateElement\UpdateElement;
|
||||||
use App\Element\UseCases\UpdateElement\UpdateElementRequest;
|
use App\Element\UseCases\UpdateElement\UpdateElementRequest;
|
||||||
use App\Exceptions\BadRequestException;
|
use App\Exceptions\BadRequestException;
|
||||||
use App\Exceptions\NotFoundException;
|
use App\Exceptions\NotFoundException;
|
||||||
|
use App\Shared\Files\FileUploader;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
|
||||||
class ElementController
|
class ElementController
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private GetElement $getElement,
|
private GetElement $getElement,
|
||||||
private UpdateElement $updateElement,
|
private UpdateElement $updateElement,
|
||||||
|
private UpdateElementIconImage $updateElementIconImage,
|
||||||
|
private FileUploader $fileUploader,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,6 +90,35 @@ class ElementController
|
||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function uploadIconImage(?int $id, Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
$iconImage = $this->uploadedFileInput($request, 'iconImage');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$element = $this->updateElementIconImage->execute(
|
||||||
|
new UpdateElementIconImageRequest(
|
||||||
|
id: $id,
|
||||||
|
iconImageContents: $iconImage['contents'],
|
||||||
|
iconImageOriginalName: $iconImage['originalName'],
|
||||||
|
iconImageMimeType: $iconImage['mimeType'],
|
||||||
|
iconImageSizeBytes: $iconImage['sizeBytes'],
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (BadRequestException $exception) {
|
||||||
|
return new JsonResponse([
|
||||||
|
'error' => $exception->getMessage(),
|
||||||
|
], 400);
|
||||||
|
} catch (NotFoundException $exception) {
|
||||||
|
return new JsonResponse([
|
||||||
|
'error' => $exception->getMessage(),
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JsonResponse([
|
||||||
|
'element' => $this->buildElementPayload($element),
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
private function stringInput(Request $request, string $key): ?string
|
private function stringInput(Request $request, string $key): ?string
|
||||||
{
|
{
|
||||||
$value = $request->input($key);
|
$value = $request->input($key);
|
||||||
|
|
@ -94,6 +129,45 @@ class ElementController
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{
|
||||||
|
* contents: string|null,
|
||||||
|
* originalName: string|null,
|
||||||
|
* mimeType: string|null,
|
||||||
|
* sizeBytes: int|null
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
private function uploadedFileInput(Request $request, string $key): array
|
||||||
|
{
|
||||||
|
$emptyFileInput = [
|
||||||
|
'contents' => null,
|
||||||
|
'originalName' => null,
|
||||||
|
'mimeType' => null,
|
||||||
|
'sizeBytes' => null,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (! $request->hasFile($key)) {
|
||||||
|
return $emptyFileInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
$file = $request->file($key);
|
||||||
|
if (! $file instanceof UploadedFile) {
|
||||||
|
return $emptyFileInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
$realPath = $file->getRealPath();
|
||||||
|
if ($realPath === false) {
|
||||||
|
return $emptyFileInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'contents' => (string) file_get_contents($realPath),
|
||||||
|
'originalName' => $file->getClientOriginalName(),
|
||||||
|
'mimeType' => (string) $file->getMimeType(),
|
||||||
|
'sizeBytes' => (int) $file->getSize(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array{
|
* @return array{
|
||||||
* id: int,
|
* id: int,
|
||||||
|
|
@ -111,10 +185,26 @@ class ElementController
|
||||||
'id' => $element->getId(),
|
'id' => $element->getId(),
|
||||||
'title' => $element->getTitle(),
|
'title' => $element->getTitle(),
|
||||||
'description' => $element->getDescription(),
|
'description' => $element->getDescription(),
|
||||||
'iconImageUrl' => $element->getIconImageUrl(),
|
'iconImageUrl' => $this->storageUrl(
|
||||||
|
$element->getIconImageUrl(),
|
||||||
|
'element-icons/',
|
||||||
|
),
|
||||||
'richText' => $element->getRichText(),
|
'richText' => $element->getRichText(),
|
||||||
'pdfPath' => $element->getPdfPath(),
|
'pdfPath' => $element->getPdfPath(),
|
||||||
'youtubeUrl' => $element->getYoutubeUrl(),
|
'youtubeUrl' => $element->getYoutubeUrl(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function storageUrl(?string $path, string $folderPrefix): ?string
|
||||||
|
{
|
||||||
|
if ($path === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_starts_with($path, $folderPrefix)) {
|
||||||
|
return $this->fileUploader->url($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Element\UseCases\UpdateElement;
|
||||||
|
|
||||||
|
use App\Element\Element;
|
||||||
|
use App\Element\ElementRepository;
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
|
use App\Exceptions\NotFoundException;
|
||||||
|
use App\Shared\Files\FileToUpload;
|
||||||
|
use App\Shared\Files\FileUploader;
|
||||||
|
|
||||||
|
class UpdateElementIconImage
|
||||||
|
{
|
||||||
|
private const ALLOWED_MIME_TYPES = [
|
||||||
|
'image/jpeg',
|
||||||
|
'image/png',
|
||||||
|
'image/webp',
|
||||||
|
];
|
||||||
|
|
||||||
|
private const MAX_SIZE_BYTES = 5 * 1024 * 1024;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private ElementRepository $elementRepository,
|
||||||
|
private FileUploader $fileUploader,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
* @throws NotFoundException
|
||||||
|
*/
|
||||||
|
public function execute(UpdateElementIconImageRequest $request): Element
|
||||||
|
{
|
||||||
|
if ($request->id === null) {
|
||||||
|
throw new BadRequestException('id is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$request->iconImageContents === null
|
||||||
|
|| $request->iconImageOriginalName === null
|
||||||
|
|| $request->iconImageMimeType === null
|
||||||
|
|| $request->iconImageSizeBytes === null
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('icon image is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
! in_array(
|
||||||
|
$request->iconImageMimeType,
|
||||||
|
self::ALLOWED_MIME_TYPES,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'icon image must be a jpeg, png or webp image',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->iconImageSizeBytes > self::MAX_SIZE_BYTES) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'icon image must be 5MB or smaller',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$element = $this->elementRepository->find($request->id);
|
||||||
|
if ($element === null) {
|
||||||
|
throw new NotFoundException('Element not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$iconImage = new FileToUpload(
|
||||||
|
contents: $request->iconImageContents,
|
||||||
|
originalName: $request->iconImageOriginalName,
|
||||||
|
mimeType: $request->iconImageMimeType,
|
||||||
|
sizeBytes: $request->iconImageSizeBytes,
|
||||||
|
);
|
||||||
|
$path = $this->fileUploader->upload($iconImage, 'element-icons');
|
||||||
|
$element->setIconImageUrl($path);
|
||||||
|
|
||||||
|
return $this->elementRepository->update($element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Element\UseCases\UpdateElement;
|
||||||
|
|
||||||
|
class UpdateElementIconImageRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?int $id,
|
||||||
|
public ?string $iconImageContents,
|
||||||
|
public ?string $iconImageOriginalName,
|
||||||
|
public ?string $iconImageMimeType,
|
||||||
|
public ?int $iconImageSizeBytes,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,8 @@ use App\Auth\PasswordHasher;
|
||||||
use App\Auth\RandomTokenGenerator;
|
use App\Auth\RandomTokenGenerator;
|
||||||
use App\Auth\SystemClock;
|
use App\Auth\SystemClock;
|
||||||
use App\Auth\TokenGenerator;
|
use App\Auth\TokenGenerator;
|
||||||
|
use App\Shared\Files\FileUploader;
|
||||||
|
use App\Shared\Files\LaravelFileUploader;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
|
|
@ -26,5 +28,9 @@ class AppServiceProvider extends ServiceProvider
|
||||||
Clock::class,
|
Clock::class,
|
||||||
SystemClock::class,
|
SystemClock::class,
|
||||||
);
|
);
|
||||||
|
$this->app->bind(
|
||||||
|
FileUploader::class,
|
||||||
|
LaravelFileUploader::class,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
backend/app/Shared/Files/FileToUpload.php
Normal file
34
backend/app/Shared/Files/FileToUpload.php
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Shared\Files;
|
||||||
|
|
||||||
|
final readonly class FileToUpload
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private string $contents,
|
||||||
|
private string $originalName,
|
||||||
|
private string $mimeType,
|
||||||
|
private int $sizeBytes,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getContents(): string
|
||||||
|
{
|
||||||
|
return $this->contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOriginalName(): string
|
||||||
|
{
|
||||||
|
return $this->originalName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMimeType(): string
|
||||||
|
{
|
||||||
|
return $this->mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSizeBytes(): int
|
||||||
|
{
|
||||||
|
return $this->sizeBytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
backend/app/Shared/Files/FileUploader.php
Normal file
10
backend/app/Shared/Files/FileUploader.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Shared\Files;
|
||||||
|
|
||||||
|
interface FileUploader
|
||||||
|
{
|
||||||
|
public function upload(FileToUpload $file, string $folder): string;
|
||||||
|
|
||||||
|
public function url(string $path): string;
|
||||||
|
}
|
||||||
44
backend/app/Shared/Files/LaravelFileUploader.php
Normal file
44
backend/app/Shared/Files/LaravelFileUploader.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Shared\Files;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class LaravelFileUploader implements FileUploader
|
||||||
|
{
|
||||||
|
public function upload(FileToUpload $file, string $folder): string
|
||||||
|
{
|
||||||
|
$path = $folder . '/' . Str::random(40) . $this->extensionFor($file);
|
||||||
|
Storage::disk('public')->put($path, $file->getContents());
|
||||||
|
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function url(string $path): string
|
||||||
|
{
|
||||||
|
return Storage::disk('public')->url($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function extensionFor(FileToUpload $file): string
|
||||||
|
{
|
||||||
|
$mimeExtensions = [
|
||||||
|
'image/jpeg' => '.jpg',
|
||||||
|
'image/png' => '.png',
|
||||||
|
'image/webp' => '.webp',
|
||||||
|
];
|
||||||
|
if (isset($mimeExtensions[$file->getMimeType()])) {
|
||||||
|
return $mimeExtensions[$file->getMimeType()];
|
||||||
|
}
|
||||||
|
|
||||||
|
$originalExtension = pathinfo(
|
||||||
|
$file->getOriginalName(),
|
||||||
|
PATHINFO_EXTENSION,
|
||||||
|
);
|
||||||
|
if ($originalExtension !== '') {
|
||||||
|
return '.' . $originalExtension;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,3 +14,8 @@ Route::get('/sets', [SetController::class, 'index']);
|
||||||
Route::get('/elements/{id}', [ElementController::class, 'show']);
|
Route::get('/elements/{id}', [ElementController::class, 'show']);
|
||||||
Route::patch('/elements/{id}', [ElementController::class, 'update'])
|
Route::patch('/elements/{id}', [ElementController::class, 'update'])
|
||||||
->middleware(AuthMiddleware::class);
|
->middleware(AuthMiddleware::class);
|
||||||
|
Route::post(
|
||||||
|
'/elements/{id}/icon-image',
|
||||||
|
[ElementController::class, 'uploadIconImage'],
|
||||||
|
)
|
||||||
|
->middleware(AuthMiddleware::class);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue