Limit file upload size

master
Bel LaPointe 2020-07-24 23:57:24 -06:00
parent c47850647e
commit 8741db40b4
2 changed files with 8 additions and 5 deletions

View File

@ -16,6 +16,7 @@ type Config struct {
FileRoot string FileRoot string
Auth bool Auth bool
AuthLifetime time.Duration AuthLifetime time.Duration
MaxFileSize int64
} }
func New() Config { func New() Config {
@ -35,6 +36,7 @@ func New() Config {
as.Append(args.STRING, "drivertype", "database driver to use", "boltdb") as.Append(args.STRING, "drivertype", "database driver to use", "boltdb")
as.Append(args.BOOL, "auth", "check for authorized access", false) as.Append(args.BOOL, "auth", "check for authorized access", false)
as.Append(args.DURATION, "authlifetime", "duration auth is valid for", time.Hour) as.Append(args.DURATION, "authlifetime", "duration auth is valid for", time.Hour)
as.Append(args.INT, "max-file-size", "max file size for uploads in bytes", 50*(1<<20))
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
@ -49,5 +51,6 @@ func New() Config {
DriverType: as.GetString("drivertype"), DriverType: as.GetString("drivertype"),
Auth: as.GetBool("auth"), Auth: as.GetBool("auth"),
AuthLifetime: as.GetDuration("authlifetime"), AuthLifetime: as.GetDuration("authlifetime"),
MaxFileSize: int64(as.GetInt("max-file-size")),
} }
} }

View File

@ -77,8 +77,7 @@ func filesPostFromDirectLink(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
defer f.Close() defer f.Close()
// TODO max bytes reader _, err = io.Copy(f, io.LimitReader(resp.Body, config.New().MaxFileSize))
_, err = io.Copy(f, resp.Body)
return err return err
} }
@ -97,14 +96,15 @@ func filesPostFromUpload(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
defer f.Close() defer f.Close()
megabyte := 100 << 20 megabyte := 1 << 20
r.ParseMultipartForm(int64(megabyte)) chunkSize := 10 * megabyte
r.ParseMultipartForm(int64(chunkSize))
file, _, err := r.FormFile("file") file, _, err := r.FormFile("file")
if err != nil { if err != nil {
return err return err
} }
defer file.Close() defer file.Close()
if _, err := io.Copy(f, file); err != nil { if _, err := io.Copy(f, io.LimitReader(file, config.New().MaxFileSize)); err != nil {
return err return err
} }
return json.NewEncoder(w).Encode(map[string]interface{}{"status": "ok"}) return json.NewEncoder(w).Encode(map[string]interface{}{"status": "ok"})