1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-07 11:36:25 +02:00

Predefine static errors

This commit is contained in:
DarthSim 2018-10-06 02:29:55 +06:00
parent 85d2738a72
commit 776f57d003
6 changed files with 29 additions and 18 deletions

View File

@ -7,10 +7,15 @@ import (
"errors"
)
var (
errInvalidToken = errors.New("Invalid token")
errInvalidURLEncoding = errors.New("Invalid token encoding")
)
func validatePath(token, path string) error {
messageMAC, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return errors.New("Invalid token encoding")
return errInvalidURLEncoding
}
mac := hmac.New(sha256.New, conf.Key)
@ -19,7 +24,7 @@ func validatePath(token, path string) error {
expectedMAC := mac.Sum(nil)
if !hmac.Equal(messageMAC, expectedMAC) {
return errors.New("Invalid token")
return errInvalidToken
}
return nil

View File

@ -25,6 +25,10 @@ var (
downloadClient *http.Client
imageTypeCtxKey = ctxKey("imageType")
imageDataCtxKey = ctxKey("imageData")
errSourceDimensionsTooBig = errors.New("Source image dimensions are too big")
errSourceResolutionTooBig = errors.New("Source image resolution are too big")
errSourceImageTypeNotSupported = errors.New("Source image type not supported")
)
var downloadBufPool = sync.Pool{
@ -95,13 +99,13 @@ func checkTypeAndDimensions(r io.Reader) (imageType, error) {
return imageTypeUnknown, err
}
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
return imageTypeUnknown, errors.New("Source image is too big")
return imageTypeUnknown, errSourceDimensionsTooBig
}
if imgconf.Width*imgconf.Height > conf.MaxSrcResolution {
return imageTypeUnknown, errors.New("Source image is too big")
return imageTypeUnknown, errSourceResolutionTooBig
}
if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
return imageTypeUnknown, errors.New("Source image type not supported")
return imageTypeUnknown, errSourceImageTypeNotSupported
}
return imgtype, nil

View File

@ -26,11 +26,6 @@ func newUnexpectedError(err error, skip int) imgproxyError {
return imgproxyError{500, msg, "Internal error"}
}
var (
invalidSecretErr = newError(403, "Invalid secret", "Forbidden")
invalidMethodErr = newError(422, "Invalid request method", "Method doesn't allowed")
)
func stacktrace(skip int) string {
callers := make([]uintptr, 10)
n := runtime.Callers(skip+1, callers)

View File

@ -9,7 +9,7 @@ import (
"sync"
)
var notModifiedErr = newError(304, "Not modified", "Not modified")
var errNotModified = newError(304, "Not modified", "Not modified")
type eTagCalc struct {
hash hash.Hash

View File

@ -17,9 +17,13 @@ import (
"unsafe"
)
var vipsSupportSmartcrop bool
var vipsTypeSupportLoad = make(map[imageType]bool)
var vipsTypeSupportSave = make(map[imageType]bool)
var (
vipsSupportSmartcrop bool
vipsTypeSupportLoad = make(map[imageType]bool)
vipsTypeSupportSave = make(map[imageType]bool)
errSmartCropNotSupported = errors.New("Smart crop is not supported by used version of libvips")
)
type cConfig struct {
Quality C.int
@ -212,7 +216,7 @@ func processImage(ctx context.Context) ([]byte, error) {
imgtype := getImageType(ctx)
if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {
return nil, errors.New("Smart crop is not supported by used version of libvips")
return nil, errSmartCropNotSupported
}
img, err := vipsLoadImage(data, imgtype, 1)

View File

@ -25,6 +25,9 @@ var (
healthRequestURI = []byte("/health")
serverMutex mutex
errInvalidMethod = newError(422, "Invalid request method", "Method doesn't allowed")
errInvalidSecret = newError(403, "Invalid secret", "Forbidden")
)
func startServer() *fasthttp.Server {
@ -146,11 +149,11 @@ func serveHTTP(rctx *fasthttp.RequestCtx) {
}
if !rctx.IsGet() {
panic(invalidMethodErr)
panic(errInvalidMethod)
}
if !checkSecret(rctx) {
panic(invalidSecretErr)
panic(errInvalidSecret)
}
serverMutex.Lock()
@ -183,7 +186,7 @@ func serveHTTP(rctx *fasthttp.RequestCtx) {
rctx.Response.Header.SetBytesV("ETag", eTag)
if bytes.Equal(eTag, rctx.Request.Header.Peek("If-None-Match")) {
panic(notModifiedErr)
panic(errNotModified)
}
}