Skip to content

Commit

Permalink
fix: 修复使用rawUrl导致的图片无法正常加载的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Roozenlz committed Nov 30, 2024
1 parent ef72e49 commit 7595578
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -31,7 +31,7 @@ tasks.withType(JavaCompile).configureEach {
}

halo {
version = '2.18'
version = '2.20'
superAdminUsername = 'admin'
superAdminPassword = 'admin'
externalUrl = 'http://localhost:8090'
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/run/halo/alist/endpoint/AListAttachmentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -84,7 +85,8 @@ public Mono<Attachment> 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();
Expand Down Expand Up @@ -164,7 +166,7 @@ public Mono<String> 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();
Expand Down Expand Up @@ -196,7 +198,7 @@ public Mono<Attachment> 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())
Expand Down Expand Up @@ -260,9 +262,36 @@ public Mono<URI> 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<AListResult<AListGetCurrentUserInfoRes>>() {
})
.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());
});
});
}

Expand All @@ -273,7 +302,7 @@ private Mono<AListGetFileInfoRes> 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)
Expand Down

0 comments on commit 7595578

Please sign in to comment.