diff --git a/config/example.json b/config/example.json index 072d863..03a5161 100644 --- a/config/example.json +++ b/config/example.json @@ -12,13 +12,10 @@ "height": 720, "upscale": false } - }, - { - "name": "compress image", - "type": 1, - "options": { - "quality": 80 - } } - ] + ], + "output": { + "format": 0, + "quality": 90 + } } diff --git a/pipelines/executable_step.go b/pipelines/executable_step.go index 2661011..dd2da7d 100644 --- a/pipelines/executable_step.go +++ b/pipelines/executable_step.go @@ -24,16 +24,3 @@ func (s ResizeImageStep) Execute(src image.Image) (image.Image, error) { src = imaging.Resize(src, s.Options.Width, s.Options.Height, imaging.Linear) return src, nil } - -// Compress image - -type CompressImageStep struct { - Step - Options struct { - Quality int `json:"quality"` - } `json:"options"` -} - -func (s CompressImageStep) Execute(src image.Image) (image.Image, error) { - return src, nil -} diff --git a/pipelines/executable_step_test.go b/pipelines/executable_step_test.go index 9e91c2c..4be7ae8 100644 --- a/pipelines/executable_step_test.go +++ b/pipelines/executable_step_test.go @@ -32,31 +32,6 @@ func TestDeserializeOptionsResizeImage(t *testing.T) { }) } -func TestDeserializeOptionsCompressImage(t *testing.T) { - const Payload string = `{ - "name": "example pipeline", - "type": 0, - "removeMetadata": false, - "steps": [ - { - "name": "compress image", - "type": 1, - "options": { - "quality": 80 - } - } - ] - }` - - t.Run("Image pipeline deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(Payload)}) - - _, err := values[0].GetSteps()[0].GetExecutable() - - assert.Equal(t, nil, err) - }) -} - func TestDeserializeMissingOptions(t *testing.T) { const Payload string = `{ "name": "example pipeline", diff --git a/pipelines/pipeline.go b/pipelines/pipeline.go index 4911fa7..e9a783a 100644 --- a/pipelines/pipeline.go +++ b/pipelines/pipeline.go @@ -36,6 +36,10 @@ type Pipeline struct { Type PipelineType `json:"type" faker:"-"` RemoveMetadata bool `json:"remove_metadata" faker:"-"` Steps []Step `json:"steps" faker:"-"` + Output struct { + Format int `json:"format"` + Quality int `json:"quality"` + } `json:"output" faker:"-"` } func (p Pipeline) Run(srcPath, bucketName string, storageProvider storage.IStorageProvider) (string, error) { @@ -58,9 +62,16 @@ func (p Pipeline) Run(srcPath, bucketName string, storageProvider storage.IStora } } + format := imaging.Format(p.Output.Format) + var options []imaging.EncodeOption + + if p.Output.Quality != 0 { + options = append(options, imaging.JPEGQuality(p.Output.Quality)) + } + // encode image to io buffer buffer := new(bytes.Buffer) - if err := imaging.Encode(buffer, src, imaging.JPEG); err != nil { + if err := imaging.Encode(buffer, src, format, options...); err != nil { return "", err } diff --git a/pipelines/pipeline_test.go b/pipelines/pipeline_test.go index 9f7642e..87f00fc 100644 --- a/pipelines/pipeline_test.go +++ b/pipelines/pipeline_test.go @@ -99,3 +99,45 @@ func TestResizeImage(t *testing.T) { os.Remove(storageProvider.GetPath(Bucket, dest)) }) } + +func TestEncodeImageWithJpegQuality(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0, + "options": { + "width": 1280, + "height": 720, + "upscale": false + } + } + ], + "output": { + "quality": 50 + } + }` + const Bucket string = "pipeline_test_02" + + t.Run("Image resizing is successful", func(t *testing.T) { + wd, _ := os.Getwd() + pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] + + storageProvider := storage.GetFileSystemStorageProvider("test", "..") + + _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg")) + assert.Nil(t, err, "Test file should be readable") + assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) + + dest, err := pipe.Run("source.jpg", Bucket, storageProvider) + assert.Nil(t, err) + assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) + + // clean up + os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) + os.Remove(storageProvider.GetPath(Bucket, dest)) + }) +} diff --git a/pipelines/step.go b/pipelines/step.go index aa80da3..6e2b0f1 100644 --- a/pipelines/step.go +++ b/pipelines/step.go @@ -9,7 +9,6 @@ type StepType int const ( TypeResizeImageStep StepType = iota - TypeCompressImageStep ) type Step struct { @@ -26,13 +25,6 @@ func (s Step) GetExecutable() (IExecutableStep, error) { return nil, err } return step, nil - - case TypeCompressImageStep: - step := CompressImageStep{} - if err := json.Unmarshal(s.Options, &step.Options); err != nil { - return nil, err - } - return step, nil } return nil, errors.New("invalid type")