feat: enhance image decoding logic to handle base64 file types and improve error handling
This commit is contained in:
@@ -120,11 +120,12 @@ func getImageToken(info *relaycommon.RelayInfo, imageUrl *dto.MessageImageUrl, m
|
|||||||
var config image.Config
|
var config image.Config
|
||||||
var err error
|
var err error
|
||||||
var format string
|
var format string
|
||||||
|
var b64str string
|
||||||
if strings.HasPrefix(imageUrl.Url, "http") {
|
if strings.HasPrefix(imageUrl.Url, "http") {
|
||||||
config, format, err = DecodeUrlImageData(imageUrl.Url)
|
config, format, err = DecodeUrlImageData(imageUrl.Url)
|
||||||
} else {
|
} else {
|
||||||
common.SysLog(fmt.Sprintf("decoding image"))
|
common.SysLog(fmt.Sprintf("decoding image"))
|
||||||
config, format, _, err = DecodeBase64ImageData(imageUrl.Url)
|
config, format, b64str, err = DecodeBase64ImageData(imageUrl.Url)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -132,7 +133,12 @@ func getImageToken(info *relaycommon.RelayInfo, imageUrl *dto.MessageImageUrl, m
|
|||||||
imageUrl.MimeType = format
|
imageUrl.MimeType = format
|
||||||
|
|
||||||
if config.Width == 0 || config.Height == 0 {
|
if config.Width == 0 || config.Height == 0 {
|
||||||
return 0, errors.New(fmt.Sprintf("fail to decode image config: %s", imageUrl.Url))
|
// not an image
|
||||||
|
if format != "" && b64str != "" {
|
||||||
|
// file type
|
||||||
|
return 3 * baseTokens, nil
|
||||||
|
}
|
||||||
|
return 0, errors.New(fmt.Sprintf("fail to decode base64 config: %s", imageUrl.Url))
|
||||||
}
|
}
|
||||||
|
|
||||||
shortSide := config.Width
|
shortSide := config.Width
|
||||||
|
|||||||
Reference in New Issue
Block a user