Skip to content

Commit

Permalink
Add feature to manipulator service to force encode to target extension (
Browse files Browse the repository at this point in the history
#79)

* Add feature to manipulator service to force encode to target extension

* Add target image for encoding to different file extension

* Update naming from TargetExtension to TargetFormat

---------

Co-authored-by: Kenneth Halim <[email protected]>
  • Loading branch information
kanisiuskenneth and kanisiuskenneth authored Sep 20, 2023
1 parent e276ae0 commit 11b39f3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions pkg/service/manipulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (m *manipulator) Process(spec processSpec) ([]byte, error) {
if err != nil {
return nil, err
}
if spec.TargetFormat != "" {
f = spec.TargetFormat
}
m.metricService.TrackDuration(decodeDurationKey, t, spec.ImageData)
if params[fit] == crop {
t = time.Now()
Expand Down
18 changes: 18 additions & 0 deletions pkg/service/manipulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ func TestManipulator_Process_ReturnsImageAsWebPIfCallerSupportsWebP(t *testing.T
assert.Equal(t, expectedImg, img)
}

// Integration test to verify the flow of encoding with target format
func TestManipulator_Process_ReturnsImageWithTargetFormat(t *testing.T) {
// Use real processor to ensure that right encoder is being used
p := native.NewBildProcessor()
m := NewManipulator(p, nil, metrics.NewPrometheus(prometheus.NewRegistry()))

img, _ := ioutil.ReadFile("../processor/native/_testdata/test.png")
expectedImg, _ := ioutil.ReadFile("../processor/native/_testdata/test_png_to_jpg.jpg")
ext := "jpg"
s := NewSpecBuilder().
WithImageData(img).
WithTargetFormat(ext).
Build()
img, err := m.Process(s)
assert.Nil(t, err)
assert.Equal(t, expectedImg, img)
}

func TestManipulator_Process(t *testing.T) {
mp := &mockProcessor{}
ms := &metrics.MockMetricService{}
Expand Down
28 changes: 24 additions & 4 deletions pkg/service/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ type processSpec struct {
ImageData []byte
// Params hold the key-value pairs for the processing job and tells the manipulator what to do with the image
Params map[string]string
// Reformat image to target format
TargetFormat string
// Formats have the information of accepted formats, whether darkroom can return the image using webp or not
formats []string
}

const (
extJPG = "jpg"
extPNG = "png"
extWebP = "webp"
extJPEG = "jpeg"
)

func (ps *processSpec) IsWebPSupported() bool {
for _, f := range ps.formats {
if f == "image/webp" {
Expand All @@ -30,6 +39,7 @@ type SpecBuilder interface {
WithImageData(img []byte) SpecBuilder
WithParams(params map[string]string) SpecBuilder
WithFormats(formats []string) SpecBuilder
WithTargetFormat(ext string) SpecBuilder
Build() processSpec
}

Expand All @@ -38,6 +48,7 @@ type specBuilder struct {
imageData []byte
params map[string]string
formats []string
extension string
}

func (sb *specBuilder) WithScope(scope string) SpecBuilder {
Expand All @@ -60,12 +71,21 @@ func (sb *specBuilder) WithFormats(formats []string) SpecBuilder {
return sb
}

func (sb *specBuilder) WithTargetFormat(ext string) SpecBuilder {
switch ext {
case extJPG, extJPEG, extPNG, extWebP:
sb.extension = ext
}
return sb
}

func (sb *specBuilder) Build() processSpec {
return processSpec{
Scope: sb.scope,
ImageData: sb.imageData,
Params: sb.params,
formats: sb.formats,
Scope: sb.scope,
ImageData: sb.imageData,
Params: sb.params,
formats: sb.formats,
TargetFormat: sb.extension,
}
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/service/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ func TestSpecBuilder_Build(t *testing.T) {
img := []byte("imageData")
params := map[string]string{"foo": "bar"}
formats := []string{"image/webp", "image/apng"}

ext := "png"
spec := NewSpecBuilder().
WithScope(scope).
WithImageData(img).
WithParams(params).
WithFormats(formats).
WithTargetFormat(ext).
Build()

assert.Equal(t, spec.Scope, scope)
assert.Equal(t, spec.ImageData, img)
assert.Equal(t, spec.Params, params)
assert.Equal(t, spec.formats, formats)
assert.Equal(t, spec.TargetFormat, ext)
}

func TestSpec_IsWebPSupported(t *testing.T) {
Expand All @@ -34,3 +36,9 @@ func TestSpec_IsWebPSupported(t *testing.T) {
spec = NewSpecBuilder().WithFormats(f).Build()
assert.False(t, spec.IsWebPSupported())
}

func TestSpec_Build_TargetExtensionNotValid(t *testing.T) {
ext := "gif"
spec := NewSpecBuilder().WithTargetFormat(ext).Build()
assert.Empty(t, spec.TargetFormat)
}

0 comments on commit 11b39f3

Please sign in to comment.