2017-06-20 15:58:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-05 20:47:47 +02:00
|
|
|
"bufio"
|
2017-06-20 15:58:55 +02:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-07-05 14:28:22 +02:00
|
|
|
"fmt"
|
2017-06-20 15:58:55 +02:00
|
|
|
"image"
|
2017-07-01 22:26:32 +02:00
|
|
|
"io"
|
2017-07-05 14:28:22 +02:00
|
|
|
"io/ioutil"
|
2017-06-20 15:58:55 +02:00
|
|
|
"net/http"
|
2017-07-05 14:28:22 +02:00
|
|
|
"time"
|
2017-06-20 15:58:55 +02:00
|
|
|
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
2017-07-05 23:01:35 +02:00
|
|
|
|
|
|
|
_ "golang.org/x/image/webp"
|
2017-06-20 15:58:55 +02:00
|
|
|
)
|
|
|
|
|
2017-11-13 22:28:04 +02:00
|
|
|
var downloadClient *http.Client
|
2017-07-05 14:28:22 +02:00
|
|
|
|
2017-07-01 22:26:32 +02:00
|
|
|
type netReader struct {
|
2017-07-05 20:47:47 +02:00
|
|
|
reader *bufio.Reader
|
2017-07-01 22:26:32 +02:00
|
|
|
buf *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNetReader(r io.Reader) *netReader {
|
|
|
|
return &netReader{
|
2017-07-05 20:47:47 +02:00
|
|
|
reader: bufio.NewReader(r),
|
2017-07-01 22:26:32 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-05 20:47:47 +02:00
|
|
|
func (r *netReader) Peek(n int) ([]byte, error) {
|
|
|
|
return r.reader.Peek(n)
|
|
|
|
}
|
|
|
|
|
2017-07-01 22:26:32 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:28:04 +02:00
|
|
|
func initDownloading() {
|
2018-02-02 14:46:30 +02:00
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}
|
2017-12-29 21:59:20 +02:00
|
|
|
if conf.LocalFileSystemRoot != "" {
|
|
|
|
transport.RegisterProtocol("local", http.NewFileTransport(http.Dir(conf.LocalFileSystemRoot)))
|
|
|
|
}
|
2017-11-13 22:28:04 +02:00
|
|
|
downloadClient = &http.Client{
|
2018-02-02 14:46:30 +02:00
|
|
|
Timeout: time.Duration(conf.DownloadTimeout) * time.Second,
|
2017-12-29 21:59:20 +02:00
|
|
|
Transport: transport,
|
2017-11-13 22:28:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
func checkTypeAndDimensions(r io.Reader) (imageType, error) {
|
|
|
|
imgconf, imgtypeStr, err := image.DecodeConfig(r)
|
|
|
|
imgtype, imgtypeOk := imageTypes[imgtypeStr]
|
|
|
|
|
2017-06-20 15:58:55 +02:00
|
|
|
if err != nil {
|
2017-09-27 10:42:49 +02:00
|
|
|
return UNKNOWN, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
|
2017-10-07 01:00:31 +02:00
|
|
|
return UNKNOWN, errors.New("Source image is too big")
|
|
|
|
}
|
|
|
|
if imgconf.Width*imgconf.Height > conf.MaxSrcResolution {
|
|
|
|
return UNKNOWN, errors.New("Source image is too big")
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
2017-10-06 22:54:24 +02:00
|
|
|
if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
|
2017-09-27 10:42:49 +02:00
|
|
|
return UNKNOWN, errors.New("Source image type not supported")
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
2017-09-27 10:42:49 +02:00
|
|
|
|
|
|
|
return imgtype, nil
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
func readAndCheckImage(res *http.Response) ([]byte, imageType, error) {
|
2017-07-01 22:26:32 +02:00
|
|
|
nr := newNetReader(res.Body)
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
imgtype, err := checkTypeAndDimensions(nr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, UNKNOWN, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-27 10:42:49 +02:00
|
|
|
b, err := nr.ReadAll()
|
|
|
|
|
|
|
|
return b, imgtype, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
func downloadImage(url string) ([]byte, imageType, error) {
|
2017-07-05 14:28:22 +02:00
|
|
|
res, err := downloadClient.Get(url)
|
2017-06-20 15:58:55 +02:00
|
|
|
if err != nil {
|
2017-09-27 10:42:49 +02:00
|
|
|
return nil, UNKNOWN, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2017-07-05 14:28:22 +02:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
2017-09-27 10:42:49 +02:00
|
|
|
return nil, UNKNOWN, fmt.Errorf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
|
2017-07-05 14:28:22 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 15:58:55 +02:00
|
|
|
return readAndCheckImage(res)
|
|
|
|
}
|