1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-04-12 06:58:15 +02:00

Check timeout after every save attempt in saveImageToFitBytes

This commit is contained in:
DarthSim 2021-03-10 16:01:24 +06:00
parent 56d3f6d897
commit 53f8b0dbdd

View File

@ -734,12 +734,10 @@ func getIcoData(imgdata *imageData) (*imageData, error) {
return nil, fmt.Errorf("Can't load %s from ICO", meta.Format())
}
func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {
func saveImageToFitBytes(ctx context.Context, po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {
var diff float64
quality := po.getQuality()
img.CopyMemory()
for {
result, cancel, err := img.Save(po.Format, quality)
if len(result) <= po.MaxBytes || quality <= 10 || err != nil {
@ -747,6 +745,8 @@ func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context
}
cancel()
checkTimeout(ctx)
delta := float64(len(result)) / float64(po.MaxBytes)
switch {
case delta > 3:
@ -866,7 +866,7 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
}
if po.MaxBytes > 0 && canFitToBytes(po.Format) {
return saveImageToFitBytes(po, img)
return saveImageToFitBytes(ctx, po, img)
}
return img.Save(po.Format, po.getQuality())