Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(models): 图片解析复用仓储层的 provider scheme 表,对象存储上的图片不再原样交给厂商 (#4405)

linus-liu-web340b72f1419e
diff --git a/internal/models/api/images.go b/internal/models/api/images.go
index 93f9f5b9d..67421efbb 100644
--- a/internal/models/api/images.go
+++ b/internal/models/api/images.go
@@ -9,11 +9,13 @@ import (
"os"
"path/filepath"
"strings"
+
+ "github.com/Tencent/WeKnora/internal/types"
)
// ResolveImageURLForLLM converts stored image paths to a format that LLM APIs can consume.
// - data: URIs and http(s):// URLs are returned as-is.
-// - resource:// and provider-backed local paths are read through the
+// - resource:// references and provider:// paths are read through the
// application resolver and converted to base64 data URIs.
func ResolveImageURLForLLM(imageURL string) string {
if strings.HasPrefix(imageURL, "data:") ||
@@ -50,10 +52,15 @@ func ResolveImageURLForOllama(imageURL string) []byte {
return nil
}
+// isApplicationStoredImage reports whether imageURL names an image the
+// application's storage layer can read: a resource:// reference, or a
+// provider:// URL in the scheme the storage backends write (local://,
+// minio://, s3://, cos://, ...). The provider list is owned by
+// types.ParseProviderScheme, so a backend added there is resolvable here too.
+// Keeping a second, narrower list in this package left "minio://bucket/key"
+// unrecognized and it reached the vendor verbatim.
func isApplicationStoredImage(imageURL string) bool {
- return strings.HasPrefix(imageURL, "resource://") ||
- strings.HasPrefix(imageURL, "local://") ||
- strings.HasPrefix(imageURL, "storage://")
+ return strings.HasPrefix(imageURL, "resource://") || types.ParseProviderScheme(imageURL) != ""
}
// LocalImageResolver resolves a resource:// or provider storage URL to bytes
diff --git a/internal/models/api/images_test.go b/internal/models/api/images_test.go
index 743346a48..232e09d30 100644
--- a/internal/models/api/images_test.go
+++ b/internal/models/api/images_test.go
@@ -1,7 +1,9 @@
package api
import (
+ "bytes"
"fmt"
+ "strings"
"testing"
)
@@ -51,6 +53,83 @@ func TestStripImagesFromMessages_DropsEmptiedMultiContent(t *testing.T) {
}
}
+// A stored image is referenced by the scheme of the backend holding it: an
+// object-storage FileService returns "minio://bucket/key", "s3://bucket/key"
+// and so on, optionally wrapped as "storage://<backendID>/<provider>://...".
+// The predicate has to recognize the storage layer's scheme table, otherwise
+// the handle travels to the vendor verbatim: remote APIs got "minio://..." as
+// the image URL and the Ollama path dropped the picture without a word.
+func TestResolveImageURLForStoredImages(t *testing.T) {
+ pngBytes := []byte("\x89PNG\r\n\x1a\nstored image payload")
+ resolved := map[string]bool{}
+ originalResolver := LocalImageResolver
+ LocalImageResolver = func(storageURL string) ([]byte, bool) {
+ resolved[storageURL] = true
+ return pngBytes, true
+ }
+ t.Cleanup(func() { LocalImageResolver = originalResolver })
+
+ stored := []struct {
+ name string
+ url string
+ }{
+ {name: "local", url: "local://10000/1/img.png"},
+ {name: "minio", url: "minio://bucket/1/img.png"},
+ {name: "s3", url: "s3://bucket/1/img.png"},
+ {name: "cos", url: "cos://bucket/region/1/img.png"},
+ {name: "tos", url: "tos://bucket/1/img.png"},
+ {name: "oss", url: "oss://bucket/1/img.png"},
+ {name: "ks3", url: "ks3://bucket/1/img.png"},
+ {name: "resource reference", url: "resource://a1b2c3/1/img.png"},
+ {name: "backend-wrapped provider", url: "storage://backend-a/cos://bucket/region/1/img.png"},
+ }
+ for _, tc := range stored {
+ t.Run(tc.name, func(t *testing.T) {
+ got := ResolveImageURLForLLM(tc.url)
+ if !strings.HasPrefix(got, "data:image/png;base64,") {
+ t.Fatalf("ResolveImageURLForLLM(%q) = %q, want a base64 PNG data URI", tc.url, got)
+ }
+ if data := ResolveImageURLForOllama(tc.url); !bytes.Equal(data, pngBytes) {
+ t.Fatalf("ResolveImageURLForOllama(%q) = %q, want the stored bytes", tc.url, data)
+ }
+ if !resolved[tc.url] {
+ t.Errorf("resolver was never asked for %q", tc.url)
+ }
+ })
+ }
+}
+
+// A data: or http(s) URL is not application-stored: it must pass through
+// untouched and must never be routed through the storage resolver.
+func TestResolveImageURLForNonStoredURLs(t *testing.T) {
+ originalResolver := LocalImageResolver
+ LocalImageResolver = func(storageURL string) ([]byte, bool) {
+ t.Errorf("resolver called for non-stored URL %q", storageURL)
+ return nil, false
+ }
+ t.Cleanup(func() { LocalImageResolver = originalResolver })
+
+ const dataURI = "data:image/png;base64,cGF5bG9hZA=="
+ const remoteURL = "https://example.com/1/img.png"
+ for _, tc := range []struct {
+ name string
+ url string
+ wantOllama []byte
+ }{
+ {name: "data URI", url: dataURI, wantOllama: []byte("payload")},
+ {name: "http URL", url: remoteURL},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := ResolveImageURLForLLM(tc.url); got != tc.url {
+ t.Errorf("ResolveImageURLForLLM(%q) = %q, want it unchanged", tc.url, got)
+ }
+ if data := ResolveImageURLForOllama(tc.url); !bytes.Equal(data, tc.wantOllama) {
+ t.Errorf("ResolveImageURLForOllama(%q) = %q, want %q", tc.url, data, tc.wantOllama)
+ }
+ })
+ }
+}
+
func TestIsMultimodalNotSupportedError(t *testing.T) {
cases := []struct {
name string