mirror of https://github.com/halo-dev/halo
Refactor thumbnail link handling to improve URI generation and streamline attachment processing
parent
8c8ff4c1ea
commit
d24d6d5c07
|
@ -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<Map<ThumbnailSize, URI>> buildThumbnailLinks(URI permalink) {
|
||||
// resolve the thumbnail link
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
interface UploadContext {
|
||||
|
||||
FilePart file();
|
||||
|
|
|
@ -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<URI> 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<ThumbnailSize, URI> 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) {
|
||||
|
|
|
@ -64,7 +64,7 @@ public class AttachmentReconciler implements Reconciler<Request> {
|
|||
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), """
|
||||
|
|
Loading…
Reference in New Issue