refactor(knowledge-item): 优化知识项预览文件路径处理逻辑

- 将文件路径验证逻辑从方法开始位置移动到实际使用位置
- 修复了预览文件名获取方式,直接从相对路径解析文件名
- 确保文件存在性检查只在需要时执行
- 提高了代码可读性和执行效率
This commit is contained in:
2026-02-01 21:00:07 +08:00
parent 2430db290d
commit 00c41fbbd3

View File

@@ -366,12 +366,9 @@ public class KnowledgeItemApplicationService {
String relativePath = knowledgeItem.getContent();
BusinessAssert.isTrue(StringUtils.isNotBlank(relativePath), CommonErrorCode.PARAM_ERROR);
Path filePath = resolveKnowledgeItemStoragePath(relativePath);
BusinessAssert.isTrue(Files.exists(filePath) && Files.isRegularFile(filePath), CommonErrorCode.PARAM_ERROR);
String previewName = StringUtils.isNotBlank(knowledgeItem.getSourceFileId())
? knowledgeItem.getSourceFileId()
: filePath.getFileName().toString();
: Paths.get(relativePath).getFileName().toString();
if (knowledgeItemPreviewService.isOfficeDocument(previewName)) {
KnowledgeItemPreviewService.PreviewFile previewFile = knowledgeItemPreviewService.resolveReadyPreviewFile(setId, knowledgeItem);
@@ -393,6 +390,9 @@ public class KnowledgeItemApplicationService {
return;
}
Path filePath = resolveKnowledgeItemStoragePath(relativePath);
BusinessAssert.isTrue(Files.exists(filePath) && Files.isRegularFile(filePath), CommonErrorCode.PARAM_ERROR);
String contentType = null;
try {
contentType = Files.probeContentType(filePath);