🩹 Fix: Superfluous WriteHeader in Upload

Redirect was inside the for loop in UploadFile, moved it outside for loop. Also closed file after copy in place of defer keyword.
This commit is contained in:
James Musselman 2024-06-26 12:49:20 -05:00
parent 2457ee9836
commit b594df5d9f
No known key found for this signature in database
GPG key ID: 1DAEFF35ECB5D6DB

View file

@ -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)
}
}