Skip to content

Commit

Permalink
优化导入路径
Browse files Browse the repository at this point in the history
  • Loading branch information
chivehao committed Sep 16, 2024
1 parent 7981532 commit dcc5fd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# 15.2.0


- 优化导入路径

# v0.12.11

Expand Down
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 @@ -62,7 +62,7 @@ public Mono<Void> doImportFilesFromAListPath(List<String> paths) {
// 获取导入的父目录ID,不存在则创建
.switchIfEmpty(attachmentOperate.createDirectory(AttachmentConst.ROOT_DIRECTORY_ID, AListConst.Attachment.DEFAULT_PARENT_NAME))
.map(Attachment::getId)
.flatMap(rootParentId -> mkdirByLastPath(paths, rootParentId))
.flatMap(rootParentId -> mkdirByLastPath(new ArrayList<>(paths), rootParentId))
.flatMap(parentId -> createAttachmentRecursively(paths, parentId));
}

Expand Down Expand Up @@ -124,6 +124,7 @@ public AListAttachment[] fetchAttachments(List<String> paths) {
ApiResult apiResult = responseEntity.getBody();
if (apiResult != null && apiResult.getCode() == 200) {
Object content = apiResult.getData().get("content");
if (content == null) return new AListAttachment[0];
return JsonUtils.obj2Arr(content, new TypeReference<AListAttachment[]>() {
});
} else {
Expand All @@ -136,16 +137,27 @@ public AListAttachment[] fetchAttachments(List<String> paths) {
return new AListAttachment[0];
}

private Mono<Long> mkdirByLastPath(List<String> paths, Long parentId) {
String dirName = paths.get(paths.size() - 1);
private Mono<Attachment> mkdirWithNameAndParentId(String dirName, Long parentId) {
if (dirName.startsWith("/")) dirName = dirName.substring(1);
if (dirName.endsWith("/")) dirName = dirName.substring(0, dirName.length() - 1);
return attachmentOperate.findByTypeAndParentIdAndName(AttachmentType.Directory, parentId, dirName)
.switchIfEmpty(attachmentOperate.createDirectory(parentId, dirName))
.flatMap(attachment -> attachmentOperate.findByTypeAndParentIdAndName(
attachment.getType(), attachment.getParentId(), attachment.getName()
))
.map(Attachment::getId);
));
}

private Mono<Long> mkdirByLastPath(List<String> paths, Long parentId) {
final int size = paths.size();
String name = paths.remove(0);
if (size > 1) {
return mkdirWithNameAndParentId(name, parentId)
.map(Attachment::getId)
.flatMap(id -> mkdirByLastPath(paths, id));
} else {
return mkdirWithNameAndParentId(name, parentId)
.map(Attachment::getId);
}
}

private Mono<AListAttachment> saveAListAttachment(AListAttachment aListAttachment) {
Expand Down

0 comments on commit dcc5fd0

Please sign in to comment.