From f538f2f6d3e5d0cc1176ac9fcdabca251043d69f Mon Sep 17 00:00:00 2001 From: Halo Dev Bot <87291978+halo-dev-bot@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:39:52 +0800 Subject: [PATCH] [release-2.19] fix: skip thumbnail generation for GIF images (#6609) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an automated cherry-pick of #6597 /assign guqing ```release-note 修复 GIF 缩略图生成只会保留第一帧的问题 ``` --- .../core/attachment/ThumbnailGenerator.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/run/halo/app/core/attachment/ThumbnailGenerator.java b/application/src/main/java/run/halo/app/core/attachment/ThumbnailGenerator.java index 1bf189d903..59f543e759 100644 --- a/application/src/main/java/run/halo/app/core/attachment/ThumbnailGenerator.java +++ b/application/src/main/java/run/halo/app/core/attachment/ThumbnailGenerator.java @@ -13,12 +13,14 @@ import java.nio.file.Path; import java.util.Iterator; import java.util.Optional; +import java.util.Set; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.imgscalr.Scalr; +import org.springframework.lang.NonNull; @Slf4j @AllArgsConstructor @@ -29,6 +31,8 @@ public class ThumbnailGenerator { */ static final int MAX_FILE_SIZE = 30 * 1024 * 1024; + private static final Set UNSUPPORTED_FORMATS = Set.of("gif", "svg", "webp"); + private final ImageDownloader imageDownloader = new ImageDownloader(); private final ThumbnailSize size; private final Path storePath; @@ -70,13 +74,16 @@ private void generateThumbnail(Path tempImagePath) throws IOException { if (file.length() > MAX_FILE_SIZE) { throw new IOException("File size exceeds the limit: " + MAX_FILE_SIZE); } - var formatNameOpt = getFormatName(file); + String formatName = getFormatName(file) + .orElseThrow(() -> new UnsupportedOperationException("Unknown format")); + if (isUnsupportedFormat(formatName)) { + throw new UnsupportedOperationException("Unsupported image format for: " + formatName); + } + var img = ImageIO.read(file); if (img == null) { - throw new UnsupportedOperationException( - "Unsupported image format for: " + formatNameOpt.orElse("unknown")); + throw new UnsupportedOperationException("Cannot read image file: " + file); } - var formatName = formatNameOpt.orElse("jpg"); var thumbnailFile = getThumbnailFile(formatName); if (img.getWidth() <= size.getWidth()) { Files.copy(tempImagePath, thumbnailFile.toPath(), REPLACE_EXISTING); @@ -87,6 +94,10 @@ private void generateThumbnail(Path tempImagePath) throws IOException { ImageIO.write(thumbnail, formatName, thumbnailFile); } + private static boolean isUnsupportedFormat(@NonNull String formatName) { + return UNSUPPORTED_FORMATS.contains(formatName.toLowerCase()); + } + private File getThumbnailFile(String formatName) { return Optional.of(storePath) .map(path -> {