# HG changeset patch # User Sascha L. Teichmann # Date 1580509057 -3600 # Node ID 6237e6165041b3a7cb6bc02332a723e79800a9b0 # Parent 7cbe5d32a614d3301a61e900db2b999c228b705c * Raise the upload limit for sounding results from 25GB up to 50GB. * Issue an error if sounding results uploads are over the limit instead of silently truncating them. * Fix a file handle leak when storing all uploads. diff -r 7cbe5d32a614 -r 6237e6165041 pkg/controllers/srimports.go --- a/pkg/controllers/srimports.go Fri Jan 31 17:11:30 2020 +0100 +++ b/pkg/controllers/srimports.go Fri Jan 31 23:17:37 2020 +0100 @@ -40,7 +40,7 @@ const ( soundingResultName = "soundingresult" - maxSoundingResultSize = 25 * 1024 * 1024 + maxSoundingResultSize = 50 * 1024 * 1024 ) func fetchSoundingResult(req *http.Request) (string, error) { @@ -63,11 +63,12 @@ return dst, nil } - return misc.StoreUploadedFile( + return misc.StoreUploadedFileCheck( req, soundingResultName, "sr.zip", maxSoundingResultSize, + true, ) } @@ -194,11 +195,12 @@ func uploadSoundingResult(req *http.Request) (jr mw.JSONResult, err error) { var dir string - if dir, err = misc.StoreUploadedFile( + if dir, err = misc.StoreUploadedFileCheck( req, soundingResultName, "sr.zip", maxSoundingResultSize, + true, ); err != nil { return } diff -r 7cbe5d32a614 -r 6237e6165041 pkg/misc/http.go --- a/pkg/misc/http.go Fri Jan 31 17:11:30 2020 +0100 +++ b/pkg/misc/http.go Fri Jan 31 23:17:37 2020 +0100 @@ -15,6 +15,7 @@ import ( "bufio" + "fmt" "io" "io/ioutil" "net/http" @@ -30,7 +31,14 @@ // the given file name. // If the file is long than the given limit maxSize // this function returns an error. -func StoreUploadedFile(req *http.Request, field, fname string, maxSize int64) (string, error) { +func StoreUploadedFile( + req *http.Request, + field, fname string, + maxSize int64) (string, error) { + return StoreUploadedFileCheck(req, field, fname, maxSize, false) +} + +func StoreUploadedFileCheck(req *http.Request, field, fname string, maxSize int64, errorOverMax bool) (string, error) { // Check for direct upload. f, _, err := req.FormFile(field) @@ -52,14 +60,34 @@ out := bufio.NewWriter(o) - if _, err = io.Copy(out, io.LimitReader(f, maxSize)); err != nil { + // Little trick to check if we are over the limit. + size := maxSize + if errorOverMax { + size++ + } + + cleanup := func() { o.Close() os.RemoveAll(dir) + } + + r, err := io.Copy(out, io.LimitReader(f, size)) + if err != nil { + cleanup() return "", err } + if errorOverMax && r > maxSize { + cleanup() + return "", fmt.Errorf("upload exceeded limit of %d bytes", maxSize) + } + if err = out.Flush(); err != nil { - o.Close() + cleanup() + return "", err + } + + if err := o.Close(); err != nil { os.RemoveAll(dir) return "", err }