Skip to content

Commit

Permalink
fix: restrict thumbnail generation to images in the attachment library
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Nov 25, 2024
1 parent e5be856 commit 4d4ca57
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static String buildSizesAttr() {
*/
public static Mono<String> generateSrcset(URI src, ThumbnailService thumbnailService) {
return Flux.fromArray(ThumbnailSize.values())
.flatMap(size -> thumbnailService.generate(src, size)
.flatMap(size -> thumbnailService.get(src, size)
.map(thumbnail -> thumbnail.toString() + " " + size.getWidth() + "w")
)
.collect(StringBuilder::new, (builder, srcsetValue) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,16 @@ public interface ThumbnailService {
*/
Mono<URI> generate(URI imageUri, ThumbnailSize size);

/**
* <p>Get thumbnail by the given image uri and size.</p>
* <p>It depends on the {@link #generate(URI, ThumbnailSize)} method, currently the thumbnail
* generation is limited to the attachment service, that is, the thumbnail is strongly
* associated with the attachment.</p>
*
* @return if thumbnail exists, return the thumbnail uri, otherwise return the original image
* uri
*/
Mono<URI> get(URI imageUri, ThumbnailSize size);

Mono<Void> delete(URI imageUri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public Mono<URI> generate(URI imageUri, ThumbnailSize size) {
});
}

@Override
public Mono<URI> get(URI imageUri, ThumbnailSize size) {
return fetchThumbnail(imageUri, size)
.map(thumbnail -> URI.create(thumbnail.getSpec().getThumbnailUri()))
.defaultIfEmpty(imageUri);
}

@Override
public Mono<Void> delete(URI imageUri) {
Assert.notNull(imageUri, "Image uri must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public RouterFunction<ServerResponse> endpoint() {

private Mono<ServerResponse> getThumbnailByUri(ServerRequest request) {
var query = new ThumbnailQuery(request.queryParams());
return thumbnailService.generate(query.getUri(), query.getSize())
return thumbnailService.get(query.getUri(), query.getSize())
.filterWhen(uri -> isAccessible(request, uri))
.defaultIfEmpty(query.getUri())
.flatMap(uri -> ServerResponse.temporaryRedirect(uri).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ThumbnailFinderImpl implements ThumbnailFinder {
@Override
public Mono<String> gen(String uriStr, String size) {
return Mono.fromSupplier(() -> URI.create(uriStr))
.flatMap(uri -> thumbnailService.generate(uri, ThumbnailSize.fromName(size)))
.flatMap(uri -> thumbnailService.get(uri, ThumbnailSize.fromName(size)))
.map(URI::toString)
.onErrorResume(Throwable.class, e -> {
log.debug("Failed to generate thumbnail for [{}], error: [{}]", uriStr,
Expand Down

0 comments on commit 4d4ca57

Please sign in to comment.