From 3eae18571e6902ad7d203038ef5cf36db5e986bf Mon Sep 17 00:00:00 2001 From: longjuan <769022681@qq.com> Date: Wed, 27 Sep 2023 16:48:19 +0800 Subject: [PATCH] Complete the function of adding the specified URL suffix to the specified type of file --- .../run/halo/s3os/S3OsAttachmentHandler.java | 27 ++++++++++++-- .../java/run/halo/s3os/S3OsProperties.java | 14 ++++++++ src/main/java/run/halo/s3os/UrlUtils.java | 18 ++++++++++ .../extensions/policy-template-s3os.yaml | 17 +++++++++ src/test/java/run/halo/s3os/UrlUtilsTest.java | 35 +++++++++++++++++++ 5 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/main/java/run/halo/s3os/S3OsAttachmentHandler.java b/src/main/java/run/halo/s3os/S3OsAttachmentHandler.java index f94d755..1e5b955 100644 --- a/src/main/java/run/halo/s3os/S3OsAttachmentHandler.java +++ b/src/main/java/run/halo/s3os/S3OsAttachmentHandler.java @@ -65,6 +65,7 @@ public class S3OsAttachmentHandler implements AttachmentHandler { public static final String OBJECT_KEY = "s3os.plugin.halo.run/object-key"; + public static final String URL_SUFFIX_ANNO_KEY = "s3os.plugin.halo.run/url-suffix"; public static final int MULTIPART_MIN_PART_SIZE = 5 * 1024 * 1024; /** @@ -157,6 +158,10 @@ public Mono getPermalink(Attachment attachment, Policy policy, ConfigMap co } var properties = getProperties(configMap); var objectURL = getObjectURL(properties, objectKey); + var urlSuffix = getUrlSuffixAnnotation(attachment); + if (StringUtils.isNotBlank(urlSuffix)) { + objectURL += urlSuffix; + } return Mono.just(URI.create(objectURL)); } @@ -169,6 +174,15 @@ private String getObjectKey(Attachment attachment) { return annotations.get(OBJECT_KEY); } + @Nullable + private String getUrlSuffixAnnotation(Attachment attachment) { + var annotations = attachment.getMetadata().getAnnotations(); + if (annotations == null) { + return null; + } + return annotations.get(URL_SUFFIX_ANNO_KEY); + } + S3OsProperties getProperties(ConfigMap configMap) { var settingJson = configMap.getData().getOrDefault("default", "{}"); return JsonUtils.jsonToObject(settingJson, S3OsProperties.class); @@ -176,12 +190,19 @@ S3OsProperties getProperties(ConfigMap configMap) { Attachment buildAttachment(S3OsProperties properties, ObjectDetail objectDetail) { String externalLink = getObjectURL(properties, objectDetail.uploadState.objectKey); + var urlSuffix = UrlUtils.findUrlSuffix(properties.getUrlSuffixes(), + objectDetail.uploadState.fileName); var metadata = new Metadata(); metadata.setName(UUID.randomUUID().toString()); - metadata.setAnnotations(new HashMap<>( - Map.of(OBJECT_KEY, objectDetail.uploadState.objectKey, - Constant.EXTERNAL_LINK_ANNO_KEY, externalLink))); + + var annotations = new HashMap<>(Map.of(OBJECT_KEY, objectDetail.uploadState.objectKey)); + if (StringUtils.isNotBlank(urlSuffix)) { + externalLink += urlSuffix; + annotations.put(URL_SUFFIX_ANNO_KEY, urlSuffix); + } + annotations.put(Constant.EXTERNAL_LINK_ANNO_KEY, externalLink); + metadata.setAnnotations(annotations); var objectMetadata = objectDetail.objectMetadata(); var spec = new AttachmentSpec(); diff --git a/src/main/java/run/halo/s3os/S3OsProperties.java b/src/main/java/run/halo/s3os/S3OsProperties.java index 4060247..5df8add 100644 --- a/src/main/java/run/halo/s3os/S3OsProperties.java +++ b/src/main/java/run/halo/s3os/S3OsProperties.java @@ -1,6 +1,10 @@ package run.halo.s3os; +import java.util.List; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; import org.springframework.util.StringUtils; import java.time.LocalDate; @@ -39,6 +43,16 @@ class S3OsProperties { private String region = "Auto"; + private List urlSuffixes; + + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class urlSuffixItem { + private String fileSuffix; + private String urlSuffix; + } + public String getObjectName(String filename) { var objectName = filename; var finalName = FilePathUtils.getFilePathByPlaceholder(getLocation()); diff --git a/src/main/java/run/halo/s3os/UrlUtils.java b/src/main/java/run/halo/s3os/UrlUtils.java index 1c09c4a..a1c502c 100644 --- a/src/main/java/run/halo/s3os/UrlUtils.java +++ b/src/main/java/run/halo/s3os/UrlUtils.java @@ -2,6 +2,7 @@ import java.util.Arrays; import java.util.List; +import org.apache.commons.lang3.StringUtils; public class UrlUtils { private static final List HTTP_PREFIXES = Arrays.asList("http://", "https://"); @@ -17,4 +18,21 @@ public static String removeHttpPrefix(String url) { } return url; } + + public static String findUrlSuffix(List urlSuffixList, + String fileName) { + if (StringUtils.isBlank(fileName)) { + return null; + } + fileName = fileName.toLowerCase(); + for (S3OsProperties.urlSuffixItem item : urlSuffixList) { + String[] fileSuffixes = item.getFileSuffix().split(","); + for (String suffix : fileSuffixes) { + if (fileName.endsWith("." + suffix.trim().toLowerCase())) { + return item.getUrlSuffix(); + } + } + } + return null; + } } diff --git a/src/main/resources/extensions/policy-template-s3os.yaml b/src/main/resources/extensions/policy-template-s3os.yaml index 6140fee..ced586f 100644 --- a/src/main/resources/extensions/policy-template-s3os.yaml +++ b/src/main/resources/extensions/policy-template-s3os.yaml @@ -100,3 +100,20 @@ spec: label: 绑定域名(CDN域名) placeholder: 如不设置,那么将使用 Bucket + EndPoint 作为域名 help: 协议头请在上方设置,此处无需以"http://"或"https://"开头,系统会自动拼接 + - $formkit: repeater + name: urlSuffixes + label: 网址后缀 + help: 用于对指定文件类型的网址添加后缀处理参数,优先级从上到下只取第一个匹配项 + value: [ ] + min: 0 + children: + - $formkit: text + name: fileSuffix + label: 文件后缀 + placeholder: 以半角逗号分隔,例如:jpg,jpeg,png,gif + validation: required + - $formkit: text + name: urlSuffix + label: 网址后缀 + placeholder: 例如:?imageMogr2/format/webp + validation: required \ No newline at end of file diff --git a/src/test/java/run/halo/s3os/UrlUtilsTest.java b/src/test/java/run/halo/s3os/UrlUtilsTest.java index bea9d43..aecb5a0 100644 --- a/src/test/java/run/halo/s3os/UrlUtilsTest.java +++ b/src/test/java/run/halo/s3os/UrlUtilsTest.java @@ -1,5 +1,6 @@ package run.halo.s3os; +import java.util.List; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -12,4 +13,38 @@ void testRemoveHttpPrefix() { assert UrlUtils.removeHttpPrefix("http://www.example.com").equals("www.example.com"); assert UrlUtils.removeHttpPrefix("https://www.example.com").equals("www.example.com"); } + + @Test + public void testFindUrlSuffix() { + List urlSuffixList = List.of( + new S3OsProperties.urlSuffixItem("jpg,png,gif", "?imageMogr2/format/webp"), + new S3OsProperties.urlSuffixItem("pdf", "?123=123"), + new S3OsProperties.urlSuffixItem("jpg", "?456=456") + ); + + // 测试文件名为"example.jpg",期望匹配到"?imageMogr2/format/webp",只匹配第一个后缀 + String fileName1 = "example.jpg"; + String result1 = UrlUtils.findUrlSuffix(urlSuffixList, fileName1); + assertEquals("?imageMogr2/format/webp", result1); + + // 测试文件名为"Document.PDF",期望匹配到"?123=123",不区分大小写 + String fileName2 = "Document.PDF"; + String result2 = UrlUtils.findUrlSuffix(urlSuffixList, fileName2); + assertEquals("?123=123", result2); + + // 测试文件名为"unknown.txt",期望没有匹配项,返回null + String fileName3 = "unknown.txt"; + String result3 = UrlUtils.findUrlSuffix(urlSuffixList, fileName3); + assertNull(result3); + + // 测试无后缀文件名"example",期望没有匹配项,返回null + String fileName4 = "example"; + String result4 = UrlUtils.findUrlSuffix(urlSuffixList, fileName4); + assertNull(result4); + + // 测试空文件名,期望返回null + String fileName5 = ""; + String result5 = UrlUtils.findUrlSuffix(urlSuffixList, fileName5); + assertNull(result5); + } } \ No newline at end of file