Skip to content

Commit 147eea4

Browse files
committed
feat: implement bulk update for files with matching SHA256 hash
1 parent c0ae784 commit 147eea4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

apps/knowledge/serializers/document.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,7 @@ def is_valid(self, *, raise_exception=False):
15311531
).exists():
15321532
raise AppApiException(500, _('Document id does not exist'))
15331533

1534+
@transaction.atomic
15341535
def replace(self):
15351536
self.is_valid(raise_exception=True)
15361537
file = self.data.get('file')
@@ -1539,7 +1540,20 @@ def replace(self):
15391540
if not source_file:
15401541
raise AppApiException(500, _('Source file not found'))
15411542

1542-
source_file.save(file.read())
1543+
# 获取原文件的sha256_hash
1544+
original_hash = source_file.sha256_hash
1545+
1546+
# 读取新文件内容
1547+
file_content = file.read()
1548+
1549+
# 查找所有具有相同sha256_hash的文件
1550+
files_to_update = QuerySet(File).filter(
1551+
Q(sha256_hash=original_hash) & Q(source_id=self.data.get('knowledge_id'))
1552+
)
1553+
1554+
# 更新所有相同hash的文件
1555+
for file_obj in files_to_update:
1556+
file_obj.save(file_content)
15431557

15441558
return True
15451559

0 commit comments

Comments
 (0)