From 9c3565f85d70f54577112d1e3237ac0fdab63f45 Mon Sep 17 00:00:00 2001 From: Roozen <93673944+Roozenlz@users.noreply.github.com> Date: Mon, 9 Dec 2024 21:33:22 +0800 Subject: [PATCH] fix: Fixed an issue with images not loading properly when using rawUrl (#33) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复使用rawUrl导致的图片无法正常加载的问题 * chore: 更换过时api * chore: 更新版本号 * feat: 处理挂载路径为/时路径出现// --- build.gradle | 4 +- gradle.properties | 2 +- .../PolicyConfigValidationController.java | 2 +- .../endpoint/AListAttachmentHandler.java | 45 +++++++++++++++---- .../extensions/policy-template-alist.yaml | 4 +- src/main/resources/plugin.yaml | 2 +- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 134ef4d..01d1939 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ repositories { } dependencies { - implementation platform('run.halo.tools.platform:plugin:2.17.0-SNAPSHOT') + implementation platform('run.halo.tools.platform:plugin:2.20.8-SNAPSHOT') compileOnly 'run.halo.app:api' testImplementation 'run.halo.app:api' @@ -31,7 +31,7 @@ tasks.withType(JavaCompile).configureEach { } halo { - version = '2.18' + version = '2.20' superAdminUsername = 'admin' superAdminPassword = 'admin' externalUrl = 'http://localhost:8090' diff --git a/gradle.properties b/gradle.properties index 47a478a..187fffb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=1.0.1 +version=1.1.2 diff --git a/src/main/java/run/halo/alist/controller/PolicyConfigValidationController.java b/src/main/java/run/halo/alist/controller/PolicyConfigValidationController.java index 94f9094..c82b5e9 100644 --- a/src/main/java/run/halo/alist/controller/PolicyConfigValidationController.java +++ b/src/main/java/run/halo/alist/controller/PolicyConfigValidationController.java @@ -42,7 +42,7 @@ public Mono validatePolicyConfig( // check if the current user has permissions return handler.getToken(properties) .flatMap(token -> { - var getMeUri = UriComponentsBuilder.fromHttpUrl(properties.getSite().toString()) + var getMeUri = UriComponentsBuilder.fromUriString(properties.getSite().toString()) .path("/api/me") .toUriString(); return handler.getWebClient().get().uri(getMeUri) diff --git a/src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java b/src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java index 03a7877..c92bb12 100644 --- a/src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java +++ b/src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java @@ -2,7 +2,7 @@ import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import static org.springframework.http.HttpStatus.OK; -import static org.springframework.web.util.UriComponentsBuilder.fromHttpUrl; +import static org.springframework.web.util.UriComponentsBuilder.fromUriString; import io.netty.channel.ChannelOption; import java.net.URI; @@ -31,6 +31,7 @@ import run.halo.alist.dto.request.AListGetFileInfoReq; import run.halo.alist.dto.request.AListLoginReq; import run.halo.alist.dto.request.AListRemoveFileReq; +import run.halo.alist.dto.response.AListGetCurrentUserInfoRes; import run.halo.alist.dto.response.AListGetFileInfoRes; import run.halo.alist.dto.response.AListLoginRes; import run.halo.app.core.extension.attachment.Attachment; @@ -84,7 +85,8 @@ public Mono upload(UploadContext uploadContext) { return Mono.zip(sizeOfContent, getToken(properties)).flatMap(tuple2 -> { var contentLength = tuple2.getT1(); var token = tuple2.getT2(); - var uploadUri = fromHttpUrl(properties.getSite().toString()) + var uploadUri = fromUriString( + properties.getSite().toString()) .path("/api/fs/put") .build() .toUri(); @@ -164,7 +166,7 @@ public Mono getToken(AListProperties properties) { }) .flatMap(loginBody -> { var site = properties.getSite(); - var loginUri = fromHttpUrl(site.toString()) + var loginUri = fromUriString(site.toString()) .path("/api/auth/login") .build() .toUri(); @@ -196,7 +198,7 @@ public Mono delete(DeleteContext deleteContext) { .flatMap(context -> { var properties = getProperties(context.configMap()); var attachment = context.attachment(); - var deleteUri = fromHttpUrl(properties.getSite().toString()) + var deleteUri = fromUriString(properties.getSite().toString()) .path("/api/fs/remove") .toUriString(); var rawFilePath = Optional.ofNullable(attachment.getMetadata().getAnnotations()) @@ -260,9 +262,36 @@ public Mono getPermalink(Attachment attachment, Policy policy, ConfigMap co .toString() ); return getToken(properties) - .flatMap(token -> getFile(token, rawFilePath, properties, false)) - .map(AListGetFileInfoRes::getRawUrl) - .map(URI::create); + .flatMap(token -> Mono.zip(Mono.just(token), + getFile(token, rawFilePath, properties, false))) + .flatMap(tuple2 -> { + var token = tuple2.getT1(); + var fileInfo = tuple2.getT2(); + var getMeUri = fromUriString( + properties.getSite().toString()) + .path("/api/me") + .toUriString(); + + return webClient.get() + .uri(getMeUri) + .header(HttpHeaders.AUTHORIZATION, token) + .retrieve() + .bodyToMono( + new ParameterizedTypeReference>() { + }) + .map(userInfoRes -> fromUriString( + properties.getSite().toString()) + .path("/d{basePath}{path}/{name}") + .queryParamIfPresent("sign", + Optional.ofNullable(fileInfo.getSign()) + .filter(s -> !s.isEmpty())) + .buildAndExpand( + userInfoRes.getData().getBasePath(), + "/".equals(properties.getPath())? "" : properties.getPath(), + fileInfo.getName() + ) + .toUri()); + }); }); } @@ -273,7 +302,7 @@ private Mono getFile(String token, var body = AListGetFileInfoReq.builder() .path(filePath) .build(); - var fsGetUri = fromHttpUrl(properties.getSite().toString()) + var fsGetUri = fromUriString(properties.getSite().toString()) .path("/api/fs/get") .toUriString(); return webClient.post().uri(fsGetUri) diff --git a/src/main/resources/extensions/policy-template-alist.yaml b/src/main/resources/extensions/policy-template-alist.yaml index bfd8947..feab7e5 100644 --- a/src/main/resources/extensions/policy-template-alist.yaml +++ b/src/main/resources/extensions/policy-template-alist.yaml @@ -26,9 +26,9 @@ spec: - $formkit: text name: path label: 挂载路径 - help: 所填用户基本路径(可在 AList 管理 -> 用户 查看)下文件夹的路径,必须以 / 开头,支持多级目录如 /picture/2024,则全路径为{基本路径}/picture/2024,上传文件时会自动创建不存在的目录,为 / 时留空即可 + help: 所填用户基本路径(可在 AList 管理 -> 用户 查看)下文件夹的路径,必须以 / 开头,支持多级目录如 /picture/2024,则全路径为{基本路径}/picture/2024,上传文件时会自动创建不存在的目录,留空表示基本路径根目录 / - $formkit: secret name: secretName required: true label: secretName - help: 需要创建一个包含 key 为 username、password 的密钥,分别填入你的 AList 站点用户名和密码,修改配置后,需要重新验证来刷新缓存 + help: 需要创建一个包含 key 为 username、password 的密钥,分别填入你的 AList 站点用户名和密码 diff --git a/src/main/resources/plugin.yaml b/src/main/resources/plugin.yaml index 54b2563..941e4a2 100644 --- a/src/main/resources/plugin.yaml +++ b/src/main/resources/plugin.yaml @@ -6,7 +6,7 @@ metadata: "store.halo.run/app-id": "app-wEGMV" spec: enabled: true - requires: ">=2.17.0" + requires: ">=2.20.0" author: name: Halo website: https://github.com/halo-dev