I would like to know if there is a way of checking the size and the type before uploading it to the server. I am worried about people trying to upload really large files to slow the server down on purpose.
I only know how to check the size of a file after I have copied it to the server. I don't know how to check the file type. I would like to do it before having to upload 2 GB of data, and then validating the file.
This is what I have so far but this copies the file to the server first which is not what I want.
func userUploadImage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) error {
mpf, mpfh, err := r.FormFile("file")
if err != nil {
return nil
}
defer mpf.Close()
dstFile, err := os.Create(config.UploadDir + "/img/" + mpfh.Filename)
if err != nil {
return err
}
defer dstFile.Close()
size, err := io.Copy(dstFile, mpf)
if err != nil {
return err
}
spew.Dump(size)
return nil
}