Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip thumbnail generation for GIF images #6597

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +31,8 @@ public class ThumbnailGenerator {
*/
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 ThumbnailSize size;
private final Path storePath;
Expand Down Expand Up @@ -71,12 +75,14 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
throw new IOException("File size exceeds the limit: " + MAX_FILE_SIZE);
}
var formatNameOpt = getFormatName(file);
var formatName = formatNameOpt.orElse("jpg");
guqing marked this conversation as resolved.
Show resolved Hide resolved
if (isUnsupportedFormat(formatName)) {
throwUnsupportedException(formatName);
}
var img = ImageIO.read(file);
if (img == null) {
throw new UnsupportedOperationException(
"Unsupported image format for: " + formatNameOpt.orElse("unknown"));
throwUnsupportedException(formatNameOpt.orElse("unknown"));
}
var formatName = formatNameOpt.orElse("jpg");
var thumbnailFile = getThumbnailFile(formatName);
if (img.getWidth() <= size.getWidth()) {
Files.copy(tempImagePath, thumbnailFile.toPath(), REPLACE_EXISTING);
Expand All @@ -87,6 +93,14 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
ImageIO.write(thumbnail, formatName, thumbnailFile);
}

private static void throwUnsupportedException(String formatName) {
throw new UnsupportedOperationException("Unsupported image format for: " + formatName);
}

private static boolean isUnsupportedFormat(@NonNull String formatName) {
return UNSUPPORTED_FORMATS.contains(formatName.toLowerCase());
}

private File getThumbnailFile(String formatName) {
return Optional.of(storePath)
.map(path -> {
Expand Down
Loading