fix(knowledge): delete extracted images from storage when knowledge is removed

Previously, only the original uploaded file was deleted via knowledge.FilePath.
Images extracted by the builtin parser (stored as individual S3/MinIO/local files
via ImageResolver) were never cleaned up, causing orphan files to accumulate.

- Add ListImageInfoByKnowledgeIDs repository method (no chunk_type filter)
- Add collectImageURLs utility with URL deduplication and JSON parse logging
- Add deleteExtractedImages helper (standalone, best-effort with error logging)
- Fix DeleteKnowledge: collect image URLs before errgroup, delete in file goroutine
- Fix DeleteKnowledgeList: bulk collect with KB-level fileSvc mapping
- Fix cleanupKnowledgeResources: collect before chunk deletion, delete after
- Fix ProcessKBDelete: collect before chunk deletion for KB-wide cleanup
This commit is contained in:
ochan.kwon
2026-04-01 22:45:23 +09:00
committed by lyingbug
parent 8a375749f1
commit 33e919cc80
5 changed files with 230 additions and 4 deletions
+8
View File
@@ -6,6 +6,12 @@ import (
"github.com/Tencent/WeKnora/internal/types"
)
// ChunkImageInfo holds (knowledge_id, image_info) pairs for image cleanup before chunk deletion.
type ChunkImageInfo struct {
KnowledgeID string `gorm:"column:knowledge_id"`
ImageInfo string `gorm:"column:image_info"`
}
// ChunkRepository defines the interface for chunk repository operations
type ChunkRepository interface {
// CreateChunks creates chunks
@@ -58,6 +64,8 @@ type ChunkRepository interface {
DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) error
// DeleteByKnowledgeList deletes all chunks for a knowledge list
DeleteByKnowledgeList(ctx context.Context, tenantID uint64, knowledgeIDs []string) error
// ListImageInfoByKnowledgeIDs returns non-empty (knowledge_id, image_info) pairs for image cleanup.
ListImageInfoByKnowledgeIDs(ctx context.Context, tenantID uint64, knowledgeIDs []string) ([]ChunkImageInfo, error)
// MoveChunksByKnowledgeID updates knowledge_base_id for all chunks of a knowledge item
MoveChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string, targetKBID string) error
// DeleteChunksByTagID deletes all chunks with the specified tag ID