-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from thoas/refac-mutiple-operations
Refac mutiple operations
- Loading branch information
Showing
12 changed files
with
421 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
package application | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/disintegration/imaging" | ||
"github.com/thoas/picfit/engine" | ||
"github.com/thoas/picfit/engine/backend" | ||
"github.com/thoas/picfit/image" | ||
) | ||
|
||
const ( | ||
defaultUpscale = true | ||
defaultWidth = 0 | ||
defaultHeight = 0 | ||
defaultDegree = 90 | ||
) | ||
|
||
var formats = map[string]imaging.Format{ | ||
"jpeg": imaging.JPEG, | ||
"jpg": imaging.JPEG, | ||
"png": imaging.PNG, | ||
"gif": imaging.GIF, | ||
"bmp": imaging.BMP, | ||
} | ||
|
||
type Parameters struct { | ||
Output *image.ImageFile | ||
Operations []engine.EngineOperation | ||
} | ||
|
||
func NewParameters(e *engine.Engine, input *image.ImageFile, qs map[string]interface{}) (*Parameters, error) { | ||
format, ok := qs["fmt"].(string) | ||
filepath := input.Filepath | ||
|
||
if ok { | ||
if _, ok := engine.ContentTypes[format]; !ok { | ||
return nil, fmt.Errorf("Unknown format %s", format) | ||
} | ||
|
||
} | ||
|
||
if format == "" && e.Format != "" { | ||
format = e.Format | ||
} | ||
|
||
if format == "" { | ||
format = input.Format() | ||
} | ||
|
||
if format == "" { | ||
format = e.DefaultFormat | ||
} | ||
|
||
if format != input.Format() { | ||
index := len(filepath) - len(input.Format()) | ||
|
||
filepath = filepath[:index] + format | ||
|
||
if contentType, ok := engine.ContentTypes[format]; ok { | ||
input.Headers["Content-Type"] = contentType | ||
} | ||
} | ||
|
||
output := &image.ImageFile{ | ||
Source: input.Source, | ||
Key: input.Key, | ||
Headers: input.Headers, | ||
Filepath: filepath, | ||
} | ||
|
||
var operations []engine.EngineOperation | ||
|
||
op, ok := qs["op"].(string) | ||
if ok { | ||
operation := engine.Operation(op) | ||
opts, err := newBackendOptionsFromParameters(e, operation, qs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts.Format = formats[format] | ||
operations = append(operations, engine.EngineOperation{ | ||
Options: opts, | ||
Operation: operation, | ||
}) | ||
} | ||
|
||
ops, ok := qs["op"].([]string) | ||
if ok { | ||
for i := range ops { | ||
var err error | ||
engineOperation := &engine.EngineOperation{} | ||
operation, k := engine.Operations[ops[i]] | ||
if k { | ||
engineOperation.Operation = operation | ||
engineOperation.Options, err = newBackendOptionsFromParameters(e, operation, qs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
engineOperation, err = newEngineOperationFromQuery(e, ops[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if engineOperation != nil { | ||
engineOperation.Options.Format = formats[format] | ||
operations = append(operations, *engineOperation) | ||
} | ||
} | ||
} | ||
|
||
return &Parameters{ | ||
Output: output, | ||
Operations: operations, | ||
}, nil | ||
} | ||
|
||
func newEngineOperationFromQuery(e *engine.Engine, op string) (*engine.EngineOperation, error) { | ||
params := make(map[string]interface{}) | ||
for _, p := range strings.Split(op, " ") { | ||
l := strings.Split(p, ":") | ||
if len(l) > 1 { | ||
params[l[0]] = l[1] | ||
} | ||
} | ||
|
||
op, ok := params["op"].(string) | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
operation := engine.Operation(op) | ||
opts, err := newBackendOptionsFromParameters(e, operation, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &engine.EngineOperation{ | ||
Options: opts, | ||
Operation: operation, | ||
}, nil | ||
} | ||
|
||
func newBackendOptionsFromParameters(e *engine.Engine, operation engine.Operation, qs map[string]interface{}) (*backend.Options, error) { | ||
var ( | ||
err error | ||
quality int | ||
upscale = defaultUpscale | ||
height = defaultHeight | ||
width = defaultWidth | ||
degree = defaultDegree | ||
) | ||
|
||
q, ok := qs["q"].(string) | ||
if ok { | ||
quality, err = strconv.Atoi(q) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if quality > 100 { | ||
return nil, fmt.Errorf("Quality should be <= 100") | ||
} | ||
} else { | ||
quality = e.DefaultQuality | ||
} | ||
|
||
position, ok := qs["pos"].(string) | ||
if !ok && operation == engine.Flip { | ||
return nil, fmt.Errorf("Parameter \"pos\" not found in query string") | ||
} | ||
|
||
if deg, ok := qs["deg"].(string); ok { | ||
degree, err = strconv.Atoi(deg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if up, ok := qs["upscale"].(string); ok { | ||
upscale, err = strconv.ParseBool(up) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if w, ok := qs["w"].(string); ok { | ||
width, err = strconv.Atoi(w) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if h, ok := qs["h"].(string); ok { | ||
height, err = strconv.Atoi(h) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &backend.Options{ | ||
Width: width, | ||
Height: height, | ||
Upscale: upscale, | ||
Position: position, | ||
Quality: quality, | ||
Degree: degree, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package application | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/thoas/picfit/engine" | ||
) | ||
|
||
func TestEngineOperationFromQuery(t *testing.T) { | ||
op := "op:resize w:123 h:321 upscale:true pos:top q:99" | ||
operation, err := newEngineOperationFromQuery(&engine.Engine{}, op) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, operation.Operation.String(), "resize") | ||
assert.Equal(t, operation.Options.Height, 321) | ||
assert.Equal(t, operation.Options.Width, 123) | ||
assert.Equal(t, operation.Options.Position, "top") | ||
assert.Equal(t, operation.Options.Quality, 99) | ||
assert.True(t, operation.Options.Upscale) | ||
} |
Oops, something went wrong.