fix(agent): send SVG attachments as files

This commit is contained in:
xp
2026-05-05 19:36:09 +08:00
parent 5745957429
commit d601b75268
2 changed files with 83 additions and 1 deletions
+7 -1
View File
@@ -285,6 +285,12 @@ func inferMediaType(filename, contentType string) string {
ct := strings.ToLower(contentType)
fn := strings.ToLower(filename)
// SVG is an image MIME type, but raster-only delivery endpoints such as
// Telegram SendPhoto reject it. Treat it as a file/document instead.
if strings.HasPrefix(ct, "image/svg") || filepath.Ext(fn) == ".svg" {
return "file"
}
if strings.HasPrefix(ct, "image/") {
return "image"
}
@@ -298,7 +304,7 @@ func inferMediaType(filename, contentType string) string {
// Fallback: infer from extension
ext := filepath.Ext(fn)
switch ext {
case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".svg":
case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp":
return "image"
case ".mp3", ".wav", ".ogg", ".m4a", ".flac", ".aac", ".wma", ".opus":
return "audio"
+76
View File
@@ -0,0 +1,76 @@
package agent
import "testing"
func TestInferMediaType(t *testing.T) {
tests := []struct {
name string
filename string
contentType string
want string
}{
{
name: "png content type",
filename: "diagram",
contentType: "image/png",
want: "image",
},
{
name: "jpeg extension fallback",
filename: "photo.JPG",
contentType: "",
want: "image",
},
{
name: "svg content type is file",
filename: "diagram",
contentType: "image/svg+xml",
want: "file",
},
{
name: "svg content type with parameters is file",
filename: "diagram",
contentType: "image/svg+xml; charset=utf-8",
want: "file",
},
{
name: "svg extension fallback is file",
filename: "diagram.SVG",
contentType: "",
want: "file",
},
{
name: "audio content type",
filename: "voice",
contentType: "audio/ogg",
want: "audio",
},
{
name: "ogg application content type",
filename: "voice.ogg",
contentType: "application/ogg",
want: "audio",
},
{
name: "video extension fallback",
filename: "clip.MP4",
contentType: "",
want: "video",
},
{
name: "unknown type",
filename: "archive.bin",
contentType: "application/octet-stream",
want: "file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := inferMediaType(tt.filename, tt.contentType)
if got != tt.want {
t.Fatalf("inferMediaType(%q, %q) = %q, want %q", tt.filename, tt.contentType, got, tt.want)
}
})
}
}