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

120 lines
2.4 KiB
Go
Raw Normal View History

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() {
transport := &http.Transport{}
if conf.LocalFileSystemRoot != "" {
transport.RegisterProtocol("local", http.NewFileTransport(http.Dir(conf.LocalFileSystemRoot)))
}
2017-11-13 22:28:04 +02:00
downloadClient = &http.Client{
Timeout: time.Duration(conf.DownloadTimeout) * time.Second,
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 {
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)
}