feat(image): enhance image resolution handling for data URIs and remote images
- Added functionality to resolve embedded data:image;base64 images in markdown, storing them and replacing references with stable URLs. - Updated the knowledge service to process data URIs before handling remote images, ensuring all images are resolved consistently. - Improved logging for resolved images, providing better insights during manual processing. - Added unit tests to validate the new data URI resolution functionality and ensure reliability.
This commit is contained in:
@@ -2,10 +2,18 @@ package docparser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// createTestPNG generates a minimal PNG image with the given dimensions.
|
||||
@@ -67,3 +75,68 @@ func TestIsIconImage(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// captureSaveBytes implements interfaces.FileService for tests; only SaveBytes records data.
|
||||
type captureSaveBytes struct {
|
||||
saved [][]byte
|
||||
urls []string
|
||||
}
|
||||
|
||||
func (c *captureSaveBytes) CheckConnectivity(context.Context) error { return nil }
|
||||
|
||||
func (c *captureSaveBytes) SaveFile(context.Context, *multipart.FileHeader, uint64, string) (string, error) {
|
||||
return "", fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (c *captureSaveBytes) SaveBytes(_ context.Context, data []byte, _ uint64, fileName string, _ bool) (string, error) {
|
||||
c.saved = append(c.saved, append([]byte(nil), data...))
|
||||
u := "local://test/" + fileName
|
||||
c.urls = append(c.urls, u)
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (c *captureSaveBytes) GetFile(context.Context, string) (io.ReadCloser, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (c *captureSaveBytes) GetFileURL(context.Context, string) (string, error) { return "", nil }
|
||||
|
||||
func (c *captureSaveBytes) DeleteFile(context.Context, string) error { return nil }
|
||||
|
||||
var _ interfaces.FileService = (*captureSaveBytes)(nil)
|
||||
|
||||
func TestResolveDataURIImages(t *testing.T) {
|
||||
png := createTestPNG(200, 150)
|
||||
b64 := base64.StdEncoding.EncodeToString(png)
|
||||
md := "pre  post"
|
||||
svc := &captureSaveBytes{}
|
||||
r := NewImageResolver()
|
||||
out, imgs, err := r.ResolveDataURIImages(context.Background(), md, svc, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(imgs) != 1 {
|
||||
t.Fatalf("got %d images", len(imgs))
|
||||
}
|
||||
if len(svc.saved) != 1 || !bytes.Equal(svc.saved[0], png) {
|
||||
t.Fatal("SaveBytes payload mismatch")
|
||||
}
|
||||
if !strings.Contains(out, "local://test/") || strings.Contains(out, "data:image") {
|
||||
t.Fatalf("markdown: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDataURIImages_CaseInsensitive(t *testing.T) {
|
||||
png := createTestPNG(200, 150)
|
||||
b64 := base64.StdEncoding.EncodeToString(png)
|
||||
md := ""
|
||||
svc := &captureSaveBytes{}
|
||||
r := NewImageResolver()
|
||||
out, imgs, err := r.ResolveDataURIImages(context.Background(), md, svc, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(imgs) != 1 || !strings.Contains(out, "local://test/") {
|
||||
t.Fatalf("imgs=%d out=%q", len(imgs), out)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user