Enhance thumbnail processing by validating upload paths and updating image size attributes

feat/add-thumbnail-router
John Niang 2025-09-26 16:59:45 +08:00
parent 7c1c25348f
commit 24b7b3f37f
No known key found for this signature in database
GPG Key ID: D7363C015BBCAA59
2 changed files with 14 additions and 7 deletions

View File

@ -78,6 +78,12 @@ class ThumbnailImgTagPostProcessor implements ElementTagPostProcessor {
}
}
var path = imageUri.getPath();
if (!path.startsWith("/upload/")) {
log.debug("Skip processing img tag with non-upload path: {}", path);
return Mono.empty();
}
var fileSuffix = FilenameUtils.getExtension(imageUri.getPath());
if (!ThumbnailUtils.isSupportedImage(fileSuffix)) {
log.debug("Skip processing img tag with unsupported image suffix: {}", fileSuffix);
@ -90,14 +96,13 @@ class ThumbnailImgTagPostProcessor implements ElementTagPostProcessor {
log.debug("Skip processing img tag because the image is not supported: {}", imageUri);
return Mono.empty();
}
var modelFactory = context.getModelFactory();
tag = modelFactory.setAttribute(tag, "size", """
(max-width: 400px) 400px, \
(max-width: 800px) 800px, \
(max-width: 1200px) 1200px, \
(max-width: 1600px) 1600px\
""");
var modelFactory = context.getModelFactory();
if (!tag.hasAttribute("sizes")) {
tag = modelFactory.setAttribute(tag, "sizes", """
(min-width: 1024px) 33vw, (min-width: 600px) 50vw, 100vw\
""");
}
var srcset = thumbnails.keySet().stream()
.map(size -> {
var uri = thumbnails.get(size);

View File

@ -7,6 +7,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.ThumbnailParameter;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
@ -126,6 +127,7 @@ class ThumbnailRouters {
Files.createDirectories(thumbnailPath.getParent());
Thumbnails.of(bufferedImage)
.width(size.getWidth())
.imageType(ThumbnailParameter.DEFAULT_IMAGE_TYPE)
.toFile(thumbnailPath.toFile());
log.info("Generated thumbnail for path: {}, target: {}, size: {}",
attachmentPath, thumbnailPath, size);