43 lines
979 B
Go
43 lines
979 B
Go
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
|
|
}
|