From d24d6d5c07a67e0013f48d76fb59e6c8e868c3a0 Mon Sep 17 00:00:00 2001 From: John Niang Date: Mon, 29 Sep 2025 19:00:38 +0800 Subject: [PATCH] Refactor thumbnail link handling to improve URI generation and streamline attachment processing --- .../endpoint/AttachmentHandler.java | 12 ---- .../LocalAttachmentUploadHandler.java | 56 +++++++++++++------ .../reconciler/AttachmentReconciler.java | 2 +- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/api/src/main/java/run/halo/app/core/extension/attachment/endpoint/AttachmentHandler.java b/api/src/main/java/run/halo/app/core/extension/attachment/endpoint/AttachmentHandler.java index 9436e31f2..58f80dc1f 100644 --- a/api/src/main/java/run/halo/app/core/extension/attachment/endpoint/AttachmentHandler.java +++ b/api/src/main/java/run/halo/app/core/extension/attachment/endpoint/AttachmentHandler.java @@ -3,7 +3,6 @@ package run.halo.app.core.extension.attachment.endpoint; import java.net.URI; import java.time.Duration; import java.util.Map; -import java.util.Optional; import org.pf4j.ExtensionPoint; import org.springframework.http.codec.multipart.FilePart; import reactor.core.publisher.Mono; @@ -70,17 +69,6 @@ public interface AttachmentHandler extends ExtensionPoint { return Mono.empty(); } - /** - * Build the thumbnail links from the given permalink. - * - * @param permalink the permalink - * @return an optional map of thumbnail sizes to their respective URIs - */ - default Optional> buildThumbnailLinks(URI permalink) { - // resolve the thumbnail link - return Optional.empty(); - } - interface UploadContext { FilePart file(); diff --git a/application/src/main/java/run/halo/app/core/attachment/endpoint/LocalAttachmentUploadHandler.java b/application/src/main/java/run/halo/app/core/attachment/endpoint/LocalAttachmentUploadHandler.java index 41cda91da..9dd616c9f 100644 --- a/application/src/main/java/run/halo/app/core/attachment/endpoint/LocalAttachmentUploadHandler.java +++ b/application/src/main/java/run/halo/app/core/attachment/endpoint/LocalAttachmentUploadHandler.java @@ -21,6 +21,7 @@ import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.RandomStringUtils; @@ -152,6 +153,20 @@ class LocalAttachmentUploadHandler implements AttachmentHandler { var attachment = new Attachment(); attachment.setMetadata(metadata); attachment.setSpec(spec); + + attachment.setStatus(new Attachment.AttachmentStatus()); + doGetPermalink(attachment).ifPresent(permalink -> { + attachment.getStatus().setPermalink(permalink.toASCIIString()); + }); + var thumbnailLinks = doGetThumbnailLinks(attachment); + var thumbnails = thumbnailLinks.keySet().stream() + .collect(Collectors.toMap( + ThumbnailSize::name, + size -> thumbnailLinks.get(size).toString() + )); + if (!thumbnails.isEmpty()) { + attachment.getStatus().setThumbnails(thumbnails); + } return attachment; }) .onErrorMap(FileAlreadyExistsException.class, @@ -276,20 +291,7 @@ class LocalAttachmentUploadHandler implements AttachmentHandler { if (!this.shouldHandle(policy)) { return Mono.empty(); } - var annotations = attachment.getMetadata().getAnnotations(); - if (annotations == null - || !annotations.containsKey(Constant.URI_ANNO_KEY)) { - return Mono.empty(); - } - var uriStr = annotations.get(Constant.URI_ANNO_KEY); - // the uriStr is encoded before. - uriStr = UriUtils.decode(uriStr, StandardCharsets.UTF_8); - var uri = UriComponentsBuilder.fromUri(externalUrl.get()) - // The URI has been encoded before, so there is no need to encode it again. - .path(uriStr) - .build() - .toUri(); - return Mono.just(uri); + return Mono.justOrEmpty(doGetPermalink(attachment)); } @Override @@ -306,13 +308,33 @@ class LocalAttachmentUploadHandler implements AttachmentHandler { if (!this.shouldHandle(policy)) { return Mono.empty(); } + return Mono.just(doGetThumbnailLinks(attachment)); + } + + private Optional doGetPermalink(Attachment attachment) { + var annotations = attachment.getMetadata().getAnnotations(); + if (annotations == null + || !annotations.containsKey(Constant.URI_ANNO_KEY)) { + return Optional.empty(); + } + var uriStr = annotations.get(Constant.URI_ANNO_KEY); + // the uriStr is encoded before. + uriStr = UriUtils.decode(uriStr, StandardCharsets.UTF_8); + var uri = UriComponentsBuilder.fromUri(externalUrl.get()) + // The URI has been encoded before, so there is no need to encode it again. + .path(uriStr) + .build() + .toUri(); + return Optional.of(uri); + } + + private Map doGetThumbnailLinks(Attachment attachment) { if (attachment.getStatus() == null || !StringUtils.hasText(attachment.getStatus().getPermalink())) { - return Mono.just(Map.of()); + return Map.of(); } var permalinkUri = URI.create(attachment.getStatus().getPermalink()); - var thumbnails = ThumbnailUtils.buildSrcsetMap(permalinkUri); - return Mono.just(thumbnails); + return ThumbnailUtils.buildSrcsetMap(permalinkUri); } private boolean shouldHandle(Policy policy) { diff --git a/application/src/main/java/run/halo/app/core/attachment/reconciler/AttachmentReconciler.java b/application/src/main/java/run/halo/app/core/attachment/reconciler/AttachmentReconciler.java index 62b9723fd..bec50fac8 100644 --- a/application/src/main/java/run/halo/app/core/attachment/reconciler/AttachmentReconciler.java +++ b/application/src/main/java/run/halo/app/core/attachment/reconciler/AttachmentReconciler.java @@ -64,7 +64,7 @@ public class AttachmentReconciler implements Reconciler { var thumbnails = attachmentService.getThumbnailLinks(attachment) .map(map -> map.keySet() .stream() - .collect(Collectors.toMap(Enum::name, k -> map.get(k).toASCIIString())) + .collect(Collectors.toMap(Enum::name, k -> map.get(k).toString())) ) .blockOptional(Duration.ofSeconds(10)) .orElseThrow(() -> new RequeueException(new Result(true, null), """