123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package imagedata
- import (
- "bytes"
- "context"
- "io"
- "github.com/imgproxy/imgproxy/v3/bufpool"
- "github.com/imgproxy/imgproxy/v3/bufreader"
- "github.com/imgproxy/imgproxy/v3/config"
- "github.com/imgproxy/imgproxy/v3/imagemeta"
- "github.com/imgproxy/imgproxy/v3/security"
- )
- var downloadBufPool *bufpool.Pool
- func initRead() {
- downloadBufPool = bufpool.New("download", config.Workers, config.DownloadBufferSize)
- }
- func readAndCheckImage(r io.Reader, contentLength int, secopts security.Options) (*ImageData, error) {
- if err := security.CheckFileSize(contentLength, secopts); err != nil {
- return nil, err
- }
- buf := downloadBufPool.Get(contentLength, false)
- cancel := func() { downloadBufPool.Put(buf) }
- r = security.LimitFileSize(r, secopts)
- br := bufreader.New(r, buf)
- meta, err := imagemeta.DecodeMeta(br)
- if err != nil {
- buf.Reset()
- cancel()
- return nil, wrapError(err)
- }
- if err = security.CheckDimensions(meta.Width(), meta.Height(), 1, secopts); err != nil {
- buf.Reset()
- cancel()
- return nil, wrapError(err)
- }
- downloadBufPool.GrowBuffer(buf, contentLength)
- if err = br.Flush(); err != nil {
- buf.Reset()
- cancel()
- return nil, wrapError(err)
- }
- return &ImageData{
- Data: buf.Bytes(),
- Type: meta.Format(),
- cancel: cancel,
- }, nil
- }
- func BorrowBuffer() (*bytes.Buffer, context.CancelFunc) {
- buf := downloadBufPool.Get(0, false)
- cancel := func() { downloadBufPool.Put(buf) }
- return buf, cancel
- }
|