From 77da784cd16151f0fe86c129b8d4503db6a7cd67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 14 Sep 2020 17:19:30 +0200 Subject: [PATCH] resources/image: Fix nilpointer for images with no Exif E.g. PNG files. Fixes #7688 --- resources/image.go | 12 ++++++++---- resources/image_test.go | 10 +++++----- resources/resource/resourcetypes.go | 2 +- resources/transform.go | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/resources/image.go b/resources/image.go index d1d9f650d67..e999c5d96c2 100644 --- a/resources/image.go +++ b/resources/image.go @@ -71,11 +71,11 @@ type imageMeta struct { Exif *exif.Exif } -func (i *imageResource) Exif() (*exif.Exif, error) { +func (i *imageResource) Exif() *exif.Exif { return i.root.getExif() } -func (i *imageResource) getExif() (*exif.Exif, error) { +func (i *imageResource) getExif() *exif.Exif { i.metaInit.Do(func() { @@ -131,10 +131,14 @@ func (i *imageResource) getExif() (*exif.Exif, error) { }) if i.metaInitErr != nil { - return nil, i.metaInitErr + panic(fmt.Sprintf("metadata init failed: %s", i.metaInitErr)) } - return i.meta.Exif, nil + if i.meta == nil { + return nil + } + + return i.meta.Exif } func (i *imageResource) Clone() resource.Resource { diff --git a/resources/image_test.go b/resources/image_test.go index f98d9f4bb24..efbfcde19a4 100644 --- a/resources/image_test.go +++ b/resources/image_test.go @@ -321,6 +321,7 @@ func TestImageResize8BitPNG(t *testing.T) { c.Assert(image.MediaType().Type(), qt.Equals, "image/png") c.Assert(image.RelPermalink(), qt.Equals, "/a/gohugoio.png") c.Assert(image.ResourceType(), qt.Equals, "image") + c.Assert(image.Exif(), qt.IsNil) resized, err := image.Resize("800x") c.Assert(err, qt.IsNil) @@ -337,6 +338,7 @@ func TestImageResizeInSubPath(t *testing.T) { c.Assert(image.MediaType(), eq, media.PNGType) c.Assert(image.RelPermalink(), qt.Equals, "/a/sub/gohugoio2.png") c.Assert(image.ResourceType(), qt.Equals, "image") + //c.Assert(image.Exif()) resized, err := image.Resize("101x101") c.Assert(err, qt.IsNil) @@ -387,8 +389,7 @@ func TestImageExif(t *testing.T) { image := fetchResourceForSpec(spec, c, "sunset.jpg").(resource.Image) getAndCheckExif := func(c *qt.C, image resource.Image) { - x, err := image.Exif() - c.Assert(err, qt.IsNil) + x := image.Exif() c.Assert(x, qt.Not(qt.IsNil)) c.Assert(x.Date.Format("2006-01-02"), qt.Equals, "2017-10-27") @@ -403,7 +404,7 @@ func TestImageExif(t *testing.T) { c.Assert(ok, qt.Equals, true) c.Assert(lensModel, qt.Equals, "smc PENTAX-DA* 16-50mm F2.8 ED AL [IF] SDM") resized, _ := image.Resize("300x200") - x2, _ := resized.Exif() + x2 := resized.Exif() c.Assert(x2, eq, x) } @@ -426,8 +427,7 @@ func BenchmarkImageExif(b *testing.B) { } getAndCheckExif := func(c *qt.C, image resource.Image) { - x, err := image.Exif() - c.Assert(err, qt.IsNil) + x := image.Exif() c.Assert(x, qt.Not(qt.IsNil)) c.Assert(x.Long, qt.Equals, float64(-4.50846)) diff --git a/resources/resource/resourcetypes.go b/resources/resource/resourcetypes.go index 13ffc5ae3ea..f42372fa396 100644 --- a/resources/resource/resourcetypes.go +++ b/resources/resource/resourcetypes.go @@ -58,7 +58,7 @@ type ImageOps interface { Fit(spec string) (Image, error) Resize(spec string) (Image, error) Filter(filters ...interface{}) (Image, error) - Exif() (*exif.Exif, error) + Exif() *exif.Exif } type ResourceTypeProvider interface { diff --git a/resources/transform.go b/resources/transform.go index 98aee3c2a6f..7793d3e0ea4 100644 --- a/resources/transform.go +++ b/resources/transform.go @@ -183,7 +183,7 @@ func (r *resourceAdapter) Height() int { return r.getImageOps().Height() } -func (r *resourceAdapter) Exif() (*exif.Exif, error) { +func (r *resourceAdapter) Exif() *exif.Exif { return r.getImageOps().Exif() }