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

Better detect image loading errors

This commit is contained in:
DarthSim
2023-09-14 20:07:01 +03:00
parent 7cc77cad13
commit 4c82ec28e2

View File

@@ -11,6 +11,7 @@ import (
"errors"
"math"
"os"
"regexp"
"runtime"
"strings"
"sync"
@@ -49,6 +50,13 @@ var vipsConf struct {
AvifSpeed C.int
}
var badImageErrRe = []*regexp.Regexp{
regexp.MustCompile(`^(\S+)load_buffer: `),
regexp.MustCompile(`^VipsJpeg: `),
regexp.MustCompile(`^tiff2vips: `),
regexp.MustCompile(`^webp2vips: `),
}
func Init() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@@ -188,9 +196,12 @@ func Error() error {
errstr := strings.TrimSpace(C.GoString(C.vips_error_buffer()))
err := ierrors.NewUnexpected(errstr, 1)
if strings.Contains(errstr, "load_buffer: ") {
err.StatusCode = 422
err.PublicMessage = "Broken or unsupported image"
for _, re := range badImageErrRe {
if re.MatchString(errstr) {
err.StatusCode = 422
err.PublicMessage = "Broken or unsupported image"
break
}
}
return err