1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-03-17 20:17:48 +02:00

Fix options rewrite

This commit is contained in:
DarthSim 2019-07-22 17:27:18 +06:00
parent 5426c309f3
commit ee54296249
3 changed files with 28 additions and 24 deletions

View File

@ -19,8 +19,8 @@ func (s *PresetsTestSuite) TestParsePreset() {
require.Nil(s.T(), err) require.Nil(s.T(), err)
assert.Equal(s.T(), urlOptions{ assert.Equal(s.T(), urlOptions{
"resize": []string{"fit", "100", "200"}, urlOption{Name: "resize", Args: []string{"fit", "100", "200"}},
"sharpen": []string{"2"}, urlOption{Name: "sharpen", Args: []string{"2"}},
}, p["test"]) }, p["test"])
} }
@ -85,8 +85,8 @@ func (s *PresetsTestSuite) TestParsePresetComment() {
func (s *PresetsTestSuite) TestCheckPresets() { func (s *PresetsTestSuite) TestCheckPresets() {
p := presets{ p := presets{
"test": urlOptions{ "test": urlOptions{
"resize": []string{"fit", "100", "200"}, urlOption{Name: "resize", Args: []string{"fit", "100", "200"}},
"sharpen": []string{"2"}, urlOption{Name: "sharpen", Args: []string{"2"}},
}, },
} }
@ -98,8 +98,8 @@ func (s *PresetsTestSuite) TestCheckPresets() {
func (s *PresetsTestSuite) TestCheckPresetsInvalid() { func (s *PresetsTestSuite) TestCheckPresetsInvalid() {
p := presets{ p := presets{
"test": urlOptions{ "test": urlOptions{
"resize": []string{"fit", "-1", "-2"}, urlOption{Name: "resize", Args: []string{"fit", "-1", "-2"}},
"sharpen": []string{"2"}, urlOption{Name: "sharpen", Args: []string{"2"}},
}, },
} }

View File

@ -12,7 +12,11 @@ import (
"strings" "strings"
) )
type urlOptions map[string][]string type urlOption struct {
Name string
Args []string
}
type urlOptions []urlOption
type processingHeaders struct { type processingHeaders struct {
Accept string Accept string
@ -713,8 +717,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
} }
func applyProcessingOptions(po *processingOptions, options urlOptions) error { func applyProcessingOptions(po *processingOptions, options urlOptions) error {
for name, args := range options { for _, opt := range options {
if err := applyProcessingOption(po, name, args); err != nil { if err := applyProcessingOption(po, opt.Name, opt.Args); err != nil {
return err return err
} }
} }
@ -723,7 +727,7 @@ func applyProcessingOptions(po *processingOptions, options urlOptions) error {
} }
func parseURLOptions(opts []string) (urlOptions, []string) { func parseURLOptions(opts []string) (urlOptions, []string) {
parsed := make(urlOptions) parsed := make(urlOptions, 0, len(opts))
urlStart := len(opts) + 1 urlStart := len(opts) + 1
for i, opt := range opts { for i, opt := range opts {
@ -734,7 +738,7 @@ func parseURLOptions(opts []string) (urlOptions, []string) {
break break
} }
parsed[args[0]] = args[1:] parsed = append(parsed, urlOption{Name: args[0], Args: args[1:]})
} }
var rest []string var rest []string

View File

@ -335,12 +335,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedWatermark() {
func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() { func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() {
conf.Presets["test1"] = urlOptions{ conf.Presets["test1"] = urlOptions{
"resizing_type": []string{"fill"}, urlOption{Name: "resizing_type", Args: []string{"fill"}},
} }
conf.Presets["test2"] = urlOptions{ conf.Presets["test2"] = urlOptions{
"blur": []string{"0.2"}, urlOption{Name: "blur", Args: []string{"0.2"}},
"quality": []string{"50"}, urlOption{Name: "quality", Args: []string{"50"}},
} }
req := s.getRequest("http://example.com/unsafe/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg") req := s.getRequest("http://example.com/unsafe/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
@ -356,9 +356,9 @@ func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() {
func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() { func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
conf.Presets["default"] = urlOptions{ conf.Presets["default"] = urlOptions{
"resizing_type": []string{"fill"}, urlOption{Name: "resizing_type", Args: []string{"fill"}},
"blur": []string{"0.2"}, urlOption{Name: "blur", Args: []string{"0.2"}},
"quality": []string{"50"}, urlOption{Name: "quality", Args: []string{"50"}},
} }
req := s.getRequest("http://example.com/unsafe/quality:70/plain/http://images.dev/lorem/ipsum.jpg") req := s.getRequest("http://example.com/unsafe/quality:70/plain/http://images.dev/lorem/ipsum.jpg")
@ -374,12 +374,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPresetLoopDetection() { func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPresetLoopDetection() {
conf.Presets["test1"] = urlOptions{ conf.Presets["test1"] = urlOptions{
"resizing_type": []string{"fill"}, urlOption{Name: "resizing_type", Args: []string{"fill"}},
} }
conf.Presets["test2"] = urlOptions{ conf.Presets["test2"] = urlOptions{
"blur": []string{"0.2"}, urlOption{Name: "blur", Args: []string{"0.2"}},
"quality": []string{"50"}, urlOption{Name: "quality", Args: []string{"50"}},
} }
req := s.getRequest("http://example.com/unsafe/preset:test1:test2:test1/plain/http://images.dev/lorem/ipsum.jpg") req := s.getRequest("http://example.com/unsafe/preset:test1:test2:test1/plain/http://images.dev/lorem/ipsum.jpg")
@ -553,10 +553,10 @@ func (s *ProcessingOptionsTestSuite) TestParsePathSignedInvalid() {
func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() { func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
conf.OnlyPresets = true conf.OnlyPresets = true
conf.Presets["test1"] = urlOptions{ conf.Presets["test1"] = urlOptions{
"blur": []string{"0.2"}, urlOption{Name: "blur", Args: []string{"0.2"}},
} }
conf.Presets["test2"] = urlOptions{ conf.Presets["test2"] = urlOptions{
"quality": []string{"50"}, urlOption{Name: "quality", Args: []string{"50"}},
} }
req := s.getRequest("http://example.com/unsafe/test1:test2/plain/http://images.dev/lorem/ipsum.jpg") req := s.getRequest("http://example.com/unsafe/test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
@ -573,10 +573,10 @@ func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() { func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
conf.OnlyPresets = true conf.OnlyPresets = true
conf.Presets["test1"] = urlOptions{ conf.Presets["test1"] = urlOptions{
"blur": []string{"0.2"}, urlOption{Name: "blur", Args: []string{"0.2"}},
} }
conf.Presets["test2"] = urlOptions{ conf.Presets["test2"] = urlOptions{
"quality": []string{"50"}, urlOption{Name: "quality", Args: []string{"50"}},
} }
imageURL := "http://images.dev/lorem/ipsum.jpg?param=value" imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"