Skip to content

Commit

Permalink
fix: import file repeatedly when parent and name same.
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed Oct 31, 2023
1 parent 3e49509 commit d4c67e3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 40 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=run.ikaros.plugin.file
description=A local files import plugin for ikaros.
version=0.11.3
version=0.11.4
101 changes: 63 additions & 38 deletions src/main/java/run/ikaros/plugin/file/LocalFilesImportPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,16 @@ private Mono<Void> handleImportDirFile(File file, Long parentId) {
// dir
return attachmentOperate.findByTypeAndParentIdAndName(
AttachmentType.Directory, parentId, file.getName())
.doOnSuccess(attachment ->
log.debug("{} dir attachment for logic path[{}] "
+ "by find by type[Directory] and parentId[{}]",
Objects.isNull(attachment) ? "NotExists" : "Exists",
Objects.isNull(attachment) ? null : attachment.getPath(), parentId))
.switchIfEmpty(attachmentOperate.createDirectory(parentId, file.getName())
.checkpoint("create directory for parentId=" + parentId
+ " and file=" + file.getAbsolutePath()))
+ " and file=" + file.getAbsolutePath())
.doOnSuccess(attachment ->
log.debug("Create dir attachment[{}].", attachment.getPath())))
.flatMapMany(attachment -> Flux.fromArray(Objects.requireNonNull(file.listFiles()))
.flatMap(file1 -> handleImportDirFile(file1, attachment.getId())
.checkpoint("call handleImportDirFile by "
Expand All @@ -106,51 +113,69 @@ private Mono<Void> handleImportDirFile(File file, Long parentId) {
return Mono.empty();
}

// 创建上传文件目录
String name = file.getName();
String filePostfix = FileUtils.parseFilePostfix(name);
String uploadFilePath
= FileUtils.buildAppUploadFilePath(
ikarosProperties.getWorkDir().toFile().getAbsolutePath(),
filePostfix);
File uploadFile = new File(uploadFilePath);


try {
Files.createLink(uploadFile.toPath(), file.toPath());
log.info("copy file hard link from: {}, to: {}",
file.getAbsolutePath(), uploadFile.getAbsolutePath());
} catch (FileAlreadyExistsException fileAlreadyExistsException) {
log.warn("file already exists for path: [{}].", uploadFile.getAbsolutePath());
return null;
} catch (IOException fileSystemException) {
// 硬链接失败则进行复制
log.warn("file hard links fail from: {}, to: {}",
file.getAbsolutePath(), uploadFile.getAbsolutePath());
// 如果数据库对应目录存在对应附件,也直接返回
return attachmentOperate.findByTypeAndParentIdAndName(AttachmentType.File,
parentId, file.getName())
.map(attachment -> {
log.debug("Skip linkAndSaveAttachment operate for file attachment exists, path[{}].",
attachment.getPath());
return attachment;
})
.switchIfEmpty(linkAndSaveAttachment(file, parentId))
.then();
}
}

private Mono<Attachment> linkAndSaveAttachment(File file, Long parentId) {
// 创建上传文件目录
String name = file.getName();
String filePostfix = FileUtils.parseFilePostfix(name);
String uploadFilePath
= FileUtils.buildAppUploadFilePath(
ikarosProperties.getWorkDir().toFile().getAbsolutePath(),
filePostfix);
File uploadFile = new File(uploadFilePath);

return Mono.just(uploadFile)
.publishOn(Schedulers.boundedElastic())
.map(file1 -> {
log.debug("Attachment not exists, will link and save for parentId[{}] "
+ "and exists import file[{}]", parentId, file.getAbsolutePath());
try {
Files.copy(file.toPath(), uploadFile.toPath());
log.info("copy file from: {}, to: {}",
Files.createLink(uploadFile.toPath(), file.toPath());
log.info("copy file hard link from: {}, to: {}",
file.getAbsolutePath(), uploadFile.getAbsolutePath());
return file1;
} catch (FileAlreadyExistsException fileAlreadyExistsException) {
log.warn("file already exists for path: [{}].", uploadFile.getAbsolutePath());
return file1;
} catch (IOException fileSystemException) {
// 硬链接失败则进行复制
log.warn("file hard links fail from: {}, to: {}",
file.getAbsolutePath(), uploadFile.getAbsolutePath());
} catch (IOException e) {
log.error("skip operate, copy file fail from: {}, to: {}"
, file.getAbsolutePath(), uploadFile.getAbsolutePath()
, e);
throw new RuntimeException(e);
try {
Files.copy(file.toPath(), uploadFile.toPath());
log.info("copy file from: {}, to: {}",
file.getAbsolutePath(), uploadFile.getAbsolutePath());
return file1;
} catch (IOException e) {
log.error("skip operate, copy file fail from: {}, to: {}"
, file.getAbsolutePath(), uploadFile.getAbsolutePath()
, e);
throw new RuntimeException(e);
}
}
}

Attachment attachment = Attachment.builder()
})
.map(f -> Attachment.builder()
.parentId(parentId).name(name).type(AttachmentType.File)
.fsPath(uploadFile.getAbsolutePath()).size(uploadFile.length())
.updateTime(LocalDateTime.now())
.url(uploadFilePath
.replace(ikarosProperties.getWorkDir().toFile().getAbsolutePath(),
"")
.replace("\\", "/")).build();

return Mono.just(attachment)
.flatMap(attachmentOperate::save)
.then();
}
.replace("\\", "/")).build())
.flatMap(f -> attachmentOperate.save(f)
.doOnSuccess(atta -> log.debug("Success link and save single attachment[{}].",
atta.getPath())));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: PluginLocalFilesImport
# plugin entry class that extends BasePlugin
clazz: run.ikaros.plugin.file.LocalFilesImportPlugin
# plugin 'version' is a valid semantic version string (see semver.org).
version: 0.11.3
version: 0.11.4
requires: ">=0.11.1"
author:
name: Ikaros OSS Team
Expand Down

0 comments on commit d4c67e3

Please sign in to comment.