From df11ff2431b95415fb752fbbc85e2dbe784b344e Mon Sep 17 00:00:00 2001 From: guqing Date: Wed, 4 Sep 2024 14:35:44 +0800 Subject: [PATCH] refactor: setting options definition for thumbnail param pattern --- build.gradle | 2 +- .../s3os/AttachmentThumbnailReconciler.java | 74 ------------------- .../java/run/halo/s3os/S3OsProperties.java | 8 +- .../run/halo/s3os/S3ThumbnailProvider.java | 12 +-- .../extensions/policy-template-s3os.yaml | 65 +++++++--------- 5 files changed, 34 insertions(+), 127 deletions(-) delete mode 100644 src/main/java/run/halo/s3os/AttachmentThumbnailReconciler.java diff --git a/build.gradle b/build.gradle index af92100..755c372 100644 --- a/build.gradle +++ b/build.gradle @@ -40,7 +40,7 @@ configurations.runtimeClasspath { halo { - version = '2.17.0' + version = '2.19' } haloPlugin { diff --git a/src/main/java/run/halo/s3os/AttachmentThumbnailReconciler.java b/src/main/java/run/halo/s3os/AttachmentThumbnailReconciler.java deleted file mode 100644 index d662174..0000000 --- a/src/main/java/run/halo/s3os/AttachmentThumbnailReconciler.java +++ /dev/null @@ -1,74 +0,0 @@ -package run.halo.s3os; - -import static run.halo.app.infra.FileCategoryMatcher.IMAGE; - -import java.time.Duration; -import java.time.Instant; -import lombok.RequiredArgsConstructor; -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Component; -import org.springframework.util.Assert; -import run.halo.app.core.extension.attachment.Attachment; -import run.halo.app.extension.ExtensionClient; -import run.halo.app.extension.MetadataUtil; -import run.halo.app.extension.controller.Controller; -import run.halo.app.extension.controller.ControllerBuilder; -import run.halo.app.extension.controller.Reconciler; - -/** - *

This {@link Reconciler} used to check thumbnail status are generated - * if not, update the attachment to trigger thumbnail generation by halo

- */ -@Component -@RequiredArgsConstructor -public class AttachmentThumbnailReconciler implements Reconciler { - static final String REQUEST_GEN_THUMBNAIL = "s3os.halo.run/request-gen-thumbnail"; - private final ExtensionClient client; - - @Override - public Result reconcile(Request request) { - client.fetch(Attachment.class, request.name()) - .ifPresent(attachment -> { - var annotations = MetadataUtil.nullSafeAnnotations(attachment); - var requestTime = annotations.get(REQUEST_GEN_THUMBNAIL); - if (isMadeWithIn1Day(requestTime)) { - // skip if request is made within 1 day - return; - } - - if (!isImage(attachment)) { - // skip non-image attachments - return; - } - - var status = attachment.getStatus(); - if (status == null || status.getThumbnails() == null) { - // update to trigger attachment thumbnail generation - annotations.put(REQUEST_GEN_THUMBNAIL, Instant.now().toString()); - client.update(attachment); - } - }); - return Result.doNotRetry(); - } - - static boolean isMadeWithIn1Day(String requestTimeStr) { - if (StringUtils.isBlank(requestTimeStr)) { - return false; - } - var requestTime = Instant.parse(requestTimeStr); - return Duration.between(requestTime, Instant.now()).minusDays(1).isNegative(); - } - - public static boolean isImage(Attachment attachment) { - Assert.notNull(attachment, "Attachment must not be null"); - var mediaType = attachment.getSpec().getMediaType(); - return mediaType != null && IMAGE.match(mediaType); - } - - @Override - public Controller setupWith(ControllerBuilder builder) { - return builder - .extension(new Attachment()) - .build(); - } -} diff --git a/src/main/java/run/halo/s3os/S3OsProperties.java b/src/main/java/run/halo/s3os/S3OsProperties.java index b87c7da..3d10e4a 100644 --- a/src/main/java/run/halo/s3os/S3OsProperties.java +++ b/src/main/java/run/halo/s3os/S3OsProperties.java @@ -50,13 +50,7 @@ public class S3OsProperties { private List urlSuffixes; - private ThumbnailParam thumbnailParam; - - public record ThumbnailParam(String type, String pattern) { - public boolean hasPattern() { - return StringUtils.hasText(type) && StringUtils.hasText(pattern); - } - } + private String thumbnailParamPattern; @Data @AllArgsConstructor diff --git a/src/main/java/run/halo/s3os/S3ThumbnailProvider.java b/src/main/java/run/halo/s3os/S3ThumbnailProvider.java index 0e6ac8c..c0dec7c 100644 --- a/src/main/java/run/halo/s3os/S3ThumbnailProvider.java +++ b/src/main/java/run/halo/s3os/S3ThumbnailProvider.java @@ -43,8 +43,10 @@ public Mono generate(ThumbnailContext thumbnailContext) { .mapNotNull(cacheValue -> placedPattern(cacheValue.pattern(), size)) .map(param -> { if (param.startsWith("?")) { - UriComponentsBuilder.fromHttpUrl(url) - .queryParam(param.substring(1)); + return UriComponentsBuilder.fromHttpUrl(url) + .queryParam(param.substring(1)) + .build() + .toString(); } return url + param; }) @@ -91,13 +93,13 @@ private Flux> listAllS3ObjectDomain() { var s3ConfigMapName = s3Policy.getSpec().getConfigMapName(); return fetchS3PropsByConfigMapName(s3ConfigMapName) .mapNotNull(properties -> { - var thumbnailParam = properties.getThumbnailParam(); - if (thumbnailParam == null || !thumbnailParam.hasPattern()) { + var thumbnailParam = properties.getThumbnailParamPattern(); + if (StringUtils.isBlank(thumbnailParam)) { return null; } var objectDomain = properties.toObjectURL(""); var cacheValue = S3PropsCacheValue.builder() - .pattern(thumbnailParam.pattern()) + .pattern(thumbnailParam) .configMapName(s3ConfigMapName) .build(); return Map.entry(objectDomain, cacheValue); diff --git a/src/main/resources/extensions/policy-template-s3os.yaml b/src/main/resources/extensions/policy-template-s3os.yaml index f44e4c5..437ed34 100644 --- a/src/main/resources/extensions/policy-template-s3os.yaml +++ b/src/main/resources/extensions/policy-template-s3os.yaml @@ -145,44 +145,29 @@ spec: label: 网址后缀 placeholder: 例如:?imageMogr2/format/webp validation: required - - $formkit: group + - $formkit: select + name: thumbnailParamPattern label: 缩略图参数 - name: thumbnailParam - children: - - $formkit: select - name: type - key: type - label: 类型 - options: - - label: 无 - value: "" - - label: 预设参数 - value: preset - - label: 自定义参数 - value: custom - - $formkit: select - if: "$value.type == preset" - name: pattern - key: type - label: 使用预设参数 - help: 请根据您的对象存储服务商选择对应的缩略图参数 - options: - - label: 腾讯云 COS - value: "?imageView2/0/w/{width}" - - label: 七牛云 KODO - value: "?imageView2/0/w/{width}" - - label: 阿里云 OSS - value: "?x-oss-process=image/resize,w_{width},m_lfit" - - label: 百度云 BOS - value: "?x-bce-process=image/resize,m_lfit,w_{width}" - - label: 青云 OSS - value: "?image&action=resize:w_{width},m_2" - - label: 京东云 - value: "?x-oss-process=img/sw/{width}" - - label: 又拍云 - value: "!/fw/{width}" - - $formkit: text - if: "$value.type == custom" - label: 自定义参数 - help: "{width} 为宽度占位符将被替换为所需缩略图宽度值,如: 400,参数需要以 ? 开头,间隔符除外" - name: pattern + allowCreate: true + searchable: true + value: "" + help: | + 请根据您的对象存储服务商选择对应的缩略图参数或自定义参数,{width} 为宽度占位符将被替换为所需缩略图宽度值, + 如: 400,参数需要以 ? 开头,间隔符除外 + options: + - label: 无 + value: "" + - label: 腾讯云 COS + value: "?imageView2/0/w/{width}" + - label: 七牛云 KODO + value: "?imageView2/0/w/{width}" + - label: 阿里云 OSS + value: "?x-oss-process=image/resize,w_{width},m_lfit" + - label: 百度云 BOS + value: "?x-bce-process=image/resize,m_lfit,w_{width}" + - label: 青云 OSS + value: "?image&action=resize:w_{width},m_2" + - label: 京东云 + value: "?x-oss-process=img/sw/{width}" + - label: 又拍云 + value: "!/fw/{width}"