fix: check Close() error after downloading media file

This commit is contained in:
程智超0668000959
2026-06-07 11:53:24 +08:00
parent 7d2b0c2a4d
commit f7be21bb11
+7 -1
View File
@@ -146,7 +146,6 @@ func DownloadFile(urlStr, filename string, opts DownloadOptions) string {
})
return ""
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
out.Close()
@@ -156,6 +155,13 @@ func DownloadFile(urlStr, filename string, opts DownloadOptions) string {
})
return ""
}
if err := out.Close(); err != nil {
logger.ErrorCF(opts.LoggerPrefix, "Failed to close downloaded file", map[string]any{
"error": err.Error(),
})
os.Remove(localPath)
return ""
}
logger.DebugCF(opts.LoggerPrefix, "File downloaded successfully", map[string]any{
"path": localPath,