improve file handling in bug report route to ensure proper closure and error logging

This commit is contained in:
Flavio Fois
2026-03-18 10:54:19 +01:00
parent 42623a47bd
commit 210eefe4ce

View File

@@ -10,6 +10,7 @@ import (
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"strings"
"text/template"
@@ -92,7 +93,12 @@ func CreateBugReport(db *sqlx.DB) http.HandlerFunc {
if err != nil {
continue
}
defer file.Close()
defer func(file multipart.File) {
err := file.Close()
if err != nil {
log.Fatalf("closing uploaded file failed: %v", err)
}
}(file)
data, err := io.ReadAll(file)
if err != nil {
@@ -272,7 +278,11 @@ func GetBugReportZipById(db *sqlx.DB) http.HandlerFunc {
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"report-%d.zip\"", report.ID))
w.Write(buf.Bytes())
_, err = w.Write(buf.Bytes())
if err != nil {
return
}
}
}