From 48528f3a5617c3e74ecad56197452192c09d9afa Mon Sep 17 00:00:00 2001 From: Lucas Barbieri Date: Mon, 19 Aug 2024 16:39:57 -0300 Subject: [PATCH] feat(): add max file size for upload --- main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index d87af73..791facb 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,9 @@ import ( ) const ( - port = ":8080" - filesDir = "./files" + port = ":8080" + filesDir = "./files" + maxFileSize = 20 * 1024 * 1024 // 20MiB ) var url string = os.Getenv("URL") @@ -41,9 +42,11 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { return } + r.Body = http.MaxBytesReader(w, r.Body, maxFileSize) + file, _, err := r.FormFile("file") if err != nil { - http.Error(w, "Error retrieving the file", http.StatusBadRequest) + http.Error(w, "Error retrieving the file or file size exceeds limit", http.StatusBadRequest) return } defer file.Close()