package pkg import ( "bytes" ) func DetectImageFormat(data []byte) string { switch { case bytes.HasPrefix(data, []byte{0xFF, 0xD8}): // JPEG return ".jpeg" case bytes.HasPrefix(data, []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}): // PNG return ".png" case bytes.HasPrefix(data, []byte{0x47, 0x49, 0x46, 0x38}): // GIF return ".gif" // 添加其他图片格式的检测逻辑... default: return ".jpg" } } func GetFormat(body []byte) string { buffer := body[:10] // 读取前10个字节通常足够检测大多数图片格式 format := DetectImageFormat(buffer) return format }