Skip to content

Instantly share code, notes, and snippets.

@sudhirj
Created February 11, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudhirj/b0ce60dec0b03871ed68fe9e50afa733 to your computer and use it in GitHub Desktop.
Save sudhirj/b0ce60dec0b03871ed68fe9e50afa733 to your computer and use it in GitHub Desktop.
func (h HTTPAdapter) CreateImage() httprouter.Handle {
return h.Authenticated(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
MaxSize := 10000000
if r.ContentLength > int64(MaxSize) {
http.Error(w, "FILE TOO BIG", 413)
return
}
imageBytes, err := ioutil.ReadAll(io.LimitReader(r.Body, r.ContentLength))
mustNotBeError(err)
config, _, err := image.DecodeConfig(bytes.NewReader(imageBytes))
mustNotBeError(err)
if config.Width > 9000 || config.Height > 9000 {
http.Error(w, "IMAGE TOO BIG", 413)
return
}
decodedImage, err := imaging.Decode(bytes.NewReader(imageBytes), imaging.AutoOrientation(true))
mustNotBeError(err)
_, err = h.s3Uploader.Upload(&s3manager.UploadInput{
Key: aws.String(img.Key()),
Bucket: aws.String(os.Getenv("IMAGE_SOURCE_BUCKET")),
Body: bytes.NewReader(imageBytes),
})
mustNotBeError(err)
go runtime.GC()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment