add pdf uploads
This commit is contained in:
parent
1fbe198d8c
commit
cd2e63d17d
5 changed files with 133 additions and 2 deletions
|
|
@ -5,9 +5,11 @@ 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\UpdateElement;
|
||||||
use App\Element\UseCases\UpdateElement\UpdateElementIconImage;
|
use App\Element\UseCases\UpdateElement\UpdateElementIconImage;
|
||||||
use App\Element\UseCases\UpdateElement\UpdateElementIconImageRequest;
|
use App\Element\UseCases\UpdateElement\UpdateElementIconImageRequest;
|
||||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
use App\Element\UseCases\UpdateElement\UpdateElementPdf;
|
||||||
|
use App\Element\UseCases\UpdateElement\UpdateElementPdfRequest;
|
||||||
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;
|
||||||
|
|
@ -22,6 +24,7 @@ class ElementController
|
||||||
private GetElement $getElement,
|
private GetElement $getElement,
|
||||||
private UpdateElement $updateElement,
|
private UpdateElement $updateElement,
|
||||||
private UpdateElementIconImage $updateElementIconImage,
|
private UpdateElementIconImage $updateElementIconImage,
|
||||||
|
private UpdateElementPdf $updateElementPdf,
|
||||||
private FileUploader $fileUploader,
|
private FileUploader $fileUploader,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
@ -90,6 +93,35 @@ class ElementController
|
||||||
], 200);
|
], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function uploadPdf(?int $id, Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
$pdf = $this->uploadedFileInput($request, 'pdf');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$element = $this->updateElementPdf->execute(
|
||||||
|
new UpdateElementPdfRequest(
|
||||||
|
id: $id,
|
||||||
|
pdfContents: $pdf['contents'],
|
||||||
|
pdfOriginalName: $pdf['originalName'],
|
||||||
|
pdfMimeType: $pdf['mimeType'],
|
||||||
|
pdfSizeBytes: $pdf['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);
|
||||||
|
}
|
||||||
|
|
||||||
public function uploadIconImage(?int $id, Request $request): JsonResponse
|
public function uploadIconImage(?int $id, Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$iconImage = $this->uploadedFileInput($request, 'iconImage');
|
$iconImage = $this->uploadedFileInput($request, 'iconImage');
|
||||||
|
|
@ -190,7 +222,10 @@ class ElementController
|
||||||
'element-icons/',
|
'element-icons/',
|
||||||
),
|
),
|
||||||
'richText' => $element->getRichText(),
|
'richText' => $element->getRichText(),
|
||||||
'pdfPath' => $element->getPdfPath(),
|
'pdfPath' => $this->storageUrl(
|
||||||
|
$element->getPdfPath(),
|
||||||
|
'element-pdfs/',
|
||||||
|
),
|
||||||
'youtubeUrl' => $element->getYoutubeUrl(),
|
'youtubeUrl' => $element->getYoutubeUrl(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
<?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 UpdateElementPdf
|
||||||
|
{
|
||||||
|
private const ALLOWED_MIME_TYPES = [
|
||||||
|
'application/pdf',
|
||||||
|
];
|
||||||
|
|
||||||
|
private const MAX_SIZE_BYTES = 10 * 1024 * 1024;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private ElementRepository $elementRepository,
|
||||||
|
private FileUploader $fileUploader,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
* @throws NotFoundException
|
||||||
|
*/
|
||||||
|
public function execute(UpdateElementPdfRequest $request): Element
|
||||||
|
{
|
||||||
|
if ($request->id === null) {
|
||||||
|
throw new BadRequestException('id is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$request->pdfContents === null
|
||||||
|
|| $request->pdfOriginalName === null
|
||||||
|
|| $request->pdfMimeType === null
|
||||||
|
|| $request->pdfSizeBytes === null
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('pdf is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
! in_array(
|
||||||
|
$request->pdfMimeType,
|
||||||
|
self::ALLOWED_MIME_TYPES,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('pdf must be a pdf file');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->pdfSizeBytes > self::MAX_SIZE_BYTES) {
|
||||||
|
throw new BadRequestException('pdf must be 10MB or smaller');
|
||||||
|
}
|
||||||
|
|
||||||
|
$element = $this->elementRepository->find($request->id);
|
||||||
|
if ($element === null) {
|
||||||
|
throw new NotFoundException('Element not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdf = new FileToUpload(
|
||||||
|
contents: $request->pdfContents,
|
||||||
|
originalName: $request->pdfOriginalName,
|
||||||
|
mimeType: $request->pdfMimeType,
|
||||||
|
sizeBytes: $request->pdfSizeBytes,
|
||||||
|
);
|
||||||
|
$path = $this->fileUploader->upload($pdf, 'element-pdfs');
|
||||||
|
$element->setPdfPath($path);
|
||||||
|
|
||||||
|
return $this->elementRepository->update($element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Element\UseCases\UpdateElement;
|
||||||
|
|
||||||
|
class UpdateElementPdfRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?int $id,
|
||||||
|
public ?string $pdfContents,
|
||||||
|
public ?string $pdfOriginalName,
|
||||||
|
public ?string $pdfMimeType,
|
||||||
|
public ?int $pdfSizeBytes,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -26,6 +26,7 @@ class LaravelFileUploader implements FileUploader
|
||||||
'image/jpeg' => '.jpg',
|
'image/jpeg' => '.jpg',
|
||||||
'image/png' => '.png',
|
'image/png' => '.png',
|
||||||
'image/webp' => '.webp',
|
'image/webp' => '.webp',
|
||||||
|
'application/pdf' => '.pdf',
|
||||||
];
|
];
|
||||||
if (isset($mimeExtensions[$file->getMimeType()])) {
|
if (isset($mimeExtensions[$file->getMimeType()])) {
|
||||||
return $mimeExtensions[$file->getMimeType()];
|
return $mimeExtensions[$file->getMimeType()];
|
||||||
|
|
|
||||||
|
|
@ -19,3 +19,8 @@ Route::post(
|
||||||
[ElementController::class, 'uploadIconImage'],
|
[ElementController::class, 'uploadIconImage'],
|
||||||
)
|
)
|
||||||
->middleware(AuthMiddleware::class);
|
->middleware(AuthMiddleware::class);
|
||||||
|
Route::post(
|
||||||
|
'/elements/{id}/pdf',
|
||||||
|
[ElementController::class, 'uploadPdf'],
|
||||||
|
)
|
||||||
|
->middleware(AuthMiddleware::class);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue