1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-02 11:34:20 +02:00
imgproxy/download.go

156 lines
3.4 KiB
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
"bytes"
2018-10-05 21:17:36 +06:00
"context"
"crypto/tls"
2017-06-20 16:58:55 +03:00
"errors"
2017-07-05 18:28:22 +06:00
"fmt"
2017-06-20 16:58:55 +03:00
"image"
2017-07-02 02:26:32 +06:00
"io"
2017-07-05 18:28:22 +06:00
"io/ioutil"
2017-06-20 16:58:55 +03:00
"net/http"
2018-10-05 21:17:36 +06:00
"sync"
2017-07-05 18:28:22 +06:00
"time"
2017-06-20 16:58:55 +03:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-07-06 03:01:35 +06:00
_ "golang.org/x/image/webp"
2017-06-20 16:58:55 +03:00
)
2018-10-05 21:17:36 +06:00
var (
downloadClient *http.Client
imageTypeCtxKey = ctxKey("imageType")
imageDataCtxKey = ctxKey("imageData")
2018-10-06 02:29:55 +06: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-10-05 21:17:36 +06:00
)
var downloadBufPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
2017-07-05 18:28:22 +06:00
2017-11-14 02:28:04 +06:00
func initDownloading() {
2018-02-02 18:46:30 +06:00
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
if conf.IgnoreSslVerification {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
if conf.LocalFileSystemRoot != "" {
transport.RegisterProtocol("local", http.NewFileTransport(http.Dir(conf.LocalFileSystemRoot)))
}
2018-05-26 16:22:41 +02:00
if conf.S3Enabled {
transport.RegisterProtocol("s3", newS3Transport())
2018-05-26 16:22:41 +02:00
}
2018-10-30 18:12:56 +06:00
if len(conf.GCSKey) > 0 {
transport.RegisterProtocol("gs", newGCSTransport())
}
2017-11-14 02:28:04 +06:00
downloadClient = &http.Client{
2018-02-02 18:46:30 +06:00
Timeout: time.Duration(conf.DownloadTimeout) * time.Second,
Transport: transport,
2017-11-14 02:28:04 +06:00
}
}
2018-11-08 16:34:21 +06:00
func checkDimensions(width, height int) error {
if width > conf.MaxSrcDimension || height > conf.MaxSrcDimension {
return errSourceDimensionsTooBig
}
if width*height > conf.MaxSrcResolution {
return errSourceResolutionTooBig
}
return nil
}
2017-09-27 14:42:49 +06:00
func checkTypeAndDimensions(r io.Reader) (imageType, error) {
imgconf, imgtypeStr, err := image.DecodeConfig(r)
2017-06-20 16:58:55 +03:00
if err != nil {
return imageTypeUnknown, err
2017-06-20 16:58:55 +03:00
}
2018-11-08 16:34:21 +06:00
imgtype, imgtypeOk := imageTypes[imgtypeStr]
2017-10-07 02:54:24 +06:00
if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
2018-10-06 02:29:55 +06:00
return imageTypeUnknown, errSourceImageTypeNotSupported
2017-06-20 16:58:55 +03:00
}
2017-09-27 14:42:49 +06:00
2018-11-08 16:34:21 +06:00
if err = checkDimensions(imgconf.Width, imgconf.Height); err != nil {
return imageTypeUnknown, err
}
2017-09-27 14:42:49 +06:00
return imgtype, nil
2017-06-20 16:58:55 +03:00
}
2018-10-05 21:17:36 +06: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)
}
2018-11-08 16:31:55 +06:00
imgtype, err := checkTypeAndDimensions(io.TeeReader(res.Body, buf))
2017-09-27 14:42:49 +06:00
if err != nil {
2018-10-05 21:17:36 +06:00
return ctx, cancel, err
2017-06-20 16:58:55 +03:00
}
2018-11-08 16:31:55 +06:00
if _, err = buf.ReadFrom(res.Body); err == nil {
2018-10-05 21:17:36 +06:00
ctx = context.WithValue(ctx, imageTypeCtxKey, imgtype)
2018-11-08 16:31:55 +06:00
ctx = context.WithValue(ctx, imageDataCtxKey, buf)
2017-06-20 16:58:55 +03:00
}
2018-10-05 21:17:36 +06:00
return ctx, cancel, err
2017-06-20 16:58:55 +03:00
}
2018-10-05 21:17:36 +06:00
func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, error) {
2018-11-02 21:35:21 +06:00
url := getImageURL(ctx)
2018-04-26 17:38:40 +06:00
2018-10-25 19:24:34 +06:00
if newRelicEnabled {
newRelicCancel := startNewRelicSegment(ctx, "Downloading image")
defer newRelicCancel()
}
2018-10-29 18:04:47 +06:00
if prometheusEnabled {
defer startPrometheusDuration(prometheusDownloadDuration)()
}
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 16:58:55 +03:00
if err != nil {
2018-10-05 21:17:36 +06:00
return ctx, func() {}, err
2017-06-20 16:58:55 +03:00
}
defer res.Body.Close()
2017-07-05 18:28:22 +06:00
if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
2018-10-05 21:17:36 +06:00
return ctx, func() {}, fmt.Errorf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
2017-07-05 18:28:22 +06:00
}
2018-10-05 21:17:36 +06: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 16:58:55 +03:00
}