Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat: add image icon detection and filtering functionality
wizardchen45eba325e2e6
diff --git a/internal/infrastructure/docparser/image_resolver.go b/internal/infrastructure/docparser/image_resolver.goindex 540125e76..14f372b7e 100644--- a/internal/infrastructure/docparser/image_resolver.go+++ b/internal/infrastructure/docparser/image_resolver.go@@ -1,7 +1,12 @@package docparserimport (+ "bytes""context"+ "image"+ _ "image/gif"+ _ "image/jpeg"+ _ "image/png""log""path/filepath""regexp"@@ -12,6 +17,30 @@ import ("github.com/google/uuid")+const (+ // minImageDimension is the minimum width/height in pixels; images smaller+ // than this on either axis are treated as icons and filtered out.+ minImageDimension = 128+ // minImageBytes is the minimum file size in bytes; very small images are+ // almost certainly icons or decorative elements.+ minImageBytes = 512 // 512 bytes+)++// isIconImage returns true if the image data looks like a small icon or+// decorative element that should be filtered out. It checks pixel dimensions+// when decodable, and falls back to raw byte size otherwise.+func isIconImage(data []byte) bool {+ cfg, _, err := image.DecodeConfig(bytes.NewReader(data))+ if err != nil {+ // Cannot decode dimensions — fall back to size-only heuristic.+ return len(data) < minImageBytes+ }+ if cfg.Width < minImageDimension || cfg.Height < minImageDimension {+ return true+ }+ return false+}+// StoredImage describes an image that has been saved to storage.type StoredImage struct {OriginalRef string // reference in the original markdown@@ -72,6 +101,13 @@ func (r *ImageResolver) ResolveAndStore(continue}+ // Filter out small icons and decorative images+ if isIconImage(ref.ImageData) {+ // Remove the image reference from markdown entirely+ markdown = markdown[:m[0]] + markdown[m[1]:]+ continue+ }+// Determine extensionext := extFromMime(ref.MimeType)if ext == "" {diff --git a/internal/infrastructure/docparser/image_resolver_test.go b/internal/infrastructure/docparser/image_resolver_test.gonew file mode 100644index 000000000..1c4b827af--- /dev/null+++ b/internal/infrastructure/docparser/image_resolver_test.go@@ -0,0 +1,69 @@+package docparser++import (+ "bytes"+ "image"+ "image/color"+ "image/png"+ "testing"+)++// createTestPNG generates a minimal PNG image with the given dimensions.+func createTestPNG(w, h int) []byte {+ img := image.NewRGBA(image.Rect(0, 0, w, h))+ for y := 0; y < h; y++ {+ for x := 0; x < w; x++ {+ img.Set(x, y, color.RGBA{R: 128, G: 128, B: 128, A: 255})+ }+ }+ var buf bytes.Buffer+ _ = png.Encode(&buf, img)+ return buf.Bytes()+}++func TestIsIconImage(t *testing.T) {+ tests := []struct {+ name string+ data []byte+ expect bool+ }{+ {+ name: "tiny bytes (< 2KB)",+ data: make([]byte, 1024),+ expect: true,+ },+ {+ name: "small icon 32x32",+ data: createTestPNG(32, 32),+ expect: true,+ },+ {+ name: "small icon 48x48",+ data: createTestPNG(48, 48),+ expect: true,+ },+ {+ name: "borderline 64x64",+ data: createTestPNG(64, 64),+ expect: false,+ },+ {+ name: "normal image 200x150",+ data: createTestPNG(200, 150),+ expect: false,+ },+ {+ name: "wide but short 200x30",+ data: createTestPNG(200, 30),+ expect: true,+ },+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := isIconImage(tt.data)+ if got != tt.expect {+ t.Errorf("isIconImage() = %v, want %v (data len=%d)", got, tt.expect, len(tt.data))+ }+ })+ }+}