2017-06-20 15:58:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"image"
|
2017-07-01 22:26:32 +02:00
|
|
|
"io"
|
2017-06-20 15:58:55 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
)
|
|
|
|
|
2017-07-01 22:26:32 +02:00
|
|
|
type netReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
buf *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNetReader(r io.Reader) *netReader {
|
|
|
|
return &netReader{
|
|
|
|
reader: r,
|
|
|
|
buf: bytes.NewBuffer([]byte{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *netReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.reader.Read(p)
|
|
|
|
if err == nil {
|
|
|
|
r.buf.Write(p[:n])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *netReader) ReadAll() ([]byte, error) {
|
|
|
|
if _, err := r.buf.ReadFrom(r.reader); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return r.buf.Bytes(), nil
|
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-07-01 22:26:32 +02:00
|
|
|
func (r *netReader) GrowBuf(s int) {
|
|
|
|
r.buf.Grow(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkTypeAndDimensions(r io.Reader) error {
|
|
|
|
imgconf, _, err := image.DecodeConfig(r)
|
2017-06-20 15:58:55 +02:00
|
|
|
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) {
|
2017-07-01 22:26:32 +02:00
|
|
|
nr := newNetReader(res.Body)
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-07-01 22:26:32 +02:00
|
|
|
if err := checkTypeAndDimensions(nr); err != nil {
|
2017-06-20 15:58:55 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.ContentLength > 0 {
|
2017-07-01 22:26:32 +02:00
|
|
|
nr.GrowBuf(int(res.ContentLength))
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 22:26:32 +02:00
|
|
|
return nr.ReadAll()
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func downloadImage(url string) ([]byte, error) {
|
|
|
|
res, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
return readAndCheckImage(res)
|
|
|
|
}
|