Skip to content

Commit dcc5fd0

Browse files
committed
优化导入路径
1 parent 7981532 commit dcc5fd0

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

CHANGELOG.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# 15.2.0
66

7-
7+
- 优化导入路径
88

99
# v0.12.11
1010

src/main/java/run/ikaros/plugin/alist/AListClient.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Mono<Void> doImportFilesFromAListPath(List<String> paths) {
6262
// 获取导入的父目录ID,不存在则创建
6363
.switchIfEmpty(attachmentOperate.createDirectory(AttachmentConst.ROOT_DIRECTORY_ID, AListConst.Attachment.DEFAULT_PARENT_NAME))
6464
.map(Attachment::getId)
65-
.flatMap(rootParentId -> mkdirByLastPath(paths, rootParentId))
65+
.flatMap(rootParentId -> mkdirByLastPath(new ArrayList<>(paths), rootParentId))
6666
.flatMap(parentId -> createAttachmentRecursively(paths, parentId));
6767
}
6868

@@ -124,6 +124,7 @@ public AListAttachment[] fetchAttachments(List<String> paths) {
124124
ApiResult apiResult = responseEntity.getBody();
125125
if (apiResult != null && apiResult.getCode() == 200) {
126126
Object content = apiResult.getData().get("content");
127+
if (content == null) return new AListAttachment[0];
127128
return JsonUtils.obj2Arr(content, new TypeReference<AListAttachment[]>() {
128129
});
129130
} else {
@@ -136,16 +137,27 @@ public AListAttachment[] fetchAttachments(List<String> paths) {
136137
return new AListAttachment[0];
137138
}
138139

139-
private Mono<Long> mkdirByLastPath(List<String> paths, Long parentId) {
140-
String dirName = paths.get(paths.size() - 1);
140+
private Mono<Attachment> mkdirWithNameAndParentId(String dirName, Long parentId) {
141141
if (dirName.startsWith("/")) dirName = dirName.substring(1);
142142
if (dirName.endsWith("/")) dirName = dirName.substring(0, dirName.length() - 1);
143143
return attachmentOperate.findByTypeAndParentIdAndName(AttachmentType.Directory, parentId, dirName)
144144
.switchIfEmpty(attachmentOperate.createDirectory(parentId, dirName))
145145
.flatMap(attachment -> attachmentOperate.findByTypeAndParentIdAndName(
146146
attachment.getType(), attachment.getParentId(), attachment.getName()
147-
))
148-
.map(Attachment::getId);
147+
));
148+
}
149+
150+
private Mono<Long> mkdirByLastPath(List<String> paths, Long parentId) {
151+
final int size = paths.size();
152+
String name = paths.remove(0);
153+
if (size > 1) {
154+
return mkdirWithNameAndParentId(name, parentId)
155+
.map(Attachment::getId)
156+
.flatMap(id -> mkdirByLastPath(paths, id));
157+
} else {
158+
return mkdirWithNameAndParentId(name, parentId)
159+
.map(Attachment::getId);
160+
}
149161
}
150162

151163
private Mono<AListAttachment> saveAListAttachment(AListAttachment aListAttachment) {

0 commit comments

Comments
 (0)