1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/download.go

203 lines
4.8 KiB
Go
Raw Normal View History

2017-06-20 15:58:55 +02:00
package main
import (
"bytes"
2018-10-05 17:17:36 +02:00
"context"
"crypto/tls"
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"
"net"
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
2018-12-02 15:02:19 +02:00
_ "github.com/mat/besticon/ico"
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
2018-11-20 14:53:44 +02:00
errSourceDimensionsTooBig = newError(422, "Source image dimensions are too big", "Invalid source image")
2019-01-21 12:36:31 +02:00
errSourceResolutionTooBig = newError(422, "Source image resolution is too big", "Invalid source image")
errSourceFileTooBig = newError(422, "Source image file is too big", "Invalid source image")
2018-11-20 14:53:44 +02:00
errSourceImageTypeNotSupported = newError(422, "Source image type not supported", "Invalid source image")
2018-10-05 17:17:36 +02:00
)
2018-11-20 14:53:44 +02:00
const msgSourceImageIsUnreachable = "Source image is unreachable"
var downloadBufPool *bufPool
2017-07-05 14:28:22 +02:00
2019-01-21 12:36:31 +02:00
type limitReader struct {
r io.ReadCloser
left int
}
func (lr *limitReader) Read(p []byte) (n int, err error) {
n, err = lr.r.Read(p)
lr.left = lr.left - n
if err == nil && lr.left < 0 {
err = errSourceFileTooBig
}
return
}
func (lr *limitReader) Close() error {
return lr.r.Close()
}
2017-11-13 22:28:04 +02:00
func initDownloading() {
2018-02-02 14:46:30 +02:00
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: conf.Concurrency,
MaxIdleConnsPerHost: conf.Concurrency,
DisableCompression: true,
Dial: (&net.Dialer{KeepAlive: 600 * time.Second}).Dial,
2018-02-02 14:46:30 +02:00
}
if conf.IgnoreSslVerification {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
if conf.LocalFileSystemRoot != "" {
2019-02-04 16:04:19 +02:00
transport.RegisterProtocol("local", newFsTransport())
}
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 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,
Transport: transport,
2017-11-13 22:28:04 +02:00
}
2019-01-28 18:18:54 +02:00
downloadBufPool = newBufPool("download", conf.Concurrency, conf.DownloadBufferSize)
2017-11-13 22:28:04 +02:00
}
2018-11-08 12:34:21 +02:00
func checkDimensions(width, height int) error {
2018-11-15 15:25:53 +02:00
if conf.MaxSrcDimension > 0 && (width > conf.MaxSrcDimension || height > conf.MaxSrcDimension) {
2018-11-08 12:34:21 +02:00
return errSourceDimensionsTooBig
}
if width*height > conf.MaxSrcResolution {
return errSourceResolutionTooBig
}
return nil
}
2017-09-27 10:42:49 +02:00
func checkTypeAndDimensions(r io.Reader) (imageType, error) {
imgconf, imgtypeStr, err := image.DecodeConfig(r)
2019-01-21 12:36:31 +02:00
if err == image.ErrFormat {
2018-11-20 14:53:44 +02:00
return imageTypeUnknown, errSourceImageTypeNotSupported
2017-06-20 15:58:55 +02:00
}
2019-01-21 12:36:31 +02:00
if err != nil {
return imageTypeUnknown, err
}
2018-11-08 12:34:21 +02:00
imgtype, imgtypeOk := imageTypes[imgtypeStr]
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
2018-11-08 12:34:21 +02:00
if err = checkDimensions(imgconf.Width, imgconf.Height); err != nil {
return imageTypeUnknown, err
}
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) {
2019-01-30 10:36:19 +02:00
var contentLength int
if res.ContentLength > 0 {
contentLength = int(res.ContentLength)
if conf.MaxSrcFileSize > 0 && contentLength > conf.MaxSrcFileSize {
return ctx, func() {}, errSourceFileTooBig
}
2019-01-30 10:36:19 +02:00
}
2019-01-30 12:31:00 +02:00
buf := downloadBufPool.Get(contentLength)
2018-10-05 17:17:36 +02:00
cancel := func() {
2019-01-30 12:31:00 +02:00
downloadBufPool.Put(buf)
2018-10-05 17:17:36 +02:00
}
2019-01-21 12:36:31 +02:00
body := res.Body
if conf.MaxSrcFileSize > 0 {
body = &limitReader{r: body, left: conf.MaxSrcFileSize}
}
imgtype, err := checkTypeAndDimensions(io.TeeReader(body, buf))
2017-09-27 10:42:49 +02:00
if err != nil {
2018-10-05 17:17:36 +02:00
return ctx, cancel, err
2017-06-20 15:58:55 +02:00
}
2019-01-21 12:36:31 +02:00
if _, err = buf.ReadFrom(body); err != nil {
2018-11-20 14:53:44 +02:00
return ctx, cancel, newError(404, err.Error(), msgSourceImageIsUnreachable)
2017-06-20 15:58:55 +02:00
}
2018-11-20 14:53:44 +02:00
ctx = context.WithValue(ctx, imageTypeCtxKey, imgtype)
ctx = context.WithValue(ctx, imageDataCtxKey, buf)
return ctx, cancel, nil
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) {
2018-11-02 17:35:21 +02:00
url := 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)()
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
2018-11-20 14:53:44 +02:00
return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable)
}
req.Header.Set("User-Agent", conf.UserAgent)
res, err := downloadClient.Do(req)
if res != nil {
defer res.Body.Close()
}
2017-06-20 15:58:55 +02:00
if err != nil {
2018-11-20 14:53:44 +02:00
return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable)
2017-06-20 15:58:55 +02:00
}
2017-07-05 14:28:22 +02:00
if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
2018-11-20 14:53:44 +02:00
msg := fmt.Sprintf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
return ctx, func() {}, newError(404, msg, msgSourceImageIsUnreachable)
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
}