route element file uploads
Add short and long PDF paths to elements, keep pdfPath as a short PDF compatibility alias, and route generic file uploads by fileType.
This commit is contained in:
parent
0d5783ba9c
commit
9bbabc7fa6
28 changed files with 686 additions and 214 deletions
75
backend/app/Element/UseCases/UpdateElement/UpdateLongPdf.php
Normal file
75
backend/app/Element/UseCases/UpdateElement/UpdateLongPdf.php
Normal file
|
|
@ -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 UpdateLongPdf
|
||||
{
|
||||
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(UpdateLongPdfRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if (
|
||||
$request->fileContents === null
|
||||
|| $request->fileOriginalName === null
|
||||
|| $request->fileMimeType === null
|
||||
|| $request->fileSizeBytes === null
|
||||
) {
|
||||
throw new BadRequestException('pdf is required');
|
||||
}
|
||||
|
||||
if (
|
||||
! in_array(
|
||||
$request->fileMimeType,
|
||||
self::ALLOWED_MIME_TYPES,
|
||||
true,
|
||||
)
|
||||
) {
|
||||
throw new BadRequestException('pdf must be a pdf file');
|
||||
}
|
||||
|
||||
if ($request->fileSizeBytes > 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->fileContents,
|
||||
originalName: $request->fileOriginalName,
|
||||
mimeType: $request->fileMimeType,
|
||||
sizeBytes: $request->fileSizeBytes,
|
||||
);
|
||||
$path = $this->fileUploader->upload($pdf, 'element-pdfs/long');
|
||||
$element->setLongPdfPath($path);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue