1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00
imgproxy/download.go

131 lines
2.7 KiB
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
2017-07-06 00:47:47 +06:00
"bufio"
2017-06-20 16:58:55 +03:00
"bytes"
"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"
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
)
2017-11-14 02:28:04 +06:00
var downloadClient *http.Client
2017-07-05 18:28:22 +06:00
2017-07-02 02:26:32 +06:00
type netReader struct {
2017-07-06 00:47:47 +06:00
reader *bufio.Reader
2017-07-02 02:26:32 +06:00
buf *bytes.Buffer
}
func newNetReader(r io.Reader) *netReader {
return &netReader{
2017-07-06 00:47:47 +06:00
reader: bufio.NewReader(r),
2017-07-02 02:26:32 +06: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-06 00:47:47 +06:00
func (r *netReader) Peek(n int) ([]byte, error) {
return r.reader.Peek(n)
}
2017-07-02 02:26:32 +06: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 16:58:55 +03:00
2017-07-02 02:26:32 +06:00
func (r *netReader) GrowBuf(s int) {
r.buf.Grow(s)
}
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)))
}
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
}
}
2017-09-27 14:42:49 +06:00
func checkTypeAndDimensions(r io.Reader) (imageType, error) {
imgconf, imgtypeStr, err := image.DecodeConfig(r)
imgtype, imgtypeOk := imageTypes[imgtypeStr]
2017-06-20 16:58:55 +03:00
if err != nil {
return imageTypeUnknown, err
2017-06-20 16:58:55 +03:00
}
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
return imageTypeUnknown, errors.New("Source image is too big")
}
if imgconf.Width*imgconf.Height > conf.MaxSrcResolution {
return imageTypeUnknown, errors.New("Source image is too big")
2017-09-27 14:42:49 +06:00
}
2017-10-07 02:54:24 +06:00
if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
return imageTypeUnknown, errors.New("Source image type not supported")
2017-06-20 16:58:55 +03:00
}
2017-09-27 14:42:49 +06:00
return imgtype, nil
2017-06-20 16:58:55 +03:00
}
2017-09-27 14:42:49 +06:00
func readAndCheckImage(res *http.Response) ([]byte, imageType, error) {
2017-07-02 02:26:32 +06:00
nr := newNetReader(res.Body)
2017-06-20 16:58:55 +03:00
2017-09-27 14:42:49 +06:00
imgtype, err := checkTypeAndDimensions(nr)
if err != nil {
return nil, imageTypeUnknown, err
2017-06-20 16:58:55 +03:00
}
if res.ContentLength > 0 {
2017-07-02 02:26:32 +06:00
nr.GrowBuf(int(res.ContentLength))
2017-06-20 16:58:55 +03:00
}
2017-09-27 14:42:49 +06:00
b, err := nr.ReadAll()
return b, imgtype, err
2017-06-20 16:58:55 +03:00
}
2017-09-27 14:42:49 +06:00
func downloadImage(url string) ([]byte, imageType, error) {
2018-04-26 17:38:40 +06:00
fullURL := fmt.Sprintf("%s%s", conf.BaseURL, url)
res, err := downloadClient.Get(fullURL)
2017-06-20 16:58:55 +03:00
if err != nil {
return nil, imageTypeUnknown, 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)
return nil, imageTypeUnknown, fmt.Errorf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
2017-07-05 18:28:22 +06:00
}
2017-06-20 16:58:55 +03:00
return readAndCheckImage(res)
}