From 7be931f113e5e6de1ea3acdab7a3dc0d3a845538 Mon Sep 17 00:00:00 2001 From: nodauf Date: Sun, 16 May 2021 22:42:29 +0200 Subject: [PATCH] Fix panic error when the type was not correct when uploading files --- src/controllers/upload.go | 77 ++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/src/controllers/upload.go b/src/controllers/upload.go index ff64836..4525c83 100644 --- a/src/controllers/upload.go +++ b/src/controllers/upload.go @@ -20,50 +20,53 @@ func UploadFile(w http.ResponseWriter, r *http.Request) { fmt.Println(r.URL.Path) // Maximum upload of 1000 MB files r.ParseMultipartForm(10000000 << 20) + // If the variable files exists + if r.MultipartForm != nil { - // Get handler for filename, size and headers - filesHandler := r.MultipartForm.File["files"] - for _, handler := range filesHandler { - file, err := handler.Open() - if err != nil { - fmt.Println("Error Retrieving the File") - fmt.Println(err) - return - } + // Get handler for filename, size and headers + filesHandler := r.MultipartForm.File["files"] + for _, handler := range filesHandler { + file, err := handler.Open() + if err != nil { + fmt.Println("Error Retrieving the File") + fmt.Println(err) + return + } - defer file.Close() + defer file.Close() - // Create file - dst, err := os.Create(filepath + "/" + handler.Filename) - defer dst.Close() - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + // Create file + dst, err := os.Create(filepath + "/" + handler.Filename) + defer dst.Close() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - // Copy the uploaded file to the created file on the filesystem - if _, err := io.Copy(dst, file); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + // Copy the uploaded file to the created file on the filesystem + if _, err := io.Copy(dst, file); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + f, err := os.Open(filepath) + defer f.Close() + if err != nil { + http.Error(w, "404 Not Found : Error while opening the file.", 404) + return + } } - f, err := os.Open(filepath) - defer f.Close() + data := struct { + Directory string + ServerUA string + }{ + r.URL.Path, + serverUA, + } + err := renderTemplate(w, "upload.tpl", data) if err != nil { - http.Error(w, "404 Not Found : Error while opening the file.", 404) - return + fmt.Print("Error while uploading: ") + fmt.Println(err) } } - data := struct { - Directory string - ServerUA string - }{ - r.URL.Path, - serverUA, - } - err := renderTemplate(w, "upload.tpl", data) - if err != nil { - fmt.Print("Error while uploading: ") - fmt.Println(err) - } }