Skip to content

Commit 096c725

Browse files
committed
feat: add handling for missing source files in document processing
--bug=1062915 --user=刘瑞斌 【知识库】快速创建的文档,替换原文档报错,找不到源文件 https://www.tapd.cn/62980211/s/1789099
1 parent a584378 commit 096c725

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

apps/knowledge/serializers/document.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,23 +1538,30 @@ def replace(self):
15381538
source_file = QuerySet(File).filter(source_id=self.data.get('document_id')).first()
15391539

15401540
if not source_file:
1541-
raise AppApiException(500, _('Source file not found'))
1542-
1543-
# 获取原文件的sha256_hash
1544-
original_hash = source_file.sha256_hash
1541+
# 不存在手动关联一个文档
1542+
new_source_file = File(
1543+
id=uuid.uuid7(),
1544+
file_name=file.name,
1545+
source_type=FileSourceType.DOCUMENT,
1546+
source_id=self.data.get('document_id'),
1547+
)
1548+
new_source_file.save(file.read())
1549+
else:
1550+
# 获取原文件的sha256_hash
1551+
original_hash = source_file.sha256_hash
15451552

1546-
# 读取新文件内容
1547-
file_content = file.read()
1553+
# 读取新文件内容
1554+
file_content = file.read()
15481555

1549-
# 查找所有具有相同sha256_hash的文件
1550-
files_to_update = QuerySet(File).filter(
1551-
sha256_hash=original_hash,
1552-
source_id__in=[self.data.get('knowledge_id'), self.data.get('document_id')]
1553-
)
1556+
# 查找所有具有相同sha256_hash的文件
1557+
files_to_update = QuerySet(File).filter(
1558+
sha256_hash=original_hash,
1559+
source_id__in=[self.data.get('knowledge_id'), self.data.get('document_id')]
1560+
)
15541561

1555-
# 更新所有相同hash的文件
1556-
for file_obj in files_to_update:
1557-
file_obj.save(file_content)
1562+
# 更新所有相同hash的文件
1563+
for file_obj in files_to_update:
1564+
file_obj.save(file_content)
15581565

15591566
return True
15601567

0 commit comments

Comments
 (0)