From c983d59e7b2a66856dbba2a35e83075652a12a7c Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 23 Jan 2022 16:41:30 +0100 Subject: [PATCH] Update output config format to allow string extensions --- README.md | 6 +++--- config/example.json | 2 +- pipelines/pipeline.go | 15 +++++++++++--- pipelines/pipeline_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 55f22e1..40fa5ec 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ Authorization: Bearer } ], "output": { - "format": 0, + "format": "jpg", "quality": 90 } } @@ -148,13 +148,13 @@ Available pipeline `type`s: `0` (Image), `1` (Video) The image pipeline offers the following additional output options. -Available `format` types: `0` (jpeg) +Available `format` types: `jpg` (or `jpeg`), `png`, `gif`, `tif` (or `tiff`) and `bmp` The `quality` field can contain any integer between 1 and 100. ```json { "output": { - "format": 0, + "format": "jpg", "quality": 90 } } diff --git a/config/example.json b/config/example.json index f2c773c..d50cca1 100644 --- a/config/example.json +++ b/config/example.json @@ -33,7 +33,7 @@ } ], "output": { - "format": 0, + "format": "jpg", "quality": 90 } } diff --git a/pipelines/pipeline.go b/pipelines/pipeline.go index c5cc9b8..c13d608 100644 --- a/pipelines/pipeline.go +++ b/pipelines/pipeline.go @@ -38,8 +38,8 @@ type Pipeline struct { RemoveMetadata bool `json:"remove_metadata" faker:"-"` Steps []Step `json:"steps" faker:"-"` Output struct { - Format int `json:"format"` - Quality int `json:"quality"` + Format string `json:"format"` + Quality int `json:"quality"` } `json:"output" faker:"-"` } @@ -63,7 +63,16 @@ func (p Pipeline) Run(srcPath, bucketName string, storageProvider storage.IStora } } - format := imaging.Format(p.Output.Format) + outputFormat := p.Output.Format + if outputFormat == "" { + outputFormat = "jpg" + } + + format, err := imaging.FormatFromExtension(outputFormat) + if err != nil { + return "", errors.New(fmt.Sprintf("output format '%s' is not supported", outputFormat)) + } + var options []imaging.EncodeOption if p.Output.Quality != 0 { diff --git a/pipelines/pipeline_test.go b/pipelines/pipeline_test.go index 0b2a20c..63a9f7b 100644 --- a/pipelines/pipeline_test.go +++ b/pipelines/pipeline_test.go @@ -362,4 +362,44 @@ func TestEncoding(t *testing.T) { os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) os.Remove(storageProvider.GetPath(Bucket, dest)) }) + + t.Run("Wrong output format results in error", func(t *testing.T) { + const InvalidPayload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0, + "options": { + "width": 1280, + "height": 720, + "upscale": false + } + } + ], + "output": { + "format": "foo", + "quality": 50 + } + }` + + wd, _ := os.Getwd() + pipe := DeserializePipelines([][]byte{[]byte(InvalidPayload)})[0] + + storageProvider := storage.GetFileSystemStorageProvider("test", "..") + + // copy test file to storage bucket + _, 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")) + + // run pipeline steps + _, err = pipe.Run("source.jpg", Bucket, storageProvider) + assert.EqualError(t, err, "output format 'foo' is not supported") + + // clean up + os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) + }) }