Browse Source

Update output config format to allow string extensions

feature/update-config-output-format-options
Roman Zipp 3 years ago
parent
commit
8b372c1260
  1. 6
      README.md
  2. 2
      config/example.json
  3. 13
      pipelines/pipeline.go
  4. 40
      pipelines/pipeline_test.go

6
README.md

@ -105,7 +105,7 @@ Authorization: Bearer <Token>
}
],
"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
}
}

2
config/example.json

@ -33,7 +33,7 @@
}
],
"output": {
"format": 0,
"format": "jpg",
"quality": 90
}
}

13
pipelines/pipeline.go

@ -38,7 +38,7 @@ type Pipeline struct {
RemoveMetadata bool `json:"remove_metadata" faker:"-"`
Steps []Step `json:"steps" faker:"-"`
Output struct {
Format int `json:"format"`
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 {

40
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"))
})
}
Loading…
Cancel
Save