Remove an attachment as preview image of an element, if it is not an image anymore through a change

This commit is contained in:
Jan Böhmer 2023-11-27 22:59:02 +01:00
parent 08bd4d54e3
commit dbff543fa8
2 changed files with 18 additions and 8 deletions

View file

@ -75,7 +75,8 @@ class StructuralEntityChoiceHelper
} }
if ($choice instanceof HasMasterAttachmentInterface) { 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(), $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(),
'thumbnail_xs') 'thumbnail_xs')
: null : null

View file

@ -64,7 +64,7 @@ class AttachmentSubmitHandler
'htpasswd', '']; 'htpasswd', ''];
public function __construct(protected AttachmentPathResolver $pathResolver, protected bool $allow_attachments_downloads, 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, /** protected FileTypeFilterTools $filterTools, /**
* @var string The user configured maximum upload size. This is a string like "10M" or "1G" and will be converted to * @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 //Rename blacklisted (unsecure) files to a better extension
$this->renameBlacklistedExtensions($attachment); $this->renameBlacklistedExtensions($attachment);
//Check if we should assign this attachment to master picture //Set / Unset the master picture attachment / preview image
//this is only possible if the attachment is new (not yet persisted to DB) $element = $attachment->getElement();
if ($options['become_preview_if_empty'] && null === $attachment->getID() && $attachment->isPicture()) { if ($element instanceof AttachmentContainingDBElement) {
$element = $attachment->getElement(); //Make this attachment the master picture if needed and this was requested
if ($element instanceof AttachmentContainingDBElement && !$element->getMasterPictureAttachment() instanceof Attachment) { 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); $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; return $attachment;
@ -238,7 +247,7 @@ class AttachmentSubmitHandler
//Check if the extension is blacklisted and replace the file extension with txt if needed //Check if the extension is blacklisted and replace the file extension with txt if needed
if(in_array($ext, self::BLACKLISTED_EXTENSIONS, true)) { if(in_array($ext, self::BLACKLISTED_EXTENSIONS, true)) {
$new_path = $this->generateAttachmentPath($attachment, $attachment->isSecure()) $new_path = $this->generateAttachmentPath($attachment, $attachment->isSecure())
.DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'txt'); .DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'txt');
//Move file to new directory //Move file to new directory
$fs = new Filesystem(); $fs = new Filesystem();