diff --git a/CHANGELOG.md b/CHANGELOG.md index c1d02bda..24f8cc62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] +### Added +- Add [IMGPROXY_BASE64_URL_INCLUDES_FILENAME](https://docs.imgproxy.net/latest/configuration/options#IMGPROXY_BASE64_URL_INCLUDES_FILENAME) config. + ## [3.27.1] - 2025-01-13 ### Added - Add [IMGPROXY_SOURCE_URL_QUERY_SEPARATOR](https://docs.imgproxy.net/latest/configuration/options#IMGPROXY_SOURCE_URL_QUERY_SEPARATOR) config. diff --git a/config/config.go b/config/config.go index 1e89f77a..a4e24047 100644 --- a/config/config.go +++ b/config/config.go @@ -142,8 +142,9 @@ var ( LastModifiedEnabled bool - BaseURL string - URLReplacements []URLReplacement + BaseURL string + URLReplacements []URLReplacement + Base64URLIncludesFilename bool Presets []string OnlyPresets bool @@ -350,6 +351,7 @@ func Reset() { BaseURL = "" URLReplacements = make([]URLReplacement, 0) + Base64URLIncludesFilename = false Presets = make([]string, 0) OnlyPresets = false @@ -604,6 +606,7 @@ func Configure() error { if err := configurators.Replacements(&URLReplacements, "IMGPROXY_URL_REPLACEMENTS"); err != nil { return err } + configurators.Bool(&Base64URLIncludesFilename, "IMGPROXY_BASE64_URL_INCLUDES_FILENAME") presetsSep := "," configurators.String(&presetsSep, "IMGPROXY_PRESETS_SEPARATOR") diff --git a/options/processing_options_test.go b/options/processing_options_test.go index 3106e715..b5b1672e 100644 --- a/options/processing_options_test.go +++ b/options/processing_options_test.go @@ -32,6 +32,18 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URL() { s.Require().Equal(imagetype.PNG, po.Format) } +func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithFilename() { + config.Base64URLIncludesFilename = true + + originURL := "http://images.dev/lorem/ipsum.jpg?param=value" + path := fmt.Sprintf("/size:100:100/%s.png/puppy.jpg", base64.RawURLEncoding.EncodeToString([]byte(originURL))) + po, imageURL, err := ParsePath(path, make(http.Header)) + + s.Require().NoError(err) + s.Require().Equal(originURL, imageURL) + s.Require().Equal(imagetype.PNG, po.Format) +} + func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithoutExtension() { originURL := "http://images.dev/lorem/ipsum.jpg?param=value" path := fmt.Sprintf("/size:100:100/%s", base64.RawURLEncoding.EncodeToString([]byte(originURL))) diff --git a/options/url.go b/options/url.go index e3ba82f9..c1f229c1 100644 --- a/options/url.go +++ b/options/url.go @@ -27,6 +27,10 @@ func preprocessURL(u string) string { func decodeBase64URL(parts []string) (string, string, error) { var format string + if len(parts) > 1 && config.Base64URLIncludesFilename { + parts = parts[:len(parts)-1] + } + encoded := strings.Join(parts, "") urlParts := strings.Split(encoded, ".")