From b594df5d9f9a45c92b18f6de0bfe35d4823c5611 Mon Sep 17 00:00:00 2001 From: Musselman Date: Wed, 26 Jun 2024 12:49:20 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix:=20Superfluous=20WriteHeader?= =?UTF-8?q?=20in=20Upload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Redirect was inside the for loop in UploadFile, moved it outside for loop. Also closed file after copy in place of defer keyword. --- list.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/list.go b/list.go index 3b55d0a..db3f3de 100644 --- a/list.go +++ b/list.go @@ -122,8 +122,8 @@ func ListFiles(w http.ResponseWriter, r *http.Request) { data := struct { Username string - Files []File SuccessMessage string + Files []File }{ Username: name, Files: files, @@ -189,7 +189,6 @@ func UploadFile(w http.ResponseWriter, r *http.Request) { http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } - defer destFile.Close() _, err = io.Copy(destFile, part) if err != nil { @@ -197,18 +196,19 @@ func UploadFile(w http.ResponseWriter, r *http.Request) { http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } + destFile.Close() // Insert the file record into the database with creation time currentTime := time.Now().Unix() err = insertFileIntoDatabase(userSession.username, filename, currentTime, currentTime, currentTime) if err != nil { log.Printf("Error inserting file into database: %v", err) - http.Error(w, "Internal Server Error", http.StatusInternalServerError) + http.Error(w, "Internal Server Error when inserting file into database", http.StatusInternalServerError) return } - http.Redirect(w, r, "/home?success=1", http.StatusSeeOther) } + http.Redirect(w, r, "/home?success=1", http.StatusSeeOther) } }