Adapter attachment upload forms to the new system

This commit is contained in:
Jan Böhmer 2024-03-03 18:52:06 +01:00
parent 0c33059c4e
commit 3585b8a56a
7 changed files with 32 additions and 22 deletions

View file

@ -59,7 +59,7 @@ final class HandleAttachmentsUploadsProcessor implements ProcessorInterface
//Reset the upload data //Reset the upload data
$data->setUpload(null); $data->setUpload(null);
$this->attachmentSubmitHandler->handleFormSubmit($data, $upload); $this->attachmentSubmitHandler->handleUpload($data, $upload);
} }
$result = $this->persistProcessor->process($data, $operation, $uriVariables, $context); $result = $this->persistProcessor->process($data, $operation, $uriVariables, $context);

View file

@ -24,6 +24,7 @@ namespace App\Controller\AdminPages;
use App\DataTables\LogDataTable; use App\DataTables\LogDataTable;
use App\Entity\Attachments\Attachment; use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentUpload;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractPartsContainingDBElement; use App\Entity\Base\AbstractPartsContainingDBElement;
@ -175,16 +176,10 @@ abstract class BaseAdminController extends AbstractController
$attachments = $form['attachments']; $attachments = $form['attachments'];
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
/** @var FormInterface $attachment */ /** @var FormInterface $attachment */
$options = [
'secure_attachment' => $attachment['secureFile']->getData(),
'download_url' => $attachment['downloadURL']->getData(),
];
try { try {
$this->attachmentSubmitHandler->handleFormSubmit( $this->attachmentSubmitHandler->handleUpload(
$attachment->getData(), $attachment->getData(),
$attachment['file']->getData(), AttachmentUpload::fromAttachmentForm($attachment)
$options
); );
} catch (AttachmentDownloadException $attachmentDownloadException) { } catch (AttachmentDownloadException $attachmentDownloadException) {
$this->addFlash( $this->addFlash(
@ -270,10 +265,9 @@ abstract class BaseAdminController extends AbstractController
]; ];
try { try {
$this->attachmentSubmitHandler->handleFormSubmit( $this->attachmentSubmitHandler->handleUpload(
$attachment->getData(), $attachment->getData(),
$attachment['file']->getData(), AttachmentUpload::fromAttachmentForm($attachment)
$options
); );
} catch (AttachmentDownloadException $attachmentDownloadException) { } catch (AttachmentDownloadException $attachmentDownloadException) {
$this->addFlash( $this->addFlash(

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use App\DataTables\LogDataTable; use App\DataTables\LogDataTable;
use App\Entity\Attachments\AttachmentUpload;
use App\Entity\Parts\Category; use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint; use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Manufacturer;
@ -301,13 +302,9 @@ class PartController extends AbstractController
$attachments = $form['attachments']; $attachments = $form['attachments'];
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
/** @var FormInterface $attachment */ /** @var FormInterface $attachment */
$options = [
'secure_attachment' => $attachment['secureFile']->getData(),
'download_url' => $attachment['downloadURL']->getData(),
];
try { try {
$this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); $this->attachmentSubmitHandler->handleUpload($attachment->getData(), AttachmentUpload::fromAttachmentForm($attachment));
} catch (AttachmentDownloadException $attachmentDownloadException) { } catch (AttachmentDownloadException $attachmentDownloadException) {
$this->addFlash( $this->addFlash(
'error', 'error',

View file

@ -36,7 +36,6 @@ use App\ApiPlatform\DocumentedAPIProperty;
use App\ApiPlatform\Filter\EntityFilter; use App\ApiPlatform\Filter\EntityFilter;
use App\ApiPlatform\Filter\LikeFilter; use App\ApiPlatform\Filter\LikeFilter;
use App\ApiPlatform\HandleAttachmentsUploadsProcessor; use App\ApiPlatform\HandleAttachmentsUploadsProcessor;
use App\EntityListeners\AttachmentUploadListener;
use App\Repository\AttachmentRepository; use App\Repository\AttachmentRepository;
use App\EntityListeners\AttachmentDeleteListener; use App\EntityListeners\AttachmentDeleteListener;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Entity\Attachments; namespace App\Entity\Attachments;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Attribute\Groups; use Symfony\Component\Serializer\Attribute\Groups;
@ -54,4 +55,23 @@ class AttachmentUpload
public readonly ?bool $becomePreviewIfEmpty = true, public readonly ?bool $becomePreviewIfEmpty = true,
) { ) {
} }
/**
* Creates an AttachmentUpload object from an Attachment FormInterface
* @param FormInterface $form
* @return AttachmentUpload
*/
public static function fromAttachmentForm(FormInterface $form): AttachmentUpload
{
if (!$form->has('file')) {
throw new \InvalidArgumentException('The form does not have a file field. Is it an attachment form?');
}
return new self(
file: $form->get('file')->getData(),
downloadUrl: $form->get('downloadURL')->getData(),
private: $form->get('secureFile')->getData()
);
}
} }

View file

@ -186,7 +186,7 @@ class AttachmentSubmitHandler
* *
* @return Attachment The attachment with the new filename (same instance as passed $attachment) * @return Attachment The attachment with the new filename (same instance as passed $attachment)
*/ */
public function handleFormSubmit(Attachment $attachment, ?AttachmentUpload $upload): Attachment public function handleUpload(Attachment $attachment, ?AttachmentUpload $upload): Attachment
{ {
if ($upload === null) { if ($upload === null) {
$upload = $attachment->getUpload(); $upload = $attachment->getUpload();
@ -333,7 +333,7 @@ class AttachmentSubmitHandler
* *
* @return Attachment The attachment with the new filepath * @return Attachment The attachment with the new filepath
*/ */
protected function downloadURL(Attachment $attachment, array $secureAttachment): Attachment protected function downloadURL(Attachment $attachment, bool $secureAttachment): Attachment
{ {
//Check if we are allowed to download files //Check if we are allowed to download files
if (!$this->allow_attachments_downloads) { if (!$this->allow_attachments_downloads) {

View file

@ -25,6 +25,7 @@ namespace App\Services\UserSystem;
use App\Entity\Attachments\Attachment; use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentType; use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\AttachmentUpload;
use App\Entity\Attachments\UserAttachment; use App\Entity\Attachments\UserAttachment;
use App\Entity\UserSystem\User; use App\Entity\UserSystem\User;
use App\Services\Attachments\AttachmentSubmitHandler; use App\Services\Attachments\AttachmentSubmitHandler;
@ -156,11 +157,10 @@ class UserAvatarHelper
} }
$attachment->setAttachmentType($attachment_type); $attachment->setAttachmentType($attachment_type);
//$user->setMasterPictureAttachment($attachment);
} }
//Handle the upload //Handle the upload
$this->submitHandler->handleFormSubmit($attachment, $file); $this->submitHandler->handleUpload($attachment, new AttachmentUpload(file: $file));
//Set attachment as master picture //Set attachment as master picture
$user->setMasterPictureAttachment($attachment); $user->setMasterPictureAttachment($attachment);