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:
wizardchen
2026-03-03 21:30:27 +08:00
committed by lyingbug
parent 894d2c060a
commit 4192cfd072
26 changed files with 835 additions and 897 deletions
@@ -120,7 +120,6 @@ func (p *GRPCDocumentReader) Read(ctx context.Context, req *types.ReadRequest) (
Config: &proto.ReadConfig{
ParserEngine: req.ParserEngine,
ParserEngineOverrides: req.ParserEngineOverrides,
ImageStorage: req.ImageStorage,
},
}
@@ -25,7 +25,6 @@ const (
type httpReadConfig struct {
ParserEngine string `json:"parser_engine,omitempty"`
ParserEngineOverrides map[string]string `json:"parser_engine_overrides,omitempty"`
ImageStorage map[string]string `json:"image_storage,omitempty"`
}
type httpReadRequest struct {
@@ -197,7 +196,6 @@ func (p *HTTPDocumentReader) Read(ctx context.Context, req *types.ReadRequest) (
Config: &httpReadConfig{
ParserEngine: req.ParserEngine,
ParserEngineOverrides: req.ParserEngineOverrides,
ImageStorage: req.ImageStorage,
},
}
if len(req.FileContent) > 0 {
@@ -1,63 +1,50 @@
package docparser
import (
"fmt"
"os"
"context"
"log"
"path/filepath"
"regexp"
"strings"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/utils"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/google/uuid"
)
// StoredImage describes an image that has been saved to local storage.
// StoredImage describes an image that has been saved to storage.
type StoredImage struct {
OriginalRef string // reference in the original markdown
LocalPath string // absolute path on disk (/data/files/images/xxx.png)
ServingURL string // URL for frontend access (/files/images/xxx.png)
ServingURL string // provider:// URL (e.g. local://images/xxx.png, minio://bucket/key)
MimeType string
}
// ImageResolver reads images from a DocReader ReadResult (temp dir or shared storage)
// and saves them to the local file-serving directory, replacing markdown references.
// ImageResolver reads images from a DocReader ReadResult (inline bytes only)
// and saves them via FileService, replacing markdown references with unified URLs.
type ImageResolver struct {
// LocalStorageDir is where images are persisted (e.g. /data/files)
LocalStorageDir string
// URLPrefix is the HTTP path prefix for serving (e.g. /files)
URLPrefix string
// TenantID for storage path namespacing
TenantID uint64
}
// NewImageResolver creates a resolver with defaults from environment.
func NewImageResolver(localStorageDir, urlPrefix string) *ImageResolver {
if localStorageDir == "" {
localStorageDir = "/data/files"
}
if urlPrefix == "" {
urlPrefix = "/files"
}
return &ImageResolver{
LocalStorageDir: localStorageDir,
URLPrefix: urlPrefix,
}
// NewImageResolver creates a resolver.
func NewImageResolver() *ImageResolver {
return &ImageResolver{}
}
// ResolveAndStore reads images from the convert result and persists them.
// It returns the updated markdown with replaced image refs and a list of stored images.
// ResolveAndStore reads images from the convert result, persists them via fileSvc,
// and replaces markdown references with provider:// URLs.
// It returns the updated markdown and a list of stored images.
func (r *ImageResolver) ResolveAndStore(
ctx context.Context,
result *types.ReadResult,
fileSvc interfaces.FileService,
tenantID uint64,
) (updatedMarkdown string, images []StoredImage, err error) {
markdown := result.MarkdownContent
if len(result.ImageRefs) == 0 && result.ImageDirPath == "" {
if len(result.ImageRefs) == 0 {
return markdown, nil, nil
}
imageDir := filepath.Join(r.LocalStorageDir, "images")
if err := os.MkdirAll(imageDir, 0o755); err != nil {
return markdown, nil, fmt.Errorf("create image dir: %w", err)
}
// Build a map of original_ref -> image ref for fast lookup
refMap := make(map[string]types.ImageRef)
for _, ref := range result.ImageRefs {
@@ -73,41 +60,39 @@ func (r *ImageResolver) ResolveAndStore(
m := matches[i]
refPath := markdown[m[4]:m[5]] // group 2: the URL/path
// Skip already-resolved URLs
if strings.HasPrefix(refPath, "http://") || strings.HasPrefix(refPath, "https://") || strings.HasPrefix(refPath, "/files/") {
// Skip already-resolved URLs (http/https, unified /files/, or provider:// scheme)
if strings.HasPrefix(refPath, "http://") || strings.HasPrefix(refPath, "https://") ||
isProviderScheme(refPath) {
continue
}
// Try to find the image data
imgBytes, mimeType, err := r.readImageBytes(result, refPath, refMap)
if err != nil || imgBytes == nil {
// Find inline image bytes from the result
ref, found := refMap[refPath]
if !found || len(ref.ImageData) == 0 {
continue
}
// Determine extension
ext := extFromMime(mimeType)
ext := extFromMime(ref.MimeType)
if ext == "" {
ext = filepath.Ext(refPath)
ext = filepath.Ext(ref.Filename)
}
if ext == "" {
ext = ".png"
}
// Save to local storage
newName := uuid.New().String() + ext
destPath := filepath.Join(imageDir, newName)
if writeErr := os.WriteFile(destPath, imgBytes, 0o644); writeErr != nil {
logger.Printf("WARN: failed to save image %s: %v", refPath, writeErr)
// Save via FileService — returns provider:// path
fileName := uuid.New().String() + ext
servingURL, saveErr := fileSvc.SaveBytes(ctx, ref.ImageData, tenantID, fileName, false)
if saveErr != nil {
log.Printf("WARN: failed to save image %s: %v", refPath, saveErr)
continue
}
servingURL := r.URLPrefix + "/images/" + newName
images = append(images, StoredImage{
OriginalRef: refPath,
LocalPath: destPath,
ServingURL: servingURL,
MimeType: mimeType,
MimeType: ref.MimeType,
})
// Replace in markdown
@@ -117,67 +102,6 @@ func (r *ImageResolver) ResolveAndStore(
return markdown, images, nil
}
// readImageBytes resolves image data with cascading fallback:
// - Mode A: shared temp directory (same-machine / Docker volume)
// - Mode C: inline bytes carried in gRPC response (universal, cross-machine)
// - Mode B: shared object storage via storage_key (future)
func (r *ImageResolver) readImageBytes(
result *types.ReadResult,
refPath string,
refMap map[string]types.ImageRef,
) ([]byte, string, error) {
ref, found := refMap[refPath]
// Mode A: read from shared temp directory
if result.ImageDirPath != "" {
candidates := []string{
filepath.Join(result.ImageDirPath, "images", filepath.Base(refPath)),
filepath.Join(result.ImageDirPath, refPath),
}
for _, candidate := range candidates {
data, err := os.ReadFile(candidate)
if err == nil {
mime := "application/octet-stream"
if found {
mime = ref.MimeType
}
if mime == "" {
mime = mimeFromExt(filepath.Ext(candidate))
}
return data, mime, nil
}
}
}
// Mode C: inline image bytes from gRPC/HTTP response
if found && len(ref.ImageData) > 0 {
mime := ref.MimeType
if mime == "" {
mime = mimeFromExt(filepath.Ext(ref.Filename))
}
return ref.ImageData, mime, nil
}
// Mode B: shared object storage — storage_key is a download URL returned by storage.upload_bytes()
if found && ref.StorageKey != "" {
data, err := downloadImage(ref.StorageKey)
if err != nil {
return nil, "", fmt.Errorf("download image from storage %s: %w", ref.StorageKey, err)
}
mime := ref.MimeType
if mime == "" {
mime = mimeFromExt(filepath.Ext(ref.Filename))
}
return data, mime, nil
}
return nil, "", fmt.Errorf("image not found: %s", refPath)
}
func downloadImage(url string) ([]byte, error) {
return utils.DownloadBytes(url)
}
func extFromMime(mime string) string {
switch mime {
case "image/png":
@@ -195,19 +119,12 @@ func extFromMime(mime string) string {
}
}
func mimeFromExt(ext string) string {
switch strings.ToLower(ext) {
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
case ".bmp":
return "image/bmp"
default:
return "application/octet-stream"
// isProviderScheme checks if the path uses a provider:// scheme (local://, minio://, cos://, tos://).
func isProviderScheme(path string) bool {
for _, prefix := range []string{"local://", "minio://", "cos://", "tos://"} {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}