diff options
Diffstat (limited to 'resources/images')
-rw-r--r-- | resources/images/exif/exif.go | 39 | ||||
-rw-r--r-- | resources/images/exif/exif_test.go | 26 |
2 files changed, 58 insertions, 7 deletions
diff --git a/resources/images/exif/exif.go b/resources/images/exif/exif.go index 7a3c982c1..b5161f770 100644 --- a/resources/images/exif/exif.go +++ b/resources/images/exif/exif.go @@ -24,6 +24,8 @@ import ( "unicode" "unicode/utf8" + "github.com/bep/tmc" + _exif "github.com/rwcarlsen/goexif/exif" "github.com/rwcarlsen/goexif/tiff" ) @@ -31,10 +33,10 @@ import ( const exifTimeLayout = "2006:01:02 15:04:05" type Exif struct { - Lat float64 - Long float64 - Date time.Time - Values map[string]interface{} + Lat float64 + Long float64 + Date time.Time + Tags Tags } type Decoder struct { @@ -139,7 +141,7 @@ func (d *Decoder) Decode(r io.Reader) (ex *Exif, err error) { return } - ex = &Exif{Lat: lat, Long: long, Date: tm, Values: walker.vals} + ex = &Exif{Lat: lat, Long: long, Date: tm, Tags: walker.vals} return } @@ -240,3 +242,30 @@ func nullString(in []byte) string { return "" } + +var tcodec *tmc.Codec + +func init() { + var err error + tcodec, err = tmc.New() + if err != nil { + panic(err) + } +} + +type Tags map[string]interface{} + +func (v *Tags) UnmarshalJSON(b []byte) error { + vv := make(map[string]interface{}) + if err := tcodec.Unmarshal(b, &vv); err != nil { + return err + } + + *v = vv + + return nil +} + +func (v Tags) MarshalJSON() ([]byte, error) { + return tcodec.Marshal(v) +} diff --git a/resources/images/exif/exif_test.go b/resources/images/exif/exif_test.go index eee60c089..c3cfad1cc 100644 --- a/resources/images/exif/exif_test.go +++ b/resources/images/exif/exif_test.go @@ -14,12 +14,15 @@ package exif import ( + "encoding/json" + "math/big" "os" "path/filepath" "testing" "time" "github.com/gohugoio/hugo/htesting/hqt" + "github.com/google/go-cmp/cmp" qt "github.com/frankban/quicktest" ) @@ -40,16 +43,24 @@ func TestExif(t *testing.T) { c.Assert(x.Lat, qt.Equals, float64(36.59744166666667)) c.Assert(x.Long, qt.Equals, float64(-4.50846)) - v, found := x.Values["LensModel"] + v, found := x.Tags["LensModel"] c.Assert(found, qt.Equals, true) lensModel, ok := v.(string) c.Assert(ok, qt.Equals, true) c.Assert(lensModel, qt.Equals, "smc PENTAX-DA* 16-50mm F2.8 ED AL [IF] SDM") - v, found = x.Values["DateTime"] + v, found = x.Tags["DateTime"] c.Assert(found, qt.Equals, true) c.Assert(v, hqt.IsSameType, time.Time{}) + // Verify that it survives a round-trip to JSON and back. + data, err := json.Marshal(x) + c.Assert(err, qt.IsNil) + x2 := &Exif{} + err = json.Unmarshal(data, x2) + + c.Assert(x2, eq, x) + } func TestExifPNG(t *testing.T) { @@ -81,3 +92,14 @@ func BenchmarkDecodeExif(b *testing.B) { f.Seek(0, 0) } } + +var eq = qt.CmpEquals( + cmp.Comparer( + func(v1, v2 *big.Rat) bool { + return v1.RatString() == v2.RatString() + }, + ), + cmp.Comparer(func(v1, v2 time.Time) bool { + return v1.Unix() == v2.Unix() + }), +) |