Kaynağa Gözat

Allow `IMGPROXY_TTL` to be zero

DarthSim 1 yıl önce
ebeveyn
işleme
79e6b32c9c
3 değiştirilmiş dosya ile 14 ekleme ve 6 silme
  1. 1 0
      CHANGELOG.md
  2. 6 2
      config/config.go
  3. 7 4
      processing_handler.go

+ 1 - 0
CHANGELOG.md

@@ -4,6 +4,7 @@
 ### Change
 - Allow relative values for `gravity` and `watermark` offsets.
 - Revised downloading errors reporting.
+- Allow `IMGPROXY_TTL` to be zero.
 
 ## [3.21.0] - 2023-11-23
 ### Add

+ 6 - 2
config/config.go

@@ -641,8 +641,8 @@ func Configure() error {
 		return fmt.Errorf("Max clients number should be greater than or equal 0, now - %d\n", MaxClients)
 	}
 
-	if TTL <= 0 {
-		return fmt.Errorf("TTL should be greater than 0, now - %d\n", TTL)
+	if TTL < 0 {
+		return fmt.Errorf("TTL should be greater than or equal to 0, now - %d\n", TTL)
 	}
 
 	if MaxSrcResolution <= 0 {
@@ -710,6 +710,10 @@ func Configure() error {
 		return errors.New("Watermark opacity should be less than or equal to 1")
 	}
 
+	if FallbackImageTTL < 0 {
+		return fmt.Errorf("Fallback image TTL should be greater than or equal to 0, now - %d\n", TTL)
+	}
+
 	if FallbackImageHTTPCode < 100 || FallbackImageHTTPCode > 599 {
 		return errors.New("Fallback image HTTP code should be between 100 and 599")
 	}

+ 7 - 4
processing_handler.go

@@ -17,6 +17,7 @@ import (
 	"github.com/imgproxy/imgproxy/v3/ierrors"
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
+	"github.com/imgproxy/imgproxy/v3/imath"
 	"github.com/imgproxy/imgproxy/v3/metrics"
 	"github.com/imgproxy/imgproxy/v3/metrics/stats"
 	"github.com/imgproxy/imgproxy/v3/options"
@@ -65,9 +66,7 @@ func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map
 	}
 
 	if force != nil && (ttl < 0 || force.Before(time.Now().Add(time.Duration(ttl)*time.Second))) {
-		rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", int(time.Until(*force).Seconds())))
-		rw.Header().Set("Expires", force.Format(http.TimeFormat))
-		return
+		ttl = imath.Min(config.TTL, imath.Max(0, int(time.Until(*force).Seconds())))
 	}
 
 	if config.CacheControlPassthrough && ttl < 0 && originHeaders != nil {
@@ -83,7 +82,11 @@ func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map
 		if ttl < 0 {
 			ttl = config.TTL
 		}
-		cacheControl = fmt.Sprintf("max-age=%d, public", ttl)
+		if ttl > 0 {
+			cacheControl = fmt.Sprintf("max-age=%d, public", ttl)
+		} else {
+			cacheControl = "no-cache"
+		}
 		expires = time.Now().Add(time.Second * time.Duration(ttl)).Format(http.TimeFormat)
 	}