From 7595578f5c2054fced6c10dec3bd5563a5989780 Mon Sep 17 00:00:00 2001 From: Roozen <2273038475@qq.com> Date: Sun, 1 Dec 2024 02:12:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8rawU?= =?UTF-8?q?rl=E5=AF=BC=E8=87=B4=E7=9A=84=E5=9B=BE=E7=89=87=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=8A=A0=E8=BD=BD=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 +- .../endpoint/AListAttachmentHandler.java | 45 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 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/src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java b/src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java index 03a7877..217d2b5 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 meUri = fromUriString( + properties.getSite().toString()) + .path("/api/me") + .toUriString(); + + return webClient.get() + .uri(meUri) + .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(), + 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)