浏览代码

Make the `expires` option set Expires and Cache-Control headers

DarthSim 2 年之前
父节点
当前提交
50d04d585e
共有 4 个文件被更改,包括 18 次插入4 次删除
  1. 3 0
      CHANGELOG.md
  2. 5 0
      options/processing_options.go
  3. 9 3
      processing_handler.go
  4. 1 1
      stream.go

+ 3 - 0
CHANGELOG.md

@@ -5,6 +5,9 @@
 - Add [extend_aspect_ratio](https://docs.imgproxy.net/latest/generating_the_url?id=extend-aspect-ratio) processing option.
 - (pro) Add [advanced smart crop](https://docs.imgproxy.net/latest/configuration?id=smart-crop)
 
+### Change
+- Make the `expires` processing option set `Expires` and `Cache-Control` headers.
+
 ## [3.13.2] - 2023-02-15
 ### Change
 - Remove color-related EXIF data when stripping ICC profile.

+ 5 - 0
options/processing_options.go

@@ -95,6 +95,8 @@ type ProcessingOptions struct {
 
 	CacheBuster string
 
+	Expires *time.Time
+
 	Watermark WatermarkOptions
 
 	PreferWebP  bool
@@ -826,6 +828,9 @@ func applyExpiresOption(po *ProcessingOptions, args []string) error {
 		return errExpiredURL
 	}
 
+	expires := time.Unix(timestamp, 0)
+	po.Expires = &expires
+
 	return nil
 }
 

+ 9 - 3
processing_handler.go

@@ -55,10 +55,16 @@ func initProcessingHandler() {
 	headerVaryValue = strings.Join(vary, ", ")
 }
 
-func setCacheControl(rw http.ResponseWriter, originHeaders map[string]string) {
+func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map[string]string) {
 	var cacheControl, expires string
 	var ttl int
 
+	if force != nil {
+		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
+	}
+
 	if config.CacheControlPassthrough && originHeaders != nil {
 		if val, ok := originHeaders["Cache-Control"]; ok && len(val) > 0 {
 			cacheControl = val
@@ -115,7 +121,7 @@ func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, sta
 		rw.Header().Set("Content-DPR", strconv.FormatFloat(po.Dpr, 'f', 2, 32))
 	}
 
-	setCacheControl(rw, originData.Headers)
+	setCacheControl(rw, po.Expires, originData.Headers)
 	setVary(rw)
 	setCanonical(rw, originURL)
 
@@ -143,7 +149,7 @@ func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, sta
 }
 
 func respondWithNotModified(reqID string, r *http.Request, rw http.ResponseWriter, po *options.ProcessingOptions, originURL string, originHeaders map[string]string) {
-	setCacheControl(rw, originHeaders)
+	setCacheControl(rw, po.Expires, originHeaders)
 	setVary(rw)
 
 	rw.WriteHeader(304)

+ 1 - 1
stream.go

@@ -113,7 +113,7 @@ func streamOriginImage(ctx context.Context, reqID string, r *http.Request, rw ht
 		rw.Header().Set("Content-Disposition", imagetype.ContentDisposition(filename, ext, po.ReturnAttachment))
 	}
 
-	setCacheControl(rw, map[string]string{
+	setCacheControl(rw, po.Expires, map[string]string{
 		"Cache-Control": rw.Header().Get("Cache-Control"),
 		"Expires":       rw.Header().Get("Expires"),
 	})