Implemented better images.Get() and added better tests

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-08 14:59:03 +01:00 committed by Andrea Maria Piana
parent 7f8d3674d4
commit b7cd88744c
6 changed files with 112 additions and 16 deletions

BIN
_assets/tests/elephant.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 979 B

BIN
_assets/tests/rose.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
_assets/tests/spin.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

BIN
_assets/tests/status.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@ -7,7 +7,6 @@ import (
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"io" "io"
"net/http"
"os" "os"
"golang.org/x/image/webp" "golang.org/x/image/webp"
@ -20,27 +19,45 @@ func Get(fileName string) (image.Image, error) {
} }
defer file.Close() defer file.Close()
fb := make([]byte, 12) fb, err := prepareFileForDecode(file)
file.Read(fb) if err != nil {
ft := GetFileType(fb) return nil, err
if ft == UNKNOWN {
return nil, errors.New("unsupported file type")
} }
var img image.Image return decodeImageData(fb, file)
switch ft { }
func prepareFileForDecode(file *os.File) ([]byte, error) {
// Read the first 14 bytes, used for performing image type checks before parsing the image data
fb := make([]byte, 14)
_, err := file.Read(fb)
if err != nil {
return nil, err
}
// Reset the read cursor
_, err = file.Seek(0, 0)
if err != nil {
return nil, err
}
return fb, nil
}
func decodeImageData(buf []byte, r io.Reader) (img image.Image, err error) {
switch GetFileType(buf) {
case JPEG: case JPEG:
img, err = jpeg.Decode(file) img, err = jpeg.Decode(r)
break
case PNG: case PNG:
img, err = png.Decode(file) img, err = png.Decode(r)
break
case GIF: case GIF:
img, err = gif.Decode(file) img, err = gif.Decode(r)
break
case WEBP: case WEBP:
img, err = webp.Decode(file) img, err = webp.Decode(r)
break case UNKNOWN:
fallthrough
default:
return nil, errors.New("unsupported file type")
} }
if err != nil { if err != nil {
return nil, err return nil, err

79
images/files_test.go Normal file
View File

@ -0,0 +1,79 @@
package images
import (
"github.com/stretchr/testify/require"
"image"
"testing"
)
func TestGet(t *testing.T) {
cs := []struct{
Filepath string
Error bool
Nil bool
Bounds image.Rectangle
}{
{
"../_assets/tests/elephant.jpg",
false,
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 80, Y: 80},
},
},
{
"../_assets/tests/status.png",
false,
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 256, Y: 256},
},
},
{
"../_assets/tests/spin.gif",
false,
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 256, Y: 256},
},
},
{
"../_assets/tests/rose.webp",
false,
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 400, Y: 301},
},
},
{
"../_assets/tests/test.aac",
true,
true,
image.Rectangle{},
},
}
for _, test := range cs {
img, err := Get(test.Filepath)
if test.Error {
require.Error(t, err)
} else {
require.NoError(t, err)
}
if test.Nil {
require.Nil(t, img)
continue
} else {
require.NotNil(t, img)
}
require.Exactly(t, test.Bounds, img.Bounds())
}
}