🩹 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:
parent
2457ee9836
commit
b594df5d9f
1 changed files with 4 additions and 4 deletions
8
list.go
8
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue