From 08dc814e98655b1622b4e257ff5dc12fc580eb26 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Thu, 13 Aug 2020 19:52:35 +0600 Subject: [PATCH] Ability to skip processing of some formats --- CHANGELOG.md | 1 + config.go | 21 +++++++++++++++++++++ docs/configuration.md | 10 ++++++++++ processing_handler.go | 15 +++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84069f3c..3585b0bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] ### Added +- Ability to skip processing of some formats. See [Skip processing](https://docs.imgproxy.net/#/configuration?id=skip-processing). - (pro) [video_thumbnail_second](https://docs.imgproxy.net/#/generating_the_url_advanced?id=video-thumbnail-second) processing option. - (pro) [background_alpha](https://docs.imgproxy.net/#/generating_the_url_advanced?id=background-alpha) processing option. diff --git a/config.go b/config.go index baf6f882..552f1996 100644 --- a/config.go +++ b/config.go @@ -58,6 +58,23 @@ func boolEnvConfig(b *bool, name string) { } } +func imageTypesEnvConfig(it *[]imageType, name string) { + *it = []imageType{} + + if env := os.Getenv(name); len(env) > 0 { + parts := strings.Split(env, ",") + + for _, p := range parts { + pt := strings.TrimSpace(p) + if t, ok := imageTypes[pt]; ok { + *it = append(*it, t) + } else { + logWarning("Unknown image format to skip: %s", pt) + } + } + } +} + func hexEnvConfig(b *[]securityKey, name string) error { var err error @@ -187,6 +204,8 @@ type config struct { EnforceWebp bool EnableClientHints bool + SkipProcessingFormats []imageType + UseLinearColorspace bool DisableShrinkOnLoad bool @@ -329,6 +348,8 @@ func configure() error { boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP") boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS") + imageTypesEnvConfig(&conf.SkipProcessingFormats, "IMGPROXY_SKIP_PROCESSING_FORMATS") + boolEnvConfig(&conf.UseLinearColorspace, "IMGPROXY_USE_LINEAR_COLORSPACE") boolEnvConfig(&conf.DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD") diff --git a/docs/configuration.md b/docs/configuration.md index 8376fbe6..241a276d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -174,6 +174,16 @@ You can set up a fallback image that will be used in case imgproxy can't fetch t * `IMGPROXY_FALLBACK_IMAGE_PATH`: path to the locally stored image; * `IMGPROXY_FALLBACK_IMAGE_URL`: fallback image URL. +## Skip processing + +You can configure imgproxy to skip processing of some formats: + +* `IMGPROXY_SKIP_PROCESSING_FORMATS`: list of formats that imgproxy shouldn't process, comma-divided. + +**📝Note:** Processing can be skipped only when the requested format is the same as the source format. + +**📝Note:** Video thumbnails processing can't be skipped. + ## Presets Read about imgproxy presets in the [Presets](presets.md) guide. diff --git a/processing_handler.go b/processing_handler.go index ca06e583..27686195 100644 --- a/processing_handler.go +++ b/processing_handler.go @@ -186,6 +186,21 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) { checkTimeout(ctx) + if len(conf.SkipProcessingFormats) > 0 { + imgdata := getImageData(ctx) + po := getProcessingOptions(ctx) + + if imgdata.Type == po.Format || po.Format == imageTypeUnknown { + for _, f := range conf.SkipProcessingFormats { + if f == imgdata.Type { + po.Format = imgdata.Type + respondWithImage(ctx, reqID, r, rw, imgdata.Data) + return + } + } + } + } + imageData, processcancel, err := processImage(ctx) defer processcancel() if err != nil {