1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-12 11:46:10 +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" "errors"
) )
var (
errInvalidToken = errors.New("Invalid token")
errInvalidURLEncoding = errors.New("Invalid token encoding")
)
func validatePath(token, path string) error { func validatePath(token, path string) error {
messageMAC, err := base64.RawURLEncoding.DecodeString(token) messageMAC, err := base64.RawURLEncoding.DecodeString(token)
if err != nil { if err != nil {
return errors.New("Invalid token encoding") return errInvalidURLEncoding
} }
mac := hmac.New(sha256.New, conf.Key) mac := hmac.New(sha256.New, conf.Key)
@ -19,7 +24,7 @@ func validatePath(token, path string) error {
expectedMAC := mac.Sum(nil) expectedMAC := mac.Sum(nil)
if !hmac.Equal(messageMAC, expectedMAC) { if !hmac.Equal(messageMAC, expectedMAC) {
return errors.New("Invalid token") return errInvalidToken
} }
return nil return nil

View File

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

View File

@ -26,11 +26,6 @@ func newUnexpectedError(err error, skip int) imgproxyError {
return imgproxyError{500, msg, "Internal error"} 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 { func stacktrace(skip int) string {
callers := make([]uintptr, 10) callers := make([]uintptr, 10)
n := runtime.Callers(skip+1, callers) n := runtime.Callers(skip+1, callers)

View File

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

View File

@ -17,9 +17,13 @@ import (
"unsafe" "unsafe"
) )
var vipsSupportSmartcrop bool var (
var vipsTypeSupportLoad = make(map[imageType]bool) vipsSupportSmartcrop bool
var vipsTypeSupportSave = make(map[imageType]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 { type cConfig struct {
Quality C.int Quality C.int
@ -212,7 +216,7 @@ func processImage(ctx context.Context) ([]byte, error) {
imgtype := getImageType(ctx) imgtype := getImageType(ctx)
if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop { 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) img, err := vipsLoadImage(data, imgtype, 1)

View File

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