mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-10 20:22:31 +02:00
Precheck vips image types support
This commit is contained in:
@@ -67,7 +67,7 @@ func checkTypeAndDimensions(r io.Reader) (imageType, error) {
|
|||||||
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
|
if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
|
||||||
return UNKNOWN, errors.New("File is too big")
|
return UNKNOWN, errors.New("File is too big")
|
||||||
}
|
}
|
||||||
if !imgtypeOk || !vipsTypeSupportedLoad(imgtype) {
|
if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
|
||||||
return UNKNOWN, errors.New("Source image type not supported")
|
return UNKNOWN, errors.New("Source image type not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
51
process.go
51
process.go
@@ -78,6 +78,8 @@ type processingOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var vipsSupportSmartcrop bool
|
var vipsSupportSmartcrop bool
|
||||||
|
var vipsTypeSupportLoad = make(map[imageType]bool)
|
||||||
|
var vipsTypeSupportSave = make(map[imageType]bool)
|
||||||
|
|
||||||
func initVips() {
|
func initVips() {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
@@ -97,6 +99,29 @@ func initVips() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
|
vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
|
||||||
|
|
||||||
|
if int(C.vips_type_find_load_go(C.JPEG)) != 0 {
|
||||||
|
vipsTypeSupportLoad[JPEG] = true
|
||||||
|
}
|
||||||
|
if int(C.vips_type_find_load_go(C.PNG)) != 0 {
|
||||||
|
vipsTypeSupportLoad[PNG] = true
|
||||||
|
}
|
||||||
|
if int(C.vips_type_find_load_go(C.WEBP)) != 0 {
|
||||||
|
vipsTypeSupportLoad[WEBP] = true
|
||||||
|
}
|
||||||
|
if int(C.vips_type_find_load_go(C.GIF)) != 0 {
|
||||||
|
vipsTypeSupportLoad[GIF] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if int(C.vips_type_find_save_go(C.JPEG)) != 0 {
|
||||||
|
vipsTypeSupportSave[JPEG] = true
|
||||||
|
}
|
||||||
|
if int(C.vips_type_find_save_go(C.PNG)) != 0 {
|
||||||
|
vipsTypeSupportSave[PNG] = true
|
||||||
|
}
|
||||||
|
if int(C.vips_type_find_save_go(C.WEBP)) != 0 {
|
||||||
|
vipsTypeSupportSave[WEBP] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomAccessRequired(po processingOptions) int {
|
func randomAccessRequired(po processingOptions) int {
|
||||||
@@ -106,32 +131,6 @@ func randomAccessRequired(po processingOptions) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsTypeSupportedLoad(imgtype imageType) bool {
|
|
||||||
switch imgtype {
|
|
||||||
case JPEG:
|
|
||||||
return int(C.vips_type_find_load_go(C.JPEG)) != 0
|
|
||||||
case PNG:
|
|
||||||
return int(C.vips_type_find_load_go(C.PNG)) != 0
|
|
||||||
case WEBP:
|
|
||||||
return int(C.vips_type_find_load_go(C.WEBP)) != 0
|
|
||||||
case GIF:
|
|
||||||
return int(C.vips_type_find_load_go(C.GIF)) != 0
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func vipsTypeSupportedSave(imgtype imageType) bool {
|
|
||||||
switch imgtype {
|
|
||||||
case JPEG:
|
|
||||||
return int(C.vips_type_find_save_go(C.JPEG)) != 0
|
|
||||||
case PNG:
|
|
||||||
return int(C.vips_type_find_save_go(C.PNG)) != 0
|
|
||||||
case WEBP:
|
|
||||||
return int(C.vips_type_find_save_go(C.WEBP)) != 0
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func round(f float64) int {
|
func round(f float64) int {
|
||||||
return int(f + .5)
|
return int(f + .5)
|
||||||
}
|
}
|
||||||
|
@@ -77,7 +77,7 @@ func parsePath(r *http.Request) (string, processingOptions, error) {
|
|||||||
return "", po, fmt.Errorf("Invalid image format: %s", filenameParts[1])
|
return "", po, fmt.Errorf("Invalid image format: %s", filenameParts[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
if !vipsTypeSupportedSave(po.format) {
|
if !vipsTypeSupportSave[po.format] {
|
||||||
return "", po, errors.New("Resulting image type not supported")
|
return "", po, errors.New("Resulting image type not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user