mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-09-26 11:38:01 +02:00
Add URL replacements
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -54,6 +55,20 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
|
||||
require.Equal(s.T(), imagetype.PNG, po.Format)
|
||||
}
|
||||
|
||||
func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithReplacement() {
|
||||
config.URLReplacements = map[*regexp.Regexp]string{
|
||||
regexp.MustCompile("^test://([^/]*)/"): "http://images.dev/${1}/dolor/",
|
||||
}
|
||||
|
||||
originURL := "test://lorem/ipsum.jpg?param=value"
|
||||
path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
||||
po, imageURL, err := ParsePath(path, make(http.Header))
|
||||
|
||||
require.Nil(s.T(), err)
|
||||
require.Equal(s.T(), "http://images.dev/lorem/dolor/ipsum.jpg?param=value", imageURL)
|
||||
require.Equal(s.T(), imagetype.PNG, po.Format)
|
||||
}
|
||||
|
||||
func (s *ProcessingOptionsTestSuite) TestParsePlainURL() {
|
||||
originURL := "http://images.dev/lorem/ipsum.jpg"
|
||||
path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
|
||||
@@ -96,6 +111,20 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithBase() {
|
||||
require.Equal(s.T(), imagetype.PNG, po.Format)
|
||||
}
|
||||
|
||||
func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithReplacement() {
|
||||
config.URLReplacements = map[*regexp.Regexp]string{
|
||||
regexp.MustCompile("^test://([^/]*)/"): "http://images.dev/${1}/dolor/",
|
||||
}
|
||||
|
||||
originURL := "test://lorem/ipsum.jpg"
|
||||
path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
|
||||
po, imageURL, err := ParsePath(path, make(http.Header))
|
||||
|
||||
require.Nil(s.T(), err)
|
||||
require.Equal(s.T(), "http://images.dev/lorem/dolor/ipsum.jpg", imageURL)
|
||||
require.Equal(s.T(), imagetype.PNG, po.Format)
|
||||
}
|
||||
|
||||
func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscapedWithBase() {
|
||||
config.BaseURL = "http://images.dev/"
|
||||
|
||||
|
@@ -12,7 +12,11 @@ import (
|
||||
|
||||
const urlTokenPlain = "plain"
|
||||
|
||||
func addBaseURL(u string) string {
|
||||
func preprocessURL(u string) string {
|
||||
for re, repl := range config.URLReplacements {
|
||||
u = re.ReplaceAllString(u, repl)
|
||||
}
|
||||
|
||||
if len(config.BaseURL) == 0 || strings.HasPrefix(u, config.BaseURL) {
|
||||
return u
|
||||
}
|
||||
@@ -43,7 +47,7 @@ func decodeBase64URL(parts []string) (string, string, error) {
|
||||
return "", "", fmt.Errorf("Invalid url encoding: %s", encoded)
|
||||
}
|
||||
|
||||
return addBaseURL(string(imageURL)), format, nil
|
||||
return preprocessURL(string(imageURL)), format, nil
|
||||
}
|
||||
|
||||
func decodePlainURL(parts []string) (string, string, error) {
|
||||
@@ -69,7 +73,7 @@ func decodePlainURL(parts []string) (string, string, error) {
|
||||
return "", "", fmt.Errorf("Invalid url encoding: %s", encoded)
|
||||
}
|
||||
|
||||
return addBaseURL(unescaped), format, nil
|
||||
return preprocessURL(unescaped), format, nil
|
||||
}
|
||||
|
||||
func DecodeURL(parts []string) (string, string, error) {
|
||||
|
Reference in New Issue
Block a user