From d6371fcb680b9eede3f94b53cbe5a5a943d3c53f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E6=99=BA=E8=B6=850668000959?= Date: Thu, 25 Jun 2026 15:23:34 +0800 Subject: [PATCH] fix(agent): close base64 encoder on io.Copy error path Previously when io.Copy to the base64 encoder failed, encoder.Close() was skipped. This left the encoder's internal buffer unflushed. Now always call Close() and handle both copy and close errors explicitly. --- pkg/agent/agent_media.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/agent/agent_media.go b/pkg/agent/agent_media.go index c15e8c163..8915aca99 100644 --- a/pkg/agent/agent_media.go +++ b/pkg/agent/agent_media.go @@ -172,14 +172,22 @@ func encodeImageToDataURL(localPath, mime string, info os.FileInfo, maxSize int) buf.WriteString(prefix) 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{ "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 "" } - encoder.Close() return buf.String() }