mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-06-04 13:30:32 +08:00
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.
155 lines
4.8 KiB
Go
155 lines
4.8 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
)
|
|
|
|
var _ interfaces.FileService = (*stubFileService)(nil)
|
|
|
|
type stubFileService struct {
|
|
getFile func(ctx context.Context, filePath string) (io.ReadCloser, error)
|
|
}
|
|
|
|
func (s *stubFileService) CheckConnectivity(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *stubFileService) SaveFile(ctx context.Context, file *multipart.FileHeader, tenantID uint64, knowledgeID string) (string, error) {
|
|
panic("unexpected call to SaveFile")
|
|
}
|
|
|
|
func (s *stubFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
|
panic("unexpected call to SaveBytes")
|
|
}
|
|
|
|
func (s *stubFileService) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
|
if s.getFile == nil {
|
|
panic("unexpected call to GetFile")
|
|
}
|
|
return s.getFile(ctx, filePath)
|
|
}
|
|
|
|
func (s *stubFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
|
panic("unexpected call to GetFileURL")
|
|
}
|
|
|
|
func (s *stubFileService) DeleteFile(ctx context.Context, filePath string) error {
|
|
panic("unexpected call to DeleteFile")
|
|
}
|
|
|
|
func (s *stubFileService) CopyFile(ctx context.Context, srcPath string, tenantID uint64, knowledgeID string) (string, error) {
|
|
panic("unexpected call to CopyFile")
|
|
}
|
|
|
|
func TestServeFilesFallsBackToGlobalFileService(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
t.Setenv("STORAGE_TYPE", "local")
|
|
|
|
engine := gin.New()
|
|
var requestedPath string
|
|
serveFiles(engine, &stubFileService{
|
|
getFile: func(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
|
requestedPath = filePath
|
|
return io.NopCloser(strings.NewReader("fallback-body")), nil
|
|
},
|
|
})
|
|
|
|
filePath := "local://42/docs/example.txt"
|
|
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape(filePath), nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
|
|
|
|
recorder := httptest.NewRecorder()
|
|
engine.ServeHTTP(recorder, req)
|
|
|
|
if got, want := recorder.Code, http.StatusOK; got != want {
|
|
t.Fatalf("status = %d, want %d", got, want)
|
|
}
|
|
if requestedPath != filePath {
|
|
t.Fatalf("requested path = %q, want %q", requestedPath, filePath)
|
|
}
|
|
if body := recorder.Body.String(); body != "fallback-body" {
|
|
t.Fatalf("body = %q, want %q", body, "fallback-body")
|
|
}
|
|
}
|
|
|
|
func TestServeFilesDoesNotFallbackWhenProviderDoesNotMatchGlobalStorage(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
t.Setenv("STORAGE_TYPE", "minio")
|
|
|
|
engine := gin.New()
|
|
serveFiles(engine, &stubFileService{
|
|
getFile: func(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
|
t.Fatalf("GetFile should not be called for mismatched provider, got %q", filePath)
|
|
return nil, nil
|
|
},
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://42/docs/example.txt"), nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
|
|
|
|
recorder := httptest.NewRecorder()
|
|
engine.ServeHTTP(recorder, req)
|
|
|
|
if got, want := recorder.Code, http.StatusBadRequest; got != want {
|
|
t.Fatalf("status = %d, want %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestServeFilesRejectsCrossTenantPath(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
t.Setenv("STORAGE_TYPE", "local")
|
|
|
|
engine := gin.New()
|
|
serveFiles(engine, &stubFileService{
|
|
getFile: func(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
|
t.Fatalf("GetFile should not be called for cross-tenant path, got %q", filePath)
|
|
return nil, nil
|
|
},
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://7/knowledge/secret.pdf"), nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
|
|
|
|
recorder := httptest.NewRecorder()
|
|
engine.ServeHTTP(recorder, req)
|
|
|
|
if got, want := recorder.Code, http.StatusForbidden; got != want {
|
|
t.Fatalf("status = %d, want %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestServeFilesRejectsPathWithoutTenantSegment(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
t.Setenv("STORAGE_TYPE", "local")
|
|
|
|
engine := gin.New()
|
|
serveFiles(engine, &stubFileService{
|
|
getFile: func(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
|
t.Fatalf("GetFile should not be called without tenant segment, got %q", filePath)
|
|
return nil, nil
|
|
},
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape("local://docs/example.txt"), nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
|
|
|
|
recorder := httptest.NewRecorder()
|
|
engine.ServeHTTP(recorder, req)
|
|
|
|
if got, want := recorder.Code, http.StatusForbidden; got != want {
|
|
t.Fatalf("status = %d, want %d", got, want)
|
|
}
|
|
}
|