fix: deep-copy stored files and images when cloning a knowledge base
Cloning a knowledge base previously copied only the storage path strings (knowledge.FilePath and chunk.ImageInfo.URL), so the source and the clone shared the same physical objects in the storage backend. Once the original file and extracted images are deleted on source removal, the clone is left with dangling references and its document and images become unreadable — data loss that occurs even for same-store clones. Add a CopyFile primitive to the FileService interface and implement it in every backend: server-side CopyObject on the object stores (s3/obs/cos/oss/tos/ks3/minio), io.Copy on local, and a no-op on dummy. Destinations use the knowledge-owned layout and reuse the existing path/object-key guards; a sentinel ErrCrossBackendCopy is returned when the source scheme does not match the backend. Use CopyFile to deep-copy the document file in cloneKnowledge and the extracted images in CloneChunk and cloneFAQKnowledgeBase via a shared cloneChunkImageInfo helper that deduplicates identical image URLs per clone and rewrites them to the new objects. Copied objects are cleaned up best-effort if a clone fails partway through. A clone-time preflight rejects cloning into a target bound to a different storage backend when the tenant pins providers via StorageEngineConfig. Adds unit tests for local CopyFile (independent copy survives source deletion, traversal rejection, cross-backend rejection), cloneChunkImageInfo (empty/multi/dedup/parse-failure/OriginalURL handling), and the storage provider preflight.
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
@@ -229,6 +230,54 @@ func (s *obsFileService) GetFileURL(ctx context.Context, filePath string) (strin
|
||||
return fmt.Sprintf("%s/%s/%s", s.endpoint, s.bucketName, strings.TrimPrefix(objectKey, "/")), nil
|
||||
}
|
||||
|
||||
// CopyFile copies an existing OBS object to a new knowledge-owned object using a
|
||||
// server-side CopyObject (OBS is S3-compatible). The destination uses the same
|
||||
// layout as SaveFile. Returns ErrCrossBackendCopy when srcPath does not belong
|
||||
// to this OBS service.
|
||||
func (s *obsFileService) CopyFile(ctx context.Context,
|
||||
srcPath string, tenantID uint64, knowledgeID string,
|
||||
) (string, error) {
|
||||
// Reject paths that do not use this service's prefix (proxy domain or obs://).
|
||||
// parseObsFilePath falls back to returning the raw input for unknown prefixes,
|
||||
// so guard explicitly here to detect cross-backend sources.
|
||||
if !strings.HasPrefix(srcPath, s.getPrifix()) {
|
||||
return "", fmt.Errorf("obs copy rejected source %q: %w", srcPath, ErrCrossBackendCopy)
|
||||
}
|
||||
srcKey, err := s.parseObsFilePath(srcPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("obs copy rejected source %q: %w", srcPath, ErrCrossBackendCopy)
|
||||
}
|
||||
|
||||
ext := filepath.Ext(srcPath)
|
||||
var destKey string
|
||||
if s.pathPrefix != "" {
|
||||
destKey = fmt.Sprintf("%s/%d/%s/%s%s", s.pathPrefix, tenantID, knowledgeID, uuid.New().String(), ext)
|
||||
} else {
|
||||
destKey = fmt.Sprintf("%d/%s/%s%s", tenantID, knowledgeID, uuid.New().String(), ext)
|
||||
}
|
||||
|
||||
// CopySource is "bucket/key"; the '/' separators must NOT be percent-encoded
|
||||
// (url.PathEscape would turn them into %2F and break the bucket/key split).
|
||||
_, err = s.client.CopyObject(ctx, &s3.CopyObjectInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
CopySource: aws.String(s.bucketName + "/" + srcKey),
|
||||
Key: aws.String(destKey),
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to copy file in OBS: %w", err)
|
||||
}
|
||||
|
||||
prefix := s.getPrifix()
|
||||
var newPath string
|
||||
if s.proxyDomain != "" {
|
||||
newPath = fmt.Sprintf("%s%s", prefix, destKey)
|
||||
} else {
|
||||
newPath = fmt.Sprintf("%s%s/%s", prefix, s.bucketName, destKey)
|
||||
}
|
||||
logger.Infof(ctx, "Copied OBS object %s to %s", srcPath, newPath)
|
||||
return newPath, nil
|
||||
}
|
||||
|
||||
func (s *obsFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
||||
ext := filepath.Ext(fileName)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user