Skip to content

Commit

Permalink
optimize: add refresh token when post alist 401
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed May 31, 2024
1 parent c9d329b commit a450b70
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/run/ikaros/plugin/alist/AListClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ private Mono<Void> createAttachmentRecursively(List<String> paths, Long parentId
@Retryable
public AListAttachment[] fetchAttachments(List<String> paths) {
Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("path", getPathByPathArr(paths));
String path = getPathByPathArr(paths);
bodyMap.put("path", path);
String body = JsonUtils.obj2Json(bodyMap);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);
Expand All @@ -117,7 +118,10 @@ public AListAttachment[] fetchAttachments(List<String> paths) {
return JsonUtils.obj2Arr(content, new TypeReference<AListAttachment[]>() {
});
} else {
log.error("post get alist attachments for apiResult: {}", apiResult);
log.error("post get alist attachments for path: {} and apiResult: {}", path, apiResult);
if (Objects.nonNull(apiResult) && apiResult.getCode() == 401) {
updateOperateByToken().subscribe();
}
}

return new AListAttachment[0];
Expand Down Expand Up @@ -166,8 +170,12 @@ public interface API {
String FS_GET = "/api/fs/get";
}

@Retryable
public Mono<AListToken> refreshToken() {
return refreshToken(false);
}

@Retryable
public Mono<AListToken> refreshToken(boolean refresh) {
if (StringUtils.isBlank(token.getUrl())
|| StringUtils.isBlank(token.getUsername())
|| StringUtils.isBlank(token.getPassword())) {
Expand All @@ -176,7 +184,7 @@ public Mono<AListToken> refreshToken() {
return Mono.empty();
}

if (StringUtils.isBlank(token.getToken())) {
if (StringUtils.isBlank(token.getToken()) || refresh) {
Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("username", token.getUsername());
bodyMap.put("password", token.getPassword());
Expand Down Expand Up @@ -217,7 +225,11 @@ private String getPathByPathArr(List<String> paths) {
for (String p: paths) {
path.append(p).append("/");
}
return path.toString();
String pStr = path.toString();
if (pStr.endsWith("/")) {
pStr = pStr.substring(0, pStr.length() - 1);
}
return pStr;
}

@Retryable
Expand Down

0 comments on commit a450b70

Please sign in to comment.