Merge pull request #2773 from zhangxinping666/codex/fix-telegram-svg-media
fix(agent): send SVG attachments as files
This commit is contained in:
commit
0129da1c8e
2 changed files with 83 additions and 1 deletions
|
|
@ -285,6 +285,12 @@ func inferMediaType(filename, contentType string) string {
|
||||||
ct := strings.ToLower(contentType)
|
ct := strings.ToLower(contentType)
|
||||||
fn := strings.ToLower(filename)
|
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/") {
|
if strings.HasPrefix(ct, "image/") {
|
||||||
return "image"
|
return "image"
|
||||||
}
|
}
|
||||||
|
|
@ -298,7 +304,7 @@ func inferMediaType(filename, contentType string) string {
|
||||||
// Fallback: infer from extension
|
// Fallback: infer from extension
|
||||||
ext := filepath.Ext(fn)
|
ext := filepath.Ext(fn)
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".svg":
|
case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp":
|
||||||
return "image"
|
return "image"
|
||||||
case ".mp3", ".wav", ".ogg", ".m4a", ".flac", ".aac", ".wma", ".opus":
|
case ".mp3", ".wav", ".ogg", ".m4a", ".flac", ".aac", ".wma", ".opus":
|
||||||
return "audio"
|
return "audio"
|
||||||
|
|
|
||||||
76
pkg/agent/agent_utils_test.go
Normal file
76
pkg/agent/agent_utils_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue