From 795325f81e4c221e3451b17e26480d43f3f274d2 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 14 Jul 2023 18:34:01 +0200 Subject: [PATCH] Invert the logic by short-circuiting for thumbnails --- .../system/bbcode/AttachmentBBCode.class.php | 70 +++++++++++-------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php b/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php index be5eb8250d..529c909c05 100644 --- a/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php @@ -46,7 +46,12 @@ final class AttachmentBBCode extends AbstractBBCode } } - return $this->showImage($attachment, $outputType, $openingTag['attributes'], $hasParentLink); + return $this->showImage( + $attachment, + $outputType, + $openingTag['attributes'], + $hasParentLink, + ); } elseif (\substr($attachment->fileType, 0, 6) === 'video/' && $outputType == 'text/html') { return $this->showVideoPlayer($attachment); } elseif (\substr($attachment->fileType, 0, 6) === 'audio/' && $outputType == 'text/html') { @@ -61,25 +66,32 @@ final class AttachmentBBCode extends AbstractBBCode $alignment = $attributes[1] ?? ''; $thumbnail = $this->renderImageAsThumbnail($attachment, $outputType, $attributes[2] ?? false); - if ($thumbnail === false) { - $class = match ($alignment) { - "left" => "messageFloatObjectLeft", - "right" => "messageFloatObjectRight", - default => "" - }; - - $source = StringUtil::encodeHTML($attachment->getLink()); - $title = StringUtil::encodeHTML($attachment->filename); - $imageElement = \sprintf( - '', - $source, - $attachment->width, - $attachment->height, + if ($thumbnail) { + return $this->showImageAsThumbnail( + $attachment, + $alignment, + $hasParentLink, ); + } + + $class = match ($alignment) { + "left" => "messageFloatObjectLeft", + "right" => "messageFloatObjectRight", + default => "" + }; + + $source = StringUtil::encodeHTML($attachment->getLink()); + $title = StringUtil::encodeHTML($attachment->filename); + $imageElement = \sprintf( + '', + $source, + $attachment->width, + $attachment->height, + ); - if (!$hasParentLink && ($attachment->width > ATTACHMENT_THUMBNAIL_WIDTH || $attachment->height > ATTACHMENT_THUMBNAIL_HEIGHT)) { - return \sprintf( - <<<'HTML' + if (!$hasParentLink && ($attachment->width > ATTACHMENT_THUMBNAIL_WIDTH || $attachment->height > ATTACHMENT_THUMBNAIL_HEIGHT)) { + return \sprintf( + <<<'HTML' %s @@ -87,22 +99,24 @@ final class AttachmentBBCode extends AbstractBBCode HTML, - $source, - $title, - $class, - $imageElement, - FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24), - ); - } - - return \sprintf( - '%s', + $source, $title, $class, $imageElement, + FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24), ); } + return \sprintf( + '%s', + $title, + $class, + $imageElement, + ); + } + + private function showImageAsThumbnail(Attachment $attachment, string $alignment, bool $hasParentLink): string + { $enlargeImageControls = \sprintf( '%s', FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24), -- 2.20.1