Skip to content

Commit 77d7a4e

Browse files
Luis G. Villegaszupzup
Luis G. Villegas
authored andcommitted
Fix file size checking
1 parent dc190f6 commit 77d7a4e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

main.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,27 @@ func main() {
2626

2727
func uploadFileHandler() http.HandlerFunc {
2828
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29-
// validate file size
30-
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
3129
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
32-
renderError(w, "FILE_TOO_BIG", http.StatusBadRequest)
30+
fmt.Printf("Could not parse multipart form: %v\n", err)
31+
renderError(w, "CANT_PARSE_FORM", http.StatusInternalServerError)
3332
return
3433
}
3534

3635
// parse and validate file and post parameters
37-
file, _, err := r.FormFile("uploadFile")
36+
file, fileHeader, err := r.FormFile("uploadfile")
3837
if err != nil {
3938
renderError(w, "INVALID_FILE", http.StatusBadRequest)
4039
return
4140
}
4241
defer file.Close()
42+
// Get and print out file size
43+
fileSize := fileHeader.Size
44+
fmt.Printf("File size (bytes): %v\n", fileSize)
45+
// validate file size
46+
if fileSize > maxUploadSize {
47+
renderError(w, "FILE_TOO_BIG", http.StatusBadRequest)
48+
return
49+
}
4350
fileBytes, err := ioutil.ReadAll(file)
4451
if err != nil {
4552
renderError(w, "INVALID_FILE", http.StatusBadRequest)

0 commit comments

Comments
 (0)