feat: init sproutclaw-web — Go+Gin backend + React frontend
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
42
backend/internal/services/images.go
Normal file
42
backend/internal/services/images.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sproutclaw-web/internal/models"
|
||||
)
|
||||
|
||||
const (
|
||||
maxImages = 8
|
||||
maxImageSize = 10 * 1024 * 1024 // 10 MB
|
||||
)
|
||||
|
||||
var allowedImageTypes = map[string]bool{
|
||||
"image/jpeg": true,
|
||||
"image/png": true,
|
||||
"image/gif": true,
|
||||
"image/webp": true,
|
||||
}
|
||||
|
||||
// ValidateImages checks image count, type, and base64-decoded size.
|
||||
func ValidateImages(images []models.ChatImage) error {
|
||||
if len(images) > maxImages {
|
||||
return fmt.Errorf("最多支持 %d 张图片,当前 %d 张", maxImages, len(images))
|
||||
}
|
||||
for i, img := range images {
|
||||
mt := strings.ToLower(img.MediaType)
|
||||
if !allowedImageTypes[mt] {
|
||||
return fmt.Errorf("图片 %d 类型不支持: %s", i+1, img.MediaType)
|
||||
}
|
||||
decoded, err := base64.StdEncoding.DecodeString(img.Data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("图片 %d base64 解码失败", i+1)
|
||||
}
|
||||
if len(decoded) > maxImageSize {
|
||||
return fmt.Errorf("图片 %d 超过 10MB 限制", i+1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user