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
|
|
@ -9,15 +9,23 @@ use App\Exceptions\NotFoundException;
|
|||
|
||||
class UpdateElement
|
||||
{
|
||||
private const FILE_TYPE_ICON_IMAGE = 'iconImage';
|
||||
|
||||
private const FILE_TYPE_SHORT_PDF = 'shortPdf';
|
||||
|
||||
private const FILE_TYPE_LONG_PDF = 'longPdf';
|
||||
|
||||
public function __construct(
|
||||
private UpdateTitle $updateTitle,
|
||||
private UpdateDescription $updateDescription,
|
||||
private UpdateIconImageUrl $updateIconImageUrl,
|
||||
private UpdateRichText $updateRichText,
|
||||
private UpdatePdfPath $updatePdfPath,
|
||||
private UpdateShortPdfPath $updateShortPdfPath,
|
||||
private UpdateLongPdfPath $updateLongPdfPath,
|
||||
private UpdateYoutubeUrl $updateYoutubeUrl,
|
||||
private UpdateIconImage $updateIconImage,
|
||||
private UpdatePdf $updatePdf,
|
||||
private UpdateShortPdf $updateShortPdf,
|
||||
private UpdateLongPdf $updateLongPdf,
|
||||
private ElementRepository $elementRepository,
|
||||
) {
|
||||
}
|
||||
|
|
@ -36,10 +44,15 @@ class UpdateElement
|
|||
&& $request->description === null
|
||||
&& $request->iconImageUrl === null
|
||||
&& $request->richText === null
|
||||
&& $request->shortPdfPath === null
|
||||
&& $request->longPdfPath === null
|
||||
&& $request->pdfPath === null
|
||||
&& $request->youtubeUrl === null
|
||||
&& $request->iconImageContents === null
|
||||
&& $request->pdfContents === null;
|
||||
&& $request->fileType === null
|
||||
&& $request->fileContents === null
|
||||
&& $request->fileOriginalName === null
|
||||
&& $request->fileMimeType === null
|
||||
&& $request->fileSizeBytes === null;
|
||||
if ($hasNoFields) {
|
||||
throw new BadRequestException('nothing to update');
|
||||
}
|
||||
|
|
@ -70,10 +83,21 @@ class UpdateElement
|
|||
richText: $request->richText,
|
||||
));
|
||||
}
|
||||
if ($request->pdfPath !== null) {
|
||||
$this->updatePdfPath->execute(new UpdatePdfPathRequest(
|
||||
if ($request->shortPdfPath !== null) {
|
||||
$this->updateShortPdfPath->execute(new UpdateShortPdfPathRequest(
|
||||
id: $request->id,
|
||||
pdfPath: $request->pdfPath,
|
||||
shortPdfPath: $request->shortPdfPath,
|
||||
));
|
||||
} elseif ($request->pdfPath !== null) {
|
||||
$this->updateShortPdfPath->execute(new UpdateShortPdfPathRequest(
|
||||
id: $request->id,
|
||||
shortPdfPath: $request->pdfPath,
|
||||
));
|
||||
}
|
||||
if ($request->longPdfPath !== null) {
|
||||
$this->updateLongPdfPath->execute(new UpdateLongPdfPathRequest(
|
||||
id: $request->id,
|
||||
longPdfPath: $request->longPdfPath,
|
||||
));
|
||||
}
|
||||
if ($request->youtubeUrl !== null) {
|
||||
|
|
@ -82,27 +106,8 @@ class UpdateElement
|
|||
youtubeUrl: $request->youtubeUrl,
|
||||
));
|
||||
}
|
||||
if ($request->iconImageContents !== null) {
|
||||
$this->updateIconImage->execute(
|
||||
new UpdateIconImageRequest(
|
||||
id: $request->id,
|
||||
iconImageContents: $request->iconImageContents,
|
||||
iconImageOriginalName: $request->iconImageOriginalName,
|
||||
iconImageMimeType: $request->iconImageMimeType,
|
||||
iconImageSizeBytes: $request->iconImageSizeBytes,
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($request->pdfContents !== null) {
|
||||
$this->updatePdf->execute(
|
||||
new UpdatePdfRequest(
|
||||
id: $request->id,
|
||||
pdfContents: $request->pdfContents,
|
||||
pdfOriginalName: $request->pdfOriginalName,
|
||||
pdfMimeType: $request->pdfMimeType,
|
||||
pdfSizeBytes: $request->pdfSizeBytes,
|
||||
)
|
||||
);
|
||||
if ($this->hasFileInput($request)) {
|
||||
$this->updateFile($request);
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
|
|
@ -112,4 +117,68 @@ class UpdateElement
|
|||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
private function updateFile(UpdateElementRequest $request): void
|
||||
{
|
||||
if ($request->fileType === null) {
|
||||
throw new BadRequestException('fileType is required');
|
||||
}
|
||||
|
||||
if ($request->fileType === self::FILE_TYPE_ICON_IMAGE) {
|
||||
$this->updateIconImage->execute(
|
||||
new UpdateIconImageRequest(
|
||||
id: $request->id,
|
||||
fileContents: $request->fileContents,
|
||||
fileOriginalName: $request->fileOriginalName,
|
||||
fileMimeType: $request->fileMimeType,
|
||||
fileSizeBytes: $request->fileSizeBytes,
|
||||
)
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($request->fileType === self::FILE_TYPE_SHORT_PDF) {
|
||||
$this->updateShortPdf->execute(
|
||||
new UpdateShortPdfRequest(
|
||||
id: $request->id,
|
||||
fileContents: $request->fileContents,
|
||||
fileOriginalName: $request->fileOriginalName,
|
||||
fileMimeType: $request->fileMimeType,
|
||||
fileSizeBytes: $request->fileSizeBytes,
|
||||
)
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($request->fileType === self::FILE_TYPE_LONG_PDF) {
|
||||
$this->updateLongPdf->execute(
|
||||
new UpdateLongPdfRequest(
|
||||
id: $request->id,
|
||||
fileContents: $request->fileContents,
|
||||
fileOriginalName: $request->fileOriginalName,
|
||||
fileMimeType: $request->fileMimeType,
|
||||
fileSizeBytes: $request->fileSizeBytes,
|
||||
)
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new BadRequestException('fileType is invalid');
|
||||
}
|
||||
|
||||
private function hasFileInput(UpdateElementRequest $request): bool
|
||||
{
|
||||
return $request->fileType !== null
|
||||
|| $request->fileContents !== null
|
||||
|| $request->fileOriginalName !== null
|
||||
|| $request->fileMimeType !== null
|
||||
|| $request->fileSizeBytes !== null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,16 +10,15 @@ class UpdateElementRequest
|
|||
public ?string $description,
|
||||
public ?string $iconImageUrl,
|
||||
public ?string $richText,
|
||||
public ?string $shortPdfPath,
|
||||
public ?string $longPdfPath,
|
||||
public ?string $pdfPath,
|
||||
public ?string $youtubeUrl,
|
||||
public ?string $iconImageContents,
|
||||
public ?string $iconImageOriginalName,
|
||||
public ?string $iconImageMimeType,
|
||||
public ?int $iconImageSizeBytes,
|
||||
public ?string $pdfContents,
|
||||
public ?string $pdfOriginalName,
|
||||
public ?string $pdfMimeType,
|
||||
public ?int $pdfSizeBytes,
|
||||
public ?string $fileType,
|
||||
public ?string $fileContents,
|
||||
public ?string $fileOriginalName,
|
||||
public ?string $fileMimeType,
|
||||
public ?int $fileSizeBytes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,17 +36,17 @@ class UpdateIconImage
|
|||
}
|
||||
|
||||
if (
|
||||
$request->iconImageContents === null
|
||||
|| $request->iconImageOriginalName === null
|
||||
|| $request->iconImageMimeType === null
|
||||
|| $request->iconImageSizeBytes === null
|
||||
$request->fileContents === null
|
||||
|| $request->fileOriginalName === null
|
||||
|| $request->fileMimeType === null
|
||||
|| $request->fileSizeBytes === null
|
||||
) {
|
||||
throw new BadRequestException('icon image is required');
|
||||
}
|
||||
|
||||
if (
|
||||
! in_array(
|
||||
$request->iconImageMimeType,
|
||||
$request->fileMimeType,
|
||||
self::ALLOWED_MIME_TYPES,
|
||||
true,
|
||||
)
|
||||
|
|
@ -56,7 +56,7 @@ class UpdateIconImage
|
|||
);
|
||||
}
|
||||
|
||||
if ($request->iconImageSizeBytes > self::MAX_SIZE_BYTES) {
|
||||
if ($request->fileSizeBytes > self::MAX_SIZE_BYTES) {
|
||||
throw new BadRequestException(
|
||||
'icon image must be 5MB or smaller',
|
||||
);
|
||||
|
|
@ -68,10 +68,10 @@ class UpdateIconImage
|
|||
}
|
||||
|
||||
$iconImage = new FileToUpload(
|
||||
contents: $request->iconImageContents,
|
||||
originalName: $request->iconImageOriginalName,
|
||||
mimeType: $request->iconImageMimeType,
|
||||
sizeBytes: $request->iconImageSizeBytes,
|
||||
contents: $request->fileContents,
|
||||
originalName: $request->fileOriginalName,
|
||||
mimeType: $request->fileMimeType,
|
||||
sizeBytes: $request->fileSizeBytes,
|
||||
);
|
||||
$path = $this->fileUploader->upload($iconImage, 'element-icons');
|
||||
$element->setIconImageUrl($path);
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ class UpdateIconImageRequest
|
|||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $iconImageContents,
|
||||
public ?string $iconImageOriginalName,
|
||||
public ?string $iconImageMimeType,
|
||||
public ?int $iconImageSizeBytes,
|
||||
public ?string $fileContents,
|
||||
public ?string $fileOriginalName,
|
||||
public ?string $fileMimeType,
|
||||
public ?int $fileSizeBytes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use App\Exceptions\NotFoundException;
|
|||
use App\Shared\Files\FileToUpload;
|
||||
use App\Shared\Files\FileUploader;
|
||||
|
||||
class UpdatePdf
|
||||
class UpdateLongPdf
|
||||
{
|
||||
private const ALLOWED_MIME_TYPES = [
|
||||
'application/pdf',
|
||||
|
|
@ -27,24 +27,24 @@ class UpdatePdf
|
|||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdatePdfRequest $request): Element
|
||||
public function execute(UpdateLongPdfRequest $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
|
||||
$request->fileContents === null
|
||||
|| $request->fileOriginalName === null
|
||||
|| $request->fileMimeType === null
|
||||
|| $request->fileSizeBytes === null
|
||||
) {
|
||||
throw new BadRequestException('pdf is required');
|
||||
}
|
||||
|
||||
if (
|
||||
! in_array(
|
||||
$request->pdfMimeType,
|
||||
$request->fileMimeType,
|
||||
self::ALLOWED_MIME_TYPES,
|
||||
true,
|
||||
)
|
||||
|
|
@ -52,7 +52,7 @@ class UpdatePdf
|
|||
throw new BadRequestException('pdf must be a pdf file');
|
||||
}
|
||||
|
||||
if ($request->pdfSizeBytes > self::MAX_SIZE_BYTES) {
|
||||
if ($request->fileSizeBytes > self::MAX_SIZE_BYTES) {
|
||||
throw new BadRequestException('pdf must be 10MB or smaller');
|
||||
}
|
||||
|
||||
|
|
@ -62,13 +62,13 @@ class UpdatePdf
|
|||
}
|
||||
|
||||
$pdf = new FileToUpload(
|
||||
contents: $request->pdfContents,
|
||||
originalName: $request->pdfOriginalName,
|
||||
mimeType: $request->pdfMimeType,
|
||||
sizeBytes: $request->pdfSizeBytes,
|
||||
contents: $request->fileContents,
|
||||
originalName: $request->fileOriginalName,
|
||||
mimeType: $request->fileMimeType,
|
||||
sizeBytes: $request->fileSizeBytes,
|
||||
);
|
||||
$path = $this->fileUploader->upload($pdf, 'element-pdfs');
|
||||
$element->setPdfPath($path);
|
||||
$path = $this->fileUploader->upload($pdf, 'element-pdfs/long');
|
||||
$element->setLongPdfPath($path);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ use App\Exceptions\BadRequestException;
|
|||
use App\Exceptions\NotFoundException;
|
||||
use App\Shared\Files\FileUploader;
|
||||
|
||||
class UpdatePdfPath
|
||||
class UpdateLongPdfPath
|
||||
{
|
||||
public function __construct(
|
||||
private ElementRepository $elementRepository,
|
||||
|
|
@ -20,14 +20,14 @@ class UpdatePdfPath
|
|||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdatePdfPathRequest $request): Element
|
||||
public function execute(UpdateLongPdfPathRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->pdfPath === null) {
|
||||
throw new BadRequestException('pdfPath is required');
|
||||
if ($request->longPdfPath === null) {
|
||||
throw new BadRequestException('longPdfPath is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
|
|
@ -35,12 +35,12 @@ class UpdatePdfPath
|
|||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$pdfPath = $this->nullableString($request->pdfPath);
|
||||
if ($pdfPath === null) {
|
||||
$this->deleteManagedFile($element->getPdfPath());
|
||||
$longPdfPath = $this->nullableString($request->longPdfPath);
|
||||
if ($longPdfPath === null) {
|
||||
$this->deleteManagedFile($element->getLongPdfPath());
|
||||
}
|
||||
|
||||
$element->setPdfPath($pdfPath);
|
||||
$element->setLongPdfPath($longPdfPath);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdatePdfPathRequest
|
||||
class UpdateLongPdfPathRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $pdfPath,
|
||||
public ?string $longPdfPath,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateLongPdfRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $fileContents,
|
||||
public ?string $fileOriginalName,
|
||||
public ?string $fileMimeType,
|
||||
public ?int $fileSizeBytes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdatePdfRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $pdfContents,
|
||||
public ?string $pdfOriginalName,
|
||||
public ?string $pdfMimeType,
|
||||
public ?int $pdfSizeBytes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -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 UpdateShortPdf
|
||||
{
|
||||
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(UpdateShortPdfRequest $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/short');
|
||||
$element->setShortPdfPath($path);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
<?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\FileUploader;
|
||||
|
||||
class UpdateShortPdfPath
|
||||
{
|
||||
public function __construct(
|
||||
private ElementRepository $elementRepository,
|
||||
private FileUploader $fileUploader,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdateShortPdfPathRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->shortPdfPath === null) {
|
||||
throw new BadRequestException('shortPdfPath is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$shortPdfPath = $this->nullableString($request->shortPdfPath);
|
||||
if ($shortPdfPath === null) {
|
||||
$this->deleteManagedFile($element->getShortPdfPath());
|
||||
}
|
||||
|
||||
$element->setShortPdfPath($shortPdfPath);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
|
||||
private function nullableString(string $value): ?string
|
||||
{
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function deleteManagedFile(?string $path): void
|
||||
{
|
||||
if ($path === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fileUploader->delete($path);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateShortPdfPathRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $shortPdfPath,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateShortPdfRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $fileContents,
|
||||
public ?string $fileOriginalName,
|
||||
public ?string $fileMimeType,
|
||||
public ?int $fileSizeBytes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue