1
0
Эх сурвалжийг харах

Add IMGPROXY_BASE64_URL_INCLUDES_FILENAME config

DarthSim 3 сар өмнө
parent
commit
0f37d62fd8

+ 4 - 0
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.

+ 5 - 2
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")

+ 12 - 0
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)))

+ 4 - 0
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, ".")