Merge pull request #3170 from chengzhichao-xydt/codex/base64-encoder-close-clean

fix(agent): close base64 encoder on io.Copy error path
This commit is contained in:
Mauro 2026-06-26 08:40:58 +02:00 committed by GitHub
commit 5feeb91c60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -172,14 +172,22 @@ func encodeImageToDataURL(localPath, mime string, info os.FileInfo, maxSize int)
buf.WriteString(prefix) buf.WriteString(prefix)
encoder := base64.NewEncoder(base64.StdEncoding, &buf) encoder := base64.NewEncoder(base64.StdEncoding, &buf)
if _, err := io.Copy(encoder, f); err != nil { _, copyErr := io.Copy(encoder, f)
closeErr := encoder.Close()
if copyErr != nil {
logger.WarnCF("agent", "Failed to encode media file", map[string]any{ logger.WarnCF("agent", "Failed to encode media file", map[string]any{
"path": localPath, "path": localPath,
"error": err.Error(), "error": copyErr.Error(),
})
return ""
}
if closeErr != nil {
logger.WarnCF("agent", "Failed to close base64 encoder", map[string]any{
"path": localPath,
"error": closeErr.Error(),
}) })
return "" return ""
} }
encoder.Close()
return buf.String() return buf.String()
} }