refactor: simplify image handling in _resolve_images function
- Removed the image storage handling from the `_resolve_images` function, which now only decodes images and returns them as inline bytes. - Updated the function's docstring to clarify that image persistence is managed by the Go App, and the return value for `image_dir_path` is always empty. - Adjusted related gRPC and protobuf files to remove the `ConvertToPDF` method and its associated types, streamlining the API. - Enhanced security measures in the frontend by implementing a placeholder for provider images and ensuring proper hydration of protected file images. These changes improve the clarity and efficiency of image processing and API interactions within the application.
This commit is contained in:
@@ -26,8 +26,12 @@ type cosFileService struct {
|
||||
cosPathPrefix string
|
||||
tempClient *cos.Client
|
||||
tempBucketURL string
|
||||
bucketName string
|
||||
region string
|
||||
}
|
||||
|
||||
const cosScheme = "cos://"
|
||||
|
||||
// newCosClient creates a bare cosFileService with just the SDK client initialised.
|
||||
// Shared by NewCosFileService* constructors and CheckCosConnectivity.
|
||||
func newCosClient(bucketName, region, secretID, secretKey string) (*cosFileService, error) {
|
||||
@@ -43,7 +47,7 @@ func newCosClient(bucketName, region, secretID, secretKey string) (*cosFileServi
|
||||
SecretKey: secretKey,
|
||||
},
|
||||
})
|
||||
return &cosFileService{client: client, bucketURL: bucketURL}, nil
|
||||
return &cosFileService{client: client, bucketURL: bucketURL, bucketName: bucketName, region: region}, nil
|
||||
}
|
||||
|
||||
// NewCosFileService creates a new COS file service instance
|
||||
@@ -114,12 +118,12 @@ func (s *cosFileService) SaveFile(ctx context.Context,
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload file to COS: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("%s%s", s.bucketURL, objectName), nil
|
||||
return fmt.Sprintf("cos://%s/%s/%s", s.bucketName, s.region, objectName), nil
|
||||
}
|
||||
|
||||
// GetFile retrieves a file from COS storage by its path URL
|
||||
func (s *cosFileService) GetFile(ctx context.Context, filePathUrl string) (io.ReadCloser, error) {
|
||||
objectName := strings.TrimPrefix(filePathUrl, s.bucketURL)
|
||||
objectName := s.parseCosObjectName(filePathUrl)
|
||||
if err := utils.SafeObjectKey(objectName); err != nil {
|
||||
return nil, fmt.Errorf("invalid file path: %w", err)
|
||||
}
|
||||
@@ -132,7 +136,7 @@ func (s *cosFileService) GetFile(ctx context.Context, filePathUrl string) (io.Re
|
||||
|
||||
// DeleteFile removes a file from COS storage
|
||||
func (s *cosFileService) DeleteFile(ctx context.Context, filePath string) error {
|
||||
objectName := strings.TrimPrefix(filePath, s.bucketURL)
|
||||
objectName := s.parseCosObjectName(filePath)
|
||||
if err := utils.SafeObjectKey(objectName); err != nil {
|
||||
return fmt.Errorf("invalid file path: %w", err)
|
||||
}
|
||||
@@ -143,6 +147,23 @@ func (s *cosFileService) DeleteFile(ctx context.Context, filePath string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseCosObjectName extracts the object name from:
|
||||
// - provider scheme: cos://{bucket}/{region}/{objectKey}
|
||||
// - legacy URL: https://bucket.cos.region.myqcloud.com/{objectKey}
|
||||
func (s *cosFileService) parseCosObjectName(filePath string) string {
|
||||
// Provider scheme format: cos://{bucket}/{region}/{objectKey}
|
||||
if strings.HasPrefix(filePath, cosScheme) {
|
||||
rest := strings.TrimPrefix(filePath, cosScheme)
|
||||
parts := strings.SplitN(rest, "/", 3)
|
||||
if len(parts) == 3 {
|
||||
return parts[2]
|
||||
}
|
||||
return rest
|
||||
}
|
||||
// Legacy format: https://bucket.cos.region.myqcloud.com/{objectKey}
|
||||
return strings.TrimPrefix(filePath, s.bucketURL)
|
||||
}
|
||||
|
||||
// SaveBytes saves bytes data to COS
|
||||
// If temp is true and temp bucket is configured, saves to temp bucket (with lifecycle auto-expiration)
|
||||
// Otherwise saves to main bucket
|
||||
@@ -161,6 +182,7 @@ func (s *cosFileService) SaveBytes(ctx context.Context, data []byte, tenantID ui
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload bytes to COS temp bucket: %w", err)
|
||||
}
|
||||
// Temp bucket still uses legacy URL format for backward compat (auto-expiring)
|
||||
return fmt.Sprintf("%s%s", s.tempBucketURL, objectName), nil
|
||||
}
|
||||
|
||||
@@ -171,7 +193,7 @@ func (s *cosFileService) SaveBytes(ctx context.Context, data []byte, tenantID ui
|
||||
return "", fmt.Errorf("failed to upload bytes to COS: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s", s.bucketURL, objectName), nil
|
||||
return fmt.Sprintf("cos://%s/%s/%s", s.bucketName, s.region, objectName), nil
|
||||
}
|
||||
|
||||
// GetFileURL returns a presigned download URL for the file
|
||||
@@ -190,7 +212,7 @@ func (s *cosFileService) GetFileURL(ctx context.Context, filePath string) (strin
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
objectName := strings.TrimPrefix(filePath, s.bucketURL)
|
||||
objectName := s.parseCosObjectName(filePath)
|
||||
if err := utils.SafeObjectKey(objectName); err != nil {
|
||||
return "", fmt.Errorf("invalid file path: %w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
// NewFileServiceFromStorageConfig builds a provider-specific FileService from tenant storage config.
|
||||
// provider can be empty; in that case it falls back to sec.DefaultProvider.
|
||||
// Returns the resolved provider name together with the service.
|
||||
func NewFileServiceFromStorageConfig(
|
||||
provider string,
|
||||
sec *types.StorageEngineConfig,
|
||||
localBaseDir string,
|
||||
) (interfaces.FileService, string, error) {
|
||||
p := strings.ToLower(strings.TrimSpace(provider))
|
||||
if p == "" && sec != nil {
|
||||
p = strings.ToLower(strings.TrimSpace(sec.DefaultProvider))
|
||||
}
|
||||
if p == "" {
|
||||
return nil, "", fmt.Errorf("empty provider")
|
||||
}
|
||||
|
||||
if localBaseDir == "" {
|
||||
localBaseDir = strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR"))
|
||||
}
|
||||
if localBaseDir == "" {
|
||||
localBaseDir = "/data/files"
|
||||
}
|
||||
|
||||
switch p {
|
||||
case "local":
|
||||
baseDir := localBaseDir
|
||||
if sec != nil && sec.Local != nil {
|
||||
rawPrefix := strings.TrimSpace(sec.Local.PathPrefix)
|
||||
prefix := strings.Trim(rawPrefix, "/\\")
|
||||
if prefix != "" {
|
||||
candidate := filepath.Join(baseDir, prefix)
|
||||
if safeBaseDir, err := secutils.SafePathUnderBase(baseDir, candidate); err == nil {
|
||||
baseDir = safeBaseDir
|
||||
}
|
||||
}
|
||||
}
|
||||
return NewLocalFileService(baseDir), p, nil
|
||||
|
||||
case "minio":
|
||||
if sec == nil || sec.MinIO == nil {
|
||||
return nil, p, fmt.Errorf("missing minio config")
|
||||
}
|
||||
var endpoint, accessKeyID, secretAccessKey string
|
||||
if sec.MinIO.Mode == "remote" {
|
||||
endpoint = strings.TrimSpace(sec.MinIO.Endpoint)
|
||||
accessKeyID = strings.TrimSpace(sec.MinIO.AccessKeyID)
|
||||
secretAccessKey = strings.TrimSpace(sec.MinIO.SecretAccessKey)
|
||||
} else {
|
||||
endpoint = strings.TrimSpace(os.Getenv("MINIO_ENDPOINT"))
|
||||
accessKeyID = strings.TrimSpace(os.Getenv("MINIO_ACCESS_KEY_ID"))
|
||||
secretAccessKey = strings.TrimSpace(os.Getenv("MINIO_SECRET_ACCESS_KEY"))
|
||||
}
|
||||
bucketName := strings.TrimSpace(sec.MinIO.BucketName)
|
||||
if bucketName == "" {
|
||||
bucketName = strings.TrimSpace(os.Getenv("MINIO_BUCKET_NAME"))
|
||||
}
|
||||
if endpoint == "" || accessKeyID == "" || secretAccessKey == "" || bucketName == "" {
|
||||
return nil, p, fmt.Errorf("incomplete minio config")
|
||||
}
|
||||
svc, err := NewMinioFileService(endpoint, accessKeyID, secretAccessKey, bucketName, sec.MinIO.UseSSL)
|
||||
return svc, p, err
|
||||
|
||||
case "cos":
|
||||
if sec == nil || sec.COS == nil || sec.COS.SecretID == "" || sec.COS.SecretKey == "" || sec.COS.BucketName == "" || sec.COS.Region == "" {
|
||||
return nil, p, fmt.Errorf("incomplete cos config")
|
||||
}
|
||||
pathPrefix := strings.TrimSpace(sec.COS.PathPrefix)
|
||||
if pathPrefix == "" {
|
||||
pathPrefix = "weknora"
|
||||
}
|
||||
svc, err := NewCosFileService(sec.COS.BucketName, sec.COS.Region, sec.COS.SecretID, sec.COS.SecretKey, pathPrefix)
|
||||
return svc, p, err
|
||||
|
||||
case "tos":
|
||||
if sec == nil || sec.TOS == nil || sec.TOS.Endpoint == "" || sec.TOS.Region == "" || sec.TOS.AccessKey == "" || sec.TOS.SecretKey == "" || sec.TOS.BucketName == "" {
|
||||
return nil, p, fmt.Errorf("incomplete tos config")
|
||||
}
|
||||
svc, err := NewTosFileService(sec.TOS.Endpoint, sec.TOS.Region, sec.TOS.AccessKey, sec.TOS.SecretKey, sec.TOS.BucketName, sec.TOS.PathPrefix)
|
||||
return svc, p, err
|
||||
|
||||
default:
|
||||
return nil, p, fmt.Errorf("unsupported provider %q", p)
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ type localFileService struct {
|
||||
baseDir string // Base directory for file storage
|
||||
}
|
||||
|
||||
const localScheme = "local://"
|
||||
|
||||
// CheckConnectivity verifies the local storage directory exists and is accessible.
|
||||
func (s *localFileService) CheckConnectivity(ctx context.Context) error {
|
||||
info, err := os.Stat(s.baseDir)
|
||||
@@ -93,11 +95,14 @@ func (s *localFileService) SaveFile(ctx context.Context,
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "File saved successfully: %s", filePath)
|
||||
return filePath, nil
|
||||
// Return provider:// path format: local://{relative_path}
|
||||
relPath, _ := filepath.Rel(s.baseDir, filePath)
|
||||
return localScheme + filepath.ToSlash(relPath), nil
|
||||
}
|
||||
|
||||
// GetFile retrieves a file from the local file system by its path
|
||||
// Returns a ReadCloser for reading the file content
|
||||
// Supports both provider scheme: local://{relative_path} and legacy absolute paths.
|
||||
// 路径必须在 baseDir 下,防止路径遍历(如 ../../)
|
||||
func (s *localFileService) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {
|
||||
logger.Infof(ctx, "Getting file: %s", filePath)
|
||||
@@ -174,21 +179,37 @@ func (s *localFileService) SaveBytes(ctx context.Context, data []byte, tenantID
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Bytes data saved successfully: %s", filePath)
|
||||
return filePath, nil
|
||||
relPath, _ := filepath.Rel(s.baseDir, filePath)
|
||||
return localScheme + filepath.ToSlash(relPath), nil
|
||||
}
|
||||
|
||||
// GetFileURL returns a download URL for the file
|
||||
// For local storage, returns the file path itself (no URL support)
|
||||
// For local storage, returns the local://... path
|
||||
func (s *localFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
||||
// Local storage doesn't support URLs, return the path
|
||||
return filePath, nil
|
||||
// If already in provider:// format, return as-is
|
||||
if strings.HasPrefix(filePath, localScheme) {
|
||||
return filePath, nil
|
||||
}
|
||||
// Convert absolute path to provider:// format
|
||||
relPath, err := filepath.Rel(s.baseDir, filePath)
|
||||
if err != nil {
|
||||
return filePath, nil
|
||||
}
|
||||
return localScheme + filepath.ToSlash(relPath), nil
|
||||
}
|
||||
|
||||
// normalizePathForBase keeps backward compatibility for legacy file paths:
|
||||
// - provider scheme: "local://tenant/.." → baseDir/tenant/..
|
||||
// - absolute path: "/data/files/tenant/.."
|
||||
// - path under base dir: "tenant/.."
|
||||
// - legacy relative with base prefix: "data/files/tenant/.."
|
||||
func (s *localFileService) normalizePathForBase(filePath string) string {
|
||||
// Handle provider:// format: local://{relPath}
|
||||
if strings.HasPrefix(filePath, localScheme) {
|
||||
relPath := strings.TrimPrefix(filePath, localScheme)
|
||||
return filepath.Join(s.baseDir, filepath.FromSlash(relPath))
|
||||
}
|
||||
|
||||
clean := filepath.Clean(strings.TrimSpace(filePath))
|
||||
if clean == "." || clean == "" {
|
||||
return clean
|
||||
|
||||
@@ -90,9 +90,9 @@ func CheckMinioConnectivity(ctx context.Context, endpoint, accessKeyID, secretAc
|
||||
return svc.CheckConnectivity(ctx)
|
||||
}
|
||||
|
||||
// parseMinioFilePath extracts the object name from a "minio://bucket/key" path,
|
||||
// validates the bucket matches this service instance, and checks for path traversal.
|
||||
// parseMinioFilePath extracts the object name from a provider scheme: minio://{bucket}/{objectKey}
|
||||
func (s *minioFileService) parseMinioFilePath(filePath string) (string, error) {
|
||||
// Provider scheme format: minio://{bucket}/{objectKey}
|
||||
const prefix = "minio://"
|
||||
if !strings.HasPrefix(filePath, prefix) {
|
||||
return "", fmt.Errorf("invalid MinIO file path: %s", filePath)
|
||||
@@ -134,7 +134,6 @@ func (s *minioFileService) SaveFile(ctx context.Context,
|
||||
return "", fmt.Errorf("failed to upload file to MinIO: %w", err)
|
||||
}
|
||||
|
||||
// Return the complete path to the object
|
||||
return fmt.Sprintf("minio://%s/%s", s.bucketName, objectName), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@ type tosFileService struct {
|
||||
tempBucketName string
|
||||
}
|
||||
|
||||
const tosScheme = "tos://"
|
||||
|
||||
// NewTosFileService creates a TOS file service.
|
||||
func NewTosFileService(endpoint, region, accessKey, secretKey, bucketName, pathPrefix string) (interfaces.FileService, error) {
|
||||
return NewTosFileServiceWithTempBucket(endpoint, region, accessKey, secretKey, bucketName, pathPrefix, "", "")
|
||||
@@ -137,12 +139,11 @@ func joinTOSObjectKey(parts ...string) string {
|
||||
}
|
||||
|
||||
func parseTOSFilePath(filePath string) (bucketName string, objectKey string, err error) {
|
||||
const prefix = "tos://"
|
||||
if !strings.HasPrefix(filePath, prefix) {
|
||||
if !strings.HasPrefix(filePath, tosScheme) {
|
||||
return "", "", fmt.Errorf("invalid TOS file path: %s", filePath)
|
||||
}
|
||||
|
||||
rest := strings.TrimPrefix(filePath, prefix)
|
||||
rest := strings.TrimPrefix(filePath, tosScheme)
|
||||
parts := strings.SplitN(rest, "/", 2)
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||
return "", "", fmt.Errorf("invalid TOS file path: %s", filePath)
|
||||
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
filesvc "github.com/Tencent/WeKnora/internal/application/service/file"
|
||||
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
||||
@@ -31,8 +33,8 @@ const (
|
||||
)
|
||||
|
||||
// ImageMultimodalService handles image:multimodal asynq tasks.
|
||||
// It reads images from local storage, performs OCR and VLM caption,
|
||||
// and creates child chunks for the processed content.
|
||||
// It reads images from storage (via FileService for provider:// URLs),
|
||||
// performs OCR and VLM caption, and creates child chunks.
|
||||
type ImageMultimodalService struct {
|
||||
chunkService interfaces.ChunkService
|
||||
modelService interfaces.ModelService
|
||||
@@ -70,8 +72,8 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e
|
||||
return fmt.Errorf("unmarshal image multimodal payload: %w", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[ImageMultimodal] Processing image: chunk=%s, url=%s, local_path=%s, ocr=%v, caption=%v",
|
||||
payload.ChunkID, payload.ImageURL, payload.ImageLocalPath, payload.EnableOCR, payload.EnableCaption)
|
||||
logger.Infof(ctx, "[ImageMultimodal] Processing image: chunk=%s, url=%s, ocr=%v, caption=%v",
|
||||
payload.ChunkID, payload.ImageURL, payload.EnableOCR, payload.EnableCaption)
|
||||
|
||||
ctx = context.WithValue(ctx, types.TenantIDContextKey, payload.TenantID)
|
||||
|
||||
@@ -80,24 +82,43 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e
|
||||
return fmt.Errorf("resolve VLM: %w", err)
|
||||
}
|
||||
|
||||
imgBytes, err := os.ReadFile(payload.ImageLocalPath)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] Local file %s not available (%v), trying to download from URL", payload.ImageLocalPath, err)
|
||||
// Read image bytes: try provider:// via tenant-resolved FileService,
|
||||
// then legacy local path, then HTTP URL.
|
||||
var imgBytes []byte
|
||||
if types.ParseProviderScheme(payload.ImageURL) != "" {
|
||||
fileSvc := s.resolveFileServiceForPayload(ctx, payload)
|
||||
if fileSvc == nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] Resolve tenant file service failed, fallback to URL/local: tenant=%d kb=%s",
|
||||
payload.TenantID, payload.KnowledgeBaseID)
|
||||
} else {
|
||||
// provider:// scheme — read via FileService
|
||||
reader, getErr := fileSvc.GetFile(ctx, payload.ImageURL)
|
||||
if getErr != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] FileService.GetFile(%s) failed: %v", payload.ImageURL, getErr)
|
||||
} else {
|
||||
imgBytes, err = io.ReadAll(reader)
|
||||
reader.Close()
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] Read provider file %s failed: %v", payload.ImageURL, err)
|
||||
imgBytes = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if imgBytes == nil && payload.ImageLocalPath != "" {
|
||||
imgBytes, err = os.ReadFile(payload.ImageLocalPath)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] Local file %s not available (%v), trying URL", payload.ImageLocalPath, err)
|
||||
imgBytes = nil
|
||||
}
|
||||
}
|
||||
if imgBytes == nil {
|
||||
imgBytes, err = downloadImageFromURL(payload.ImageURL)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "[ImageMultimodal] Failed to download image from URL %s: %v", payload.ImageURL, err)
|
||||
return fmt.Errorf("read image: local file %s not found and URL download failed: %w", payload.ImageLocalPath, err)
|
||||
return fmt.Errorf("read image from URL %s failed: %w", payload.ImageURL, err)
|
||||
}
|
||||
logger.Infof(ctx, "[ImageMultimodal] Image downloaded from URL, len=%d", len(imgBytes))
|
||||
} else {
|
||||
logger.Infof(ctx, "[ImageMultimodal] Image bytes read from %s, len=%d", payload.ImageLocalPath, len(imgBytes))
|
||||
// Only clean up the local file when images have been re-uploaded to
|
||||
// external object storage (COS/TOS) — the serving URL will be an
|
||||
// absolute http(s) URL in that case. For local and MinIO storage the
|
||||
// local file IS the serving file and must be kept.
|
||||
if strings.HasPrefix(payload.ImageURL, "http://") || strings.HasPrefix(payload.ImageURL, "https://") {
|
||||
defer os.Remove(payload.ImageLocalPath)
|
||||
}
|
||||
}
|
||||
|
||||
imageInfo := types.ImageInfo{
|
||||
@@ -352,6 +373,33 @@ func (s *ImageMultimodalService) resolveVLM(ctx context.Context, kbID string) (v
|
||||
return vlm.NewVLMFromLegacyConfig(vlmCfg, s.ollamaService)
|
||||
}
|
||||
|
||||
// resolveFileServiceForPayload resolves tenant/KB scoped file service for reading provider:// URLs.
|
||||
func (s *ImageMultimodalService) resolveFileServiceForPayload(ctx context.Context, payload types.ImageMultimodalPayload) interfaces.FileService {
|
||||
tenant, err := s.tenantRepo.GetTenantByID(ctx, payload.TenantID)
|
||||
if err != nil || tenant == nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] GetTenantByID failed: tenant=%d err=%v", payload.TenantID, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
provider := types.ParseProviderScheme(payload.ImageURL)
|
||||
if provider == "" {
|
||||
kb, kbErr := s.kbService.GetKnowledgeBaseByIDOnly(ctx, payload.KnowledgeBaseID)
|
||||
if kbErr != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] GetKnowledgeBaseByIDOnly failed: kb=%s err=%v", payload.KnowledgeBaseID, kbErr)
|
||||
} else if kb != nil {
|
||||
provider = strings.ToLower(strings.TrimSpace(kb.GetStorageProvider()))
|
||||
}
|
||||
}
|
||||
|
||||
baseDir := strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR"))
|
||||
fileSvc, _, svcErr := filesvc.NewFileServiceFromStorageConfig(provider, tenant.StorageEngineConfig, baseDir)
|
||||
if svcErr != nil {
|
||||
logger.Warnf(ctx, "[ImageMultimodal] resolve file service failed: tenant=%d provider=%s err=%v", payload.TenantID, provider, svcErr)
|
||||
return nil
|
||||
}
|
||||
return fileSvc
|
||||
}
|
||||
|
||||
// downloadImageFromURL downloads image bytes from an HTTP(S) URL.
|
||||
func downloadImageFromURL(imageURL string) ([]byte, error) {
|
||||
return secutils.DownloadBytes(imageURL)
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
@@ -6845,79 +6844,13 @@ func (s *knowledgeService) resolveFileService(ctx context.Context, kb *types.Kno
|
||||
}
|
||||
|
||||
sec := tenant.StorageEngineConfig
|
||||
var svc interfaces.FileService
|
||||
var err error
|
||||
|
||||
switch provider {
|
||||
case "local":
|
||||
baseDir := strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR"))
|
||||
if baseDir == "" {
|
||||
baseDir = "/data/files"
|
||||
}
|
||||
if sec.Local != nil && sec.Local.PathPrefix != "" {
|
||||
// path_prefix is a subdirectory under local storage base dir, not a replacement.
|
||||
// Validate it cannot escape baseDir (e.g. path traversal via "..").
|
||||
rawPrefix := strings.TrimSpace(sec.Local.PathPrefix)
|
||||
prefix := strings.Trim(rawPrefix, "/\\")
|
||||
if prefix != "" {
|
||||
candidate := filepath.Join(baseDir, prefix)
|
||||
safeBaseDir, pathErr := secutils.SafePathUnderBase(baseDir, candidate)
|
||||
if pathErr != nil {
|
||||
logger.Warnf(ctx,
|
||||
"[storage] invalid local path_prefix ignored: kb=%s raw_prefix=%q err=%v",
|
||||
kb.ID, rawPrefix, pathErr)
|
||||
} else {
|
||||
baseDir = safeBaseDir
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.Infof(ctx, "[storage] resolveFileService local: kb=%s base_dir=%s", kb.ID, baseDir)
|
||||
return filesvc.NewLocalFileService(baseDir)
|
||||
|
||||
case "minio":
|
||||
if sec.MinIO == nil {
|
||||
return s.fileSvc
|
||||
}
|
||||
var endpoint, accessKeyID, secretAccessKey string
|
||||
if sec.MinIO.Mode == "remote" {
|
||||
endpoint = sec.MinIO.Endpoint
|
||||
accessKeyID = sec.MinIO.AccessKeyID
|
||||
secretAccessKey = sec.MinIO.SecretAccessKey
|
||||
} else {
|
||||
endpoint = os.Getenv("MINIO_ENDPOINT")
|
||||
accessKeyID = os.Getenv("MINIO_ACCESS_KEY_ID")
|
||||
secretAccessKey = os.Getenv("MINIO_SECRET_ACCESS_KEY")
|
||||
}
|
||||
bucketName := sec.MinIO.BucketName
|
||||
if bucketName == "" {
|
||||
bucketName = os.Getenv("MINIO_BUCKET_NAME")
|
||||
}
|
||||
if endpoint == "" || accessKeyID == "" || secretAccessKey == "" || bucketName == "" {
|
||||
logger.Warnf(ctx, "Incomplete MinIO config for KB storage, falling back to default")
|
||||
return s.fileSvc
|
||||
}
|
||||
svc, err = filesvc.NewMinioFileService(endpoint, accessKeyID, secretAccessKey, bucketName, sec.MinIO.UseSSL)
|
||||
|
||||
case "cos":
|
||||
if sec.COS == nil || sec.COS.SecretID == "" || sec.COS.BucketName == "" || sec.COS.Region == "" {
|
||||
logger.Warnf(ctx, "Incomplete COS config for KB storage, falling back to default")
|
||||
return s.fileSvc
|
||||
}
|
||||
pathPrefix := sec.COS.PathPrefix
|
||||
if pathPrefix == "" {
|
||||
pathPrefix = "weknora"
|
||||
}
|
||||
svc, err = filesvc.NewCosFileService(sec.COS.BucketName, sec.COS.Region, sec.COS.SecretID, sec.COS.SecretKey, pathPrefix)
|
||||
|
||||
default:
|
||||
return s.fileSvc
|
||||
}
|
||||
|
||||
baseDir := strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR"))
|
||||
svc, resolvedProvider, err := filesvc.NewFileServiceFromStorageConfig(provider, sec, baseDir)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to create %s file service from tenant config: %v, falling back to default", provider, err)
|
||||
return s.fileSvc
|
||||
}
|
||||
logger.Infof(ctx, "[storage] resolveFileService selected: kb=%s provider=%s", kb.ID, provider)
|
||||
logger.Infof(ctx, "[storage] resolveFileService selected: kb=%s provider=%s", kb.ID, resolvedProvider)
|
||||
return svc
|
||||
}
|
||||
|
||||
@@ -7236,7 +7169,9 @@ func (s *knowledgeService) ProcessDocument(ctx context.Context, t *asynq.Task) e
|
||||
// Step 2: Store images and update markdown references
|
||||
var storedImages []docparser.StoredImage
|
||||
if s.imageResolver != nil && convertResult != nil {
|
||||
updatedMarkdown, images, resolveErr := s.imageResolver.ResolveAndStore(convertResult)
|
||||
fileSvc := s.resolveFileService(ctx, kb)
|
||||
tenantID, _ := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
updatedMarkdown, images, resolveErr := s.imageResolver.ResolveAndStore(ctx, convertResult, fileSvc, tenantID)
|
||||
if resolveErr != nil {
|
||||
logger.Warnf(ctx, "Image resolution partially failed: %v", resolveErr)
|
||||
}
|
||||
@@ -7245,14 +7180,6 @@ func (s *knowledgeService) ProcessDocument(ctx context.Context, t *asynq.Task) e
|
||||
}
|
||||
storedImages = images
|
||||
logger.Infof(ctx, "Resolved %d images for knowledge %s", len(storedImages), knowledge.ID)
|
||||
|
||||
// If KB uses object storage (COS/MinIO), re-upload images from local to object storage
|
||||
if len(storedImages) > 0 {
|
||||
fileSvc := s.resolveFileService(ctx, kb)
|
||||
reuploadedMd, reuploadedImages := s.reuploadImagesToStorage(ctx, kb, fileSvc, storedImages, convertResult.MarkdownContent)
|
||||
convertResult.MarkdownContent = reuploadedMd
|
||||
storedImages = reuploadedImages
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Split into chunks using Go chunker
|
||||
@@ -7291,13 +7218,6 @@ func (s *knowledgeService) ProcessDocument(ctx context.Context, t *asynq.Task) e
|
||||
StoredImages: storedImages,
|
||||
})
|
||||
|
||||
// Step 5: Clean up temp dir
|
||||
if convertResult.ImageDirPath != "" {
|
||||
if removeErr := os.RemoveAll(convertResult.ImageDirPath); removeErr != nil {
|
||||
logger.Warnf(ctx, "Failed to clean up temp image dir %s: %v", convertResult.ImageDirPath, removeErr)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7341,27 +7261,12 @@ func (s *knowledgeService) convert(
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
imageStorage := s.buildImageStorageConfig(ctx, kb)
|
||||
if imageStorage == nil {
|
||||
logger.Infof(ctx, "[storage] chunk image storage: kb=%s provider=%s image_storage=nil(use local temp/files)",
|
||||
kb.ID, kb.GetStorageProvider())
|
||||
} else {
|
||||
logger.Infof(ctx, "[storage] chunk image storage: kb=%s provider=%s bucket=%s endpoint=%s path_prefix=%s",
|
||||
kb.ID,
|
||||
imageStorage["provider"],
|
||||
imageStorage["bucket_name"],
|
||||
imageStorage["endpoint"],
|
||||
imageStorage["path_prefix"],
|
||||
)
|
||||
}
|
||||
|
||||
req := &types.ReadRequest{
|
||||
URL: payload.URL,
|
||||
Title: knowledge.Title,
|
||||
ParserEngine: parserEngine,
|
||||
RequestID: payload.RequestId,
|
||||
ParserEngineOverrides: overrides,
|
||||
ImageStorage: imageStorage,
|
||||
}
|
||||
|
||||
if !isURL {
|
||||
@@ -7429,189 +7334,6 @@ func (s *knowledgeService) failKnowledge(
|
||||
return nil, fmt.Errorf(format, args...)
|
||||
}
|
||||
|
||||
// reuploadImagesToStorage re-uploads locally stored images to the KB's object storage
|
||||
// (COS/MinIO) when the KB is not using local storage. It updates the serving URLs in
|
||||
// both the markdown content and the StoredImage list.
|
||||
// Local copies are intentionally kept because async tasks (e.g. image:multimodal)
|
||||
// may still need to read them; they are cleaned up after those tasks complete.
|
||||
// Returns the original inputs unchanged when the storage provider is "local" or empty.
|
||||
func (s *knowledgeService) reuploadImagesToStorage(
|
||||
ctx context.Context,
|
||||
kb *types.KnowledgeBase,
|
||||
fileSvc interfaces.FileService,
|
||||
images []docparser.StoredImage,
|
||||
markdown string,
|
||||
) (string, []docparser.StoredImage) {
|
||||
provider := kb.GetStorageProvider()
|
||||
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
if provider == "" && tenant != nil && tenant.StorageEngineConfig != nil {
|
||||
provider = strings.ToLower(strings.TrimSpace(tenant.StorageEngineConfig.DefaultProvider))
|
||||
}
|
||||
if provider == "" || provider == "local" {
|
||||
return markdown, images
|
||||
}
|
||||
|
||||
tenantID, _ := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
updated := make([]docparser.StoredImage, len(images))
|
||||
copy(updated, images)
|
||||
|
||||
for i, img := range updated {
|
||||
data, err := os.ReadFile(img.LocalPath)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "reuploadImagesToStorage: cannot read local image %s: %v", img.LocalPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
storagePath, err := fileSvc.SaveBytes(ctx, data, tenantID, filepath.Base(img.LocalPath), false)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "reuploadImagesToStorage: failed to upload image to %s: %v", provider, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// For COS, SaveBytes returns a direct public URL (https://bucket.cos...).
|
||||
// Use it directly instead of GetFileURL which returns a presigned URL that
|
||||
// expires after 24h — unsuitable for permanent markdown image references.
|
||||
// For MinIO, SaveBytes returns minio://... which is not a usable URL, so
|
||||
// keep the local /files/images/... serving URL (the app proxies MinIO files).
|
||||
storageURL := storagePath
|
||||
if provider == "minio" {
|
||||
storageURL = img.ServingURL
|
||||
}
|
||||
|
||||
if storageURL != img.ServingURL {
|
||||
logger.Infof(ctx, "reuploadImagesToStorage: uploaded %s -> %s", img.ServingURL, storageURL)
|
||||
markdown = strings.ReplaceAll(markdown, img.ServingURL, storageURL)
|
||||
updated[i].ServingURL = storageURL
|
||||
} else {
|
||||
logger.Infof(ctx, "reuploadImagesToStorage: uploaded %s to %s storage (serving via local URL)", img.ServingURL, provider)
|
||||
}
|
||||
}
|
||||
|
||||
return markdown, updated
|
||||
}
|
||||
|
||||
// buildImageStorageConfig returns storage config map to pass to DocReader so it can
|
||||
// upload images to shared storage. Returns nil for local storage (use temp dir / inline bytes).
|
||||
// Prioritises KB/tenant-level StorageEngineConfig; falls back to environment variables.
|
||||
func (s *knowledgeService) buildImageStorageConfig(ctx context.Context, kb *types.KnowledgeBase) map[string]string {
|
||||
provider := ""
|
||||
if kb != nil {
|
||||
provider = kb.GetStorageProvider()
|
||||
}
|
||||
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
if provider == "" && tenant != nil && tenant.StorageEngineConfig != nil {
|
||||
provider = strings.ToLower(strings.TrimSpace(tenant.StorageEngineConfig.DefaultProvider))
|
||||
}
|
||||
|
||||
// Try tenant-level StorageEngineConfig first
|
||||
if tenant != nil && tenant.StorageEngineConfig != nil {
|
||||
sec := tenant.StorageEngineConfig
|
||||
switch provider {
|
||||
case "cos":
|
||||
if sec.COS != nil && sec.COS.SecretID != "" && sec.COS.BucketName != "" && sec.COS.Region != "" {
|
||||
m := map[string]string{
|
||||
"provider": "cos",
|
||||
"bucket_name": sec.COS.BucketName,
|
||||
"access_key_id": sec.COS.SecretID,
|
||||
"secret_access_key": sec.COS.SecretKey,
|
||||
"region": sec.COS.Region,
|
||||
}
|
||||
if sec.COS.AppID != "" {
|
||||
m["app_id"] = sec.COS.AppID
|
||||
}
|
||||
if sec.COS.PathPrefix != "" {
|
||||
m["path_prefix"] = sec.COS.PathPrefix
|
||||
}
|
||||
return m
|
||||
}
|
||||
case "minio":
|
||||
if sec.MinIO != nil {
|
||||
var endpoint, accessKey, secretKey, bucket string
|
||||
if sec.MinIO.Mode == "remote" {
|
||||
endpoint = sec.MinIO.Endpoint
|
||||
accessKey = sec.MinIO.AccessKeyID
|
||||
secretKey = sec.MinIO.SecretAccessKey
|
||||
} else {
|
||||
endpoint = os.Getenv("MINIO_ENDPOINT")
|
||||
accessKey = os.Getenv("MINIO_ACCESS_KEY_ID")
|
||||
secretKey = os.Getenv("MINIO_SECRET_ACCESS_KEY")
|
||||
}
|
||||
bucket = sec.MinIO.BucketName
|
||||
if bucket == "" {
|
||||
bucket = os.Getenv("MINIO_BUCKET_NAME")
|
||||
}
|
||||
if endpoint != "" && accessKey != "" && secretKey != "" && bucket != "" {
|
||||
m := map[string]string{
|
||||
"provider": "minio",
|
||||
"endpoint": endpoint,
|
||||
"bucket_name": bucket,
|
||||
"access_key_id": accessKey,
|
||||
"secret_access_key": secretKey,
|
||||
}
|
||||
if sec.MinIO.UseSSL {
|
||||
m["use_ssl"] = "true"
|
||||
}
|
||||
if sec.MinIO.PathPrefix != "" {
|
||||
m["path_prefix"] = sec.MinIO.PathPrefix
|
||||
}
|
||||
return m
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: read from environment variables
|
||||
storageType := strings.TrimSpace(os.Getenv("STORAGE_TYPE"))
|
||||
switch storageType {
|
||||
case "minio":
|
||||
endpoint := os.Getenv("MINIO_ENDPOINT")
|
||||
accessKey := os.Getenv("MINIO_ACCESS_KEY_ID")
|
||||
secretKey := os.Getenv("MINIO_SECRET_ACCESS_KEY")
|
||||
bucket := os.Getenv("MINIO_BUCKET_NAME")
|
||||
if endpoint == "" || accessKey == "" || secretKey == "" || bucket == "" {
|
||||
return nil
|
||||
}
|
||||
m := map[string]string{
|
||||
"provider": "minio",
|
||||
"endpoint": endpoint,
|
||||
"bucket_name": bucket,
|
||||
"access_key_id": accessKey,
|
||||
"secret_access_key": secretKey,
|
||||
}
|
||||
if v := os.Getenv("MINIO_USE_SSL"); v != "" {
|
||||
m["use_ssl"] = v
|
||||
}
|
||||
if v := os.Getenv("MINIO_PATH_PREFIX"); v != "" {
|
||||
m["path_prefix"] = v
|
||||
}
|
||||
return m
|
||||
case "cos":
|
||||
secretID := os.Getenv("COS_SECRET_ID")
|
||||
secretKey := os.Getenv("COS_SECRET_KEY")
|
||||
region := os.Getenv("COS_REGION")
|
||||
bucket := os.Getenv("COS_BUCKET_NAME")
|
||||
if secretID == "" || secretKey == "" || region == "" || bucket == "" {
|
||||
return nil
|
||||
}
|
||||
m := map[string]string{
|
||||
"provider": "cos",
|
||||
"bucket_name": bucket,
|
||||
"access_key_id": secretID,
|
||||
"secret_access_key": secretKey,
|
||||
"region": region,
|
||||
}
|
||||
if v := os.Getenv("COS_APP_ID"); v != "" {
|
||||
m["app_id"] = v
|
||||
}
|
||||
if v := os.Getenv("COS_PATH_PREFIX"); v != "" {
|
||||
m["path_prefix"] = v
|
||||
}
|
||||
return m
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// enqueueImageMultimodalTasks enqueues asynq tasks for multimodal image processing.
|
||||
func (s *knowledgeService) enqueueImageMultimodalTasks(
|
||||
ctx context.Context,
|
||||
@@ -7644,7 +7366,6 @@ func (s *knowledgeService) enqueueImageMultimodalTasks(
|
||||
KnowledgeBaseID: kb.ID,
|
||||
ChunkID: chunkID,
|
||||
ImageURL: img.ServingURL,
|
||||
ImageLocalPath: img.LocalPath,
|
||||
EnableOCR: true,
|
||||
EnableCaption: true,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user