Skip to content

Commit

Permalink
Fix panic error when the type was not correct when uploading files
Browse files Browse the repository at this point in the history
  • Loading branch information
nodauf committed May 16, 2021
1 parent 327b9ed commit 7be931f
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions src/controllers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

0 comments on commit 7be931f

Please sign in to comment.