Skip to content

Commit

Permalink
fix: Fixed an issue with images not loading properly when using rawUrl (
Browse files Browse the repository at this point in the history
#33)

* fix: 修复使用rawUrl导致的图片无法正常加载的问题

* chore: 更换过时api

* chore: 更新版本号

* feat: 处理挂载路径为/时路径出现//
  • Loading branch information
Roozenlz authored Dec 9, 2024
1 parent ef72e49 commit 9c3565f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.0.1
version=1.1.2
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Mono<AListGetCurrentUserInfoRes> 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)
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 getMeUri = fromUriString(
properties.getSite().toString())
.path("/api/me")
.toUriString();

return webClient.get()
.uri(getMeUri)
.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(),
"/".equals(properties.getPath())? "" : 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
4 changes: 2 additions & 2 deletions src/main/resources/extensions/policy-template-alist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 站点用户名和密码
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9c3565f

Please sign in to comment.