1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00

Make the expires option set Expires and Cache-Control headers

This commit is contained in:
DarthSim 2023-02-24 19:44:03 +03:00
parent 41b9cfbc6e
commit 50d04d585e
4 changed files with 18 additions and 4 deletions

View File

@ -5,6 +5,9 @@
- Add [extend_aspect_ratio](https://docs.imgproxy.net/latest/generating_the_url?id=extend-aspect-ratio) processing option. - 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) - (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 ## [3.13.2] - 2023-02-15
### Change ### Change
- Remove color-related EXIF data when stripping ICC profile. - Remove color-related EXIF data when stripping ICC profile.

View File

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

View File

@ -55,10 +55,16 @@ func initProcessingHandler() {
headerVaryValue = strings.Join(vary, ", ") 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 cacheControl, expires string
var ttl int 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 config.CacheControlPassthrough && originHeaders != nil {
if val, ok := originHeaders["Cache-Control"]; ok && len(val) > 0 { if val, ok := originHeaders["Cache-Control"]; ok && len(val) > 0 {
cacheControl = val 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)) rw.Header().Set("Content-DPR", strconv.FormatFloat(po.Dpr, 'f', 2, 32))
} }
setCacheControl(rw, originData.Headers) setCacheControl(rw, po.Expires, originData.Headers)
setVary(rw) setVary(rw)
setCanonical(rw, originURL) 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) { 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) setVary(rw)
rw.WriteHeader(304) rw.WriteHeader(304)

View File

@ -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)) 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"), "Cache-Control": rw.Header().Get("Cache-Control"),
"Expires": rw.Header().Get("Expires"), "Expires": rw.Header().Get("Expires"),
}) })