mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
43 lines
771 B
Go
43 lines
771 B
Go
package security
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
|
)
|
|
|
|
var ErrSourceFileTooBig = ierrors.New(422, "Source image file is too big", "Invalid source image")
|
|
|
|
type hardLimitReader struct {
|
|
r io.Reader
|
|
left int
|
|
}
|
|
|
|
func (lr *hardLimitReader) Read(p []byte) (n int, err error) {
|
|
if lr.left <= 0 {
|
|
return 0, ErrSourceFileTooBig
|
|
}
|
|
if len(p) > lr.left {
|
|
p = p[0:lr.left]
|
|
}
|
|
n, err = lr.r.Read(p)
|
|
lr.left -= n
|
|
return
|
|
}
|
|
|
|
func CheckFileSize(size int, opts Options) error {
|
|
if opts.MaxSrcFileSize > 0 && size > opts.MaxSrcFileSize {
|
|
return ErrSourceFileTooBig
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func LimitFileSize(r io.Reader, opts Options) io.Reader {
|
|
if opts.MaxSrcFileSize > 0 {
|
|
return &hardLimitReader{r: r, left: opts.MaxSrcFileSize}
|
|
}
|
|
|
|
return r
|
|
}
|