From bbe21185b6dd5adc073afcb692aab70d5ea53bb5 Mon Sep 17 00:00:00 2001 From: Quentin Bramas Date: Tue, 25 May 2021 13:57:46 +0200 Subject: [PATCH] improve (upload): Limit memory usage when parsing multipart form data, so that large files are stored in disk (#382) Co-authored-by: Quentin Bramas --- server/ctrl/files.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/ctrl/files.go b/server/ctrl/files.go index 5b78f30b..ded21276 100644 --- a/server/ctrl/files.go +++ b/server/ctrl/files.go @@ -5,15 +5,16 @@ import ( "encoding/base64" "fmt" "hash/fnv" - . "github.com/mickael-kerjean/filestash/server/common" - "github.com/mickael-kerjean/filestash/server/model" "io" "net/http" "os" "path/filepath" - "strings" "strconv" + "strings" "time" + + . "github.com/mickael-kerjean/filestash/server/common" + "github.com/mickael-kerjean/filestash/server/model" ) type FileInfo struct { @@ -301,6 +302,13 @@ func FileSave(ctx App, res http.ResponseWriter, req *http.Request) { return } + maxMemory := int64(32 << 20) // 32MB + err = req.ParseMultipartForm(maxMemory) + if err != nil { + SendErrorResult(res, err) + return + } + file, _, err := req.FormFile("file") if err != nil { SendErrorResult(res, err)