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"
|
2018-10-05 17:17:36 +02:00
|
|
|
"context"
|
2018-09-26 15:00:37 +02:00
|
|
|
"crypto/tls"
|
2017-06-20 15:58:55 +02:00
|
|
|
"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"
|
2018-10-05 17:17:36 +02:00
|
|
|
"sync"
|
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
|
|
|
)
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
var (
|
|
|
|
downloadClient *http.Client
|
|
|
|
imageTypeCtxKey = ctxKey("imageType")
|
|
|
|
imageDataCtxKey = ctxKey("imageData")
|
2018-10-05 22:29:55 +02:00
|
|
|
|
|
|
|
errSourceDimensionsTooBig = errors.New("Source image dimensions are too big")
|
|
|
|
errSourceResolutionTooBig = errors.New("Source image resolution are too big")
|
|
|
|
errSourceImageTypeNotSupported = errors.New("Source image type not supported")
|
2018-11-02 14:02:33 +02:00
|
|
|
errInvalidImageURL = errors.New("Invalid image url")
|
2018-10-05 17:17:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var downloadBufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
func newNetReader(r io.Reader, buf *bytes.Buffer) *netReader {
|
2017-07-01 22:26:32 +02:00
|
|
|
return &netReader{
|
2017-07-05 20:47:47 +02:00
|
|
|
reader: bufio.NewReader(r),
|
2018-10-05 17:17:36 +02:00
|
|
|
buf: buf,
|
2017-07-01 22:26:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
func (r *netReader) ReadAll() error {
|
2017-07-01 22:26:32 +02:00
|
|
|
if _, err := r.buf.ReadFrom(r.reader); err != nil {
|
2018-10-05 17:17:36 +02:00
|
|
|
return err
|
2017-07-01 22:26:32 +02:00
|
|
|
}
|
2018-10-05 17:17:36 +02:00
|
|
|
return nil
|
2017-07-01 22:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-13 22:28:04 +02:00
|
|
|
func initDownloading() {
|
2018-02-02 14:46:30 +02:00
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}
|
2018-09-26 15:00:37 +02:00
|
|
|
|
|
|
|
if conf.IgnoreSslVerification {
|
|
|
|
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
|
2017-12-29 21:59:20 +02:00
|
|
|
if conf.LocalFileSystemRoot != "" {
|
|
|
|
transport.RegisterProtocol("local", http.NewFileTransport(http.Dir(conf.LocalFileSystemRoot)))
|
|
|
|
}
|
2018-09-26 15:00:37 +02:00
|
|
|
|
2018-05-26 16:22:41 +02:00
|
|
|
if conf.S3Enabled {
|
2018-10-04 17:12:51 +02:00
|
|
|
transport.RegisterProtocol("s3", newS3Transport())
|
2018-05-26 16:22:41 +02:00
|
|
|
}
|
2018-10-04 17:12:51 +02:00
|
|
|
|
2018-10-30 14:12:56 +02:00
|
|
|
if len(conf.GCSKey) > 0 {
|
|
|
|
transport.RegisterProtocol("gs", newGCSTransport())
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-07 16:45:57 +02:00
|
|
|
return imageTypeUnknown, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
|
2018-10-05 22:29:55 +02:00
|
|
|
return imageTypeUnknown, errSourceDimensionsTooBig
|
2017-10-07 01:00:31 +02:00
|
|
|
}
|
|
|
|
if imgconf.Width*imgconf.Height > conf.MaxSrcResolution {
|
2018-10-05 22:29:55 +02:00
|
|
|
return imageTypeUnknown, errSourceResolutionTooBig
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
2017-10-06 22:54:24 +02:00
|
|
|
if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
|
2018-10-05 22:29:55 +02:00
|
|
|
return imageTypeUnknown, errSourceImageTypeNotSupported
|
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
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
func readAndCheckImage(ctx context.Context, res *http.Response) (context.Context, context.CancelFunc, error) {
|
|
|
|
buf := downloadBufPool.Get().(*bytes.Buffer)
|
|
|
|
cancel := func() {
|
|
|
|
buf.Reset()
|
|
|
|
downloadBufPool.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
nr := newNetReader(res.Body, buf)
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
imgtype, err := checkTypeAndDimensions(nr)
|
|
|
|
if err != nil {
|
2018-10-05 17:17:36 +02:00
|
|
|
return ctx, cancel, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
if err = nr.ReadAll(); err == nil {
|
|
|
|
ctx = context.WithValue(ctx, imageTypeCtxKey, imgtype)
|
|
|
|
ctx = context.WithValue(ctx, imageDataCtxKey, nr.buf)
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
return ctx, cancel, err
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, error) {
|
|
|
|
url := fmt.Sprintf("%s%s", conf.BaseURL, getImageURL(ctx))
|
2018-04-26 13:38:40 +02:00
|
|
|
|
2018-10-25 15:24:34 +02:00
|
|
|
if newRelicEnabled {
|
|
|
|
newRelicCancel := startNewRelicSegment(ctx, "Downloading image")
|
|
|
|
defer newRelicCancel()
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:04:47 +02:00
|
|
|
if prometheusEnabled {
|
|
|
|
defer startPrometheusDuration(prometheusDownloadDuration)()
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:37:41 +02:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, func() {}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("User-Agent", conf.UserAgent)
|
|
|
|
|
|
|
|
res, err := downloadClient.Do(req)
|
2017-06-20 15:58:55 +02:00
|
|
|
if err != nil {
|
2018-10-05 17:17:36 +02:00
|
|
|
return ctx, func() {}, 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)
|
2018-10-05 17:17:36 +02:00
|
|
|
return ctx, func() {}, fmt.Errorf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
|
2017-07-05 14:28:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
return readAndCheckImage(ctx, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getImageType(ctx context.Context) imageType {
|
|
|
|
return ctx.Value(imageTypeCtxKey).(imageType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getImageData(ctx context.Context) *bytes.Buffer {
|
|
|
|
return ctx.Value(imageDataCtxKey).(*bytes.Buffer)
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|