You've already forked imgproxy
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:
17
vips/vips.go
17
vips/vips.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user