1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-03 23:19:17 +02:00
Files
imgproxy/download.go

60 lines
1021 B
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
"bytes"
"errors"
"image"
"net/http"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)
const chunkSize = 4096
func checkTypeAndDimensions(b []byte) error {
imgconf, _, err := image.DecodeConfig(bytes.NewReader(b))
if err != nil {
return err
}
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
return errors.New("File is too big")
}
return nil
}
func readAndCheckImage(res *http.Response) ([]byte, error) {
b := make([]byte, chunkSize)
n, err := res.Body.Read(b)
if err != nil {
return nil, err
}
if err = checkTypeAndDimensions(b[:n]); err != nil {
return nil, err
}
buf := bytes.NewBuffer(b[:n])
if res.ContentLength > 0 {
buf.Grow(int(res.ContentLength))
}
if _, err = buf.ReadFrom(res.Body); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func downloadImage(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
return readAndCheckImage(res)
}