fix: skip thumbnail generation for GIF images (#6597)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
修复 GIF 缩略图生成只会保留第一帧的问题

#### Which issue(s) this PR fixes:
Fixes #6596

#### Does this PR introduce a user-facing change?
```release-note
修复 GIF 缩略图生成只会保留第一帧的问题
```
pull/6602/head^2
guqing 2024-09-06 17:27:52 +08:00 committed by GitHub
parent 6cdd2a7588
commit 2ea063d37f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 4 deletions

View File

@ -13,12 +13,14 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Iterator; import java.util.Iterator;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.ImageReader; import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageInputStream;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.imgscalr.Scalr; import org.imgscalr.Scalr;
import org.springframework.lang.NonNull;
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
@ -29,6 +31,8 @@ public class ThumbnailGenerator {
*/ */
static final int MAX_FILE_SIZE = 30 * 1024 * 1024; static final int MAX_FILE_SIZE = 30 * 1024 * 1024;
private static final Set<String> UNSUPPORTED_FORMATS = Set.of("gif", "svg", "webp");
private final ImageDownloader imageDownloader = new ImageDownloader(); private final ImageDownloader imageDownloader = new ImageDownloader();
private final ThumbnailSize size; private final ThumbnailSize size;
private final Path storePath; private final Path storePath;
@ -70,13 +74,16 @@ public class ThumbnailGenerator {
if (file.length() > MAX_FILE_SIZE) { if (file.length() > MAX_FILE_SIZE) {
throw new IOException("File size exceeds the limit: " + 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); var img = ImageIO.read(file);
if (img == null) { if (img == null) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException("Cannot read image file: " + file);
"Unsupported image format for: " + formatNameOpt.orElse("unknown"));
} }
var formatName = formatNameOpt.orElse("jpg");
var thumbnailFile = getThumbnailFile(formatName); var thumbnailFile = getThumbnailFile(formatName);
if (img.getWidth() <= size.getWidth()) { if (img.getWidth() <= size.getWidth()) {
Files.copy(tempImagePath, thumbnailFile.toPath(), REPLACE_EXISTING); Files.copy(tempImagePath, thumbnailFile.toPath(), REPLACE_EXISTING);
@ -87,6 +94,10 @@ public class ThumbnailGenerator {
ImageIO.write(thumbnail, formatName, thumbnailFile); ImageIO.write(thumbnail, formatName, thumbnailFile);
} }
private static boolean isUnsupportedFormat(@NonNull String formatName) {
return UNSUPPORTED_FORMATS.contains(formatName.toLowerCase());
}
private File getThumbnailFile(String formatName) { private File getThumbnailFile(String formatName) {
return Optional.of(storePath) return Optional.of(storePath)
.map(path -> { .map(path -> {