From dbff543fa8c8ccad06a45457d8901f008a2e8025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 27 Nov 2023 22:59:02 +0100 Subject: [PATCH] Remove an attachment as preview image of an element, if it is not an image anymore through a change --- .../Helper/StructuralEntityChoiceHelper.php | 3 ++- .../Attachments/AttachmentSubmitHandler.php | 23 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Form/Type/Helper/StructuralEntityChoiceHelper.php b/src/Form/Type/Helper/StructuralEntityChoiceHelper.php index 69763e38..0c596f83 100644 --- a/src/Form/Type/Helper/StructuralEntityChoiceHelper.php +++ b/src/Form/Type/Helper/StructuralEntityChoiceHelper.php @@ -75,7 +75,8 @@ class StructuralEntityChoiceHelper } if ($choice instanceof HasMasterAttachmentInterface) { - $tmp['data-image'] = $choice->getMasterPictureAttachment() instanceof Attachment ? + $tmp['data-image'] = ($choice->getMasterPictureAttachment() instanceof Attachment + && $choice->getMasterPictureAttachment()->isPicture()) ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null diff --git a/src/Services/Attachments/AttachmentSubmitHandler.php b/src/Services/Attachments/AttachmentSubmitHandler.php index 0d1b29c6..8470c392 100644 --- a/src/Services/Attachments/AttachmentSubmitHandler.php +++ b/src/Services/Attachments/AttachmentSubmitHandler.php @@ -64,7 +64,7 @@ class AttachmentSubmitHandler 'htpasswd', '']; public function __construct(protected AttachmentPathResolver $pathResolver, protected bool $allow_attachments_downloads, - protected HttpClientInterface $httpClient, protected MimeTypesInterface $mimeTypes, + protected HttpClientInterface $httpClient, protected MimeTypesInterface $mimeTypes, protected FileTypeFilterTools $filterTools, /** * @var string The user configured maximum upload size. This is a string like "10M" or "1G" and will be converted to */ @@ -204,13 +204,22 @@ class AttachmentSubmitHandler //Rename blacklisted (unsecure) files to a better extension $this->renameBlacklistedExtensions($attachment); - //Check if we should assign this attachment to master picture - //this is only possible if the attachment is new (not yet persisted to DB) - if ($options['become_preview_if_empty'] && null === $attachment->getID() && $attachment->isPicture()) { - $element = $attachment->getElement(); - if ($element instanceof AttachmentContainingDBElement && !$element->getMasterPictureAttachment() instanceof Attachment) { + //Set / Unset the master picture attachment / preview image + $element = $attachment->getElement(); + if ($element instanceof AttachmentContainingDBElement) { + //Make this attachment the master picture if needed and this was requested + if ($options['become_preview_if_empty'] + && $element->getMasterPictureAttachment() === null //Element must not have an preview image yet + && null === $attachment->getID() //Attachment must be null + && $attachment->isPicture() //Attachment must be a picture + ) { $element->setMasterPictureAttachment($attachment); } + + //If this attachment is the master picture, but is not a picture anymore, dont use it as master picture anymore + if ($element->getMasterPictureAttachment() === $attachment && !$attachment->isPicture()) { + $element->setMasterPictureAttachment(null); + } } return $attachment; @@ -238,7 +247,7 @@ class AttachmentSubmitHandler //Check if the extension is blacklisted and replace the file extension with txt if needed if(in_array($ext, self::BLACKLISTED_EXTENSIONS, true)) { $new_path = $this->generateAttachmentPath($attachment, $attachment->isSecure()) - .DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'txt'); + .DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'txt'); //Move file to new directory $fs = new Filesystem();