Go: only unlink file-manager files when deleting documents from a knowledge base (#17175)

This commit is contained in:
euvre
2026-07-22 12:14:22 +08:00
committed by GitHub
parent 4697ee29aa
commit 8408f9a4e2

View File

@@ -392,7 +392,9 @@ func (s *DocumentService) rollbackAddFileFromKBError(doc *entity.Document, kbID
// cleanupFileReferences deletes file2document mappings for docID, and for each
// referenced file, only hard-deletes the file record and its storage blob when
// no other document still references the same file_id.
// the file is a knowledgebase-owned upload (source_type == knowledgebase) and
// no other document still references the same file_id. Files linked from file
// management are only unlinked — the file record and blob stay intact.
func (s *DocumentService) cleanupFileReferences(docID string) {
mappings, mapErr := s.file2DocumentDAO.GetByDocumentID(docID)
if mapErr != nil {
@@ -418,7 +420,8 @@ func (s *DocumentService) cleanupFileReferences(docID string) {
common.Logger.Warn(fmt.Sprintf("cleanupFileReferences: failed to delete f2d for %s: %v", docID, delErr))
}
// For each file, only delete the record and blob when no other doc references it
// For each file, only delete the record and blob when it is a
// knowledgebase-owned upload and no other doc references it
for _, fileID := range fileIDs {
remaining, remErr := s.file2DocumentDAO.GetByFileID(fileID)
if remErr != nil {
@@ -435,6 +438,9 @@ func (s *DocumentService) cleanupFileReferences(docID string) {
common.Logger.Warn(fmt.Sprintf("cleanupFileReferences: file not found %s: %v", fileID, fErr))
continue
}
if entity.FileSource(file.SourceType) != entity.FileSourceKnowledgebase {
continue // linked from file management — unlink only, keep the file
}
if _, delErr := fileDAO.DeleteByIDs([]string{fileID}); delErr != nil {
common.Logger.Warn(fmt.Sprintf("cleanupFileReferences: failed to delete file %s: %v", fileID, delErr))
continue // keep the blob so the live file row still has its object