Skip to content

Commit

Permalink
optimize: token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed May 31, 2024
1 parent a450b70 commit 7aa15f4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/java/run/ikaros/plugin/alist/AListClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public Mono<Void> doImportFilesFromAListPath(List<String> paths) {
return Mono.error(new IllegalArgumentException("paths is null or empty"));
}
return Mono.justOrEmpty(token)
.filter(Objects::nonNull)
.filter(token -> StringUtils.isNotBlank(token.getToken()))
.switchIfEmpty(updateOperateByToken())
.flatMap(token -> attachmentOperate.findByTypeAndParentIdAndName(AttachmentType.Directory, AttachmentConst.ROOT_DIRECTORY_ID,
AListConst.Attachment.DEFAULT_PARENT_NAME))
Expand Down Expand Up @@ -119,8 +121,8 @@ public AListAttachment[] fetchAttachments(List<String> paths) {
});
} else {
log.error("post get alist attachments for path: {} and apiResult: {}", path, apiResult);
if (Objects.nonNull(apiResult) && apiResult.getCode() == 401) {
updateOperateByToken().subscribe();
if (Objects.nonNull(apiResult) && 401 == apiResult.getCode()) {
token = null;
}
}

Expand Down Expand Up @@ -204,6 +206,10 @@ public Mono<AListToken> refreshToken(boolean refresh) {
.then(Mono.just(token));
} else {
log.error("post token to alist for apiResult: {}", apiResult);
if (Objects.nonNull(apiResult) && 401 == apiResult.getCode()) {
token = null;
}
return Mono.empty();
}
}
if (StringUtils.isNotBlank(token.getToken())) {
Expand Down Expand Up @@ -264,6 +270,9 @@ private AListAttachment fetchAttachmentDetail(String path, AListAttachment attac
return aListAttachment;
} else {
log.error("post alist attachment details for apiResult: {}", apiResult);
if (Objects.nonNull(apiResult) && 401 == apiResult.getCode()) {
token = null;
}
}
return attachment;
}
Expand Down Expand Up @@ -325,7 +334,7 @@ public void startRefreshTokenTask() {
}

public Mono<AListToken> updateOperateByToken() {
return getToken().flatMap(aListToken -> refreshToken());
return getToken().flatMap(aListToken -> refreshToken(true));
}

@EventListener(ApplicationReadyEvent.class)
Expand Down

0 comments on commit 7aa15f4

Please sign in to comment.