Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 17, 2021
1 parent 2bd9e91 commit 712fabf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,6 @@ require (
)

go 1.16


replace github.com/dsoprea/go-exif/v3 => /Users/bep/dev/go/dump/go-exif/v3
18 changes: 10 additions & 8 deletions resources/images/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type Decoder struct {
excludeFieldsrRe *regexp.Regexp
noDate bool
noLatLong bool

idfm *exifcommon.IfdMapping
ti *_exif.TagIndex
}

func IncludeFields(expression string) func(*Decoder) error {
Expand Down Expand Up @@ -96,7 +99,12 @@ func compileRegexp(expression string) (*regexp.Regexp, error) {
}

func NewDecoder(options ...func(*Decoder) error) (*Decoder, error) {
d := &Decoder{}
im, err := exifcommon.NewIfdMappingWithStandard()
if err != nil {
return nil, err
}
ti := _exif.NewTagIndex()
d := &Decoder{idfm: im, ti: ti}
for _, opt := range options {
if err := opt(d); err != nil {
return nil, err
Expand All @@ -114,13 +122,7 @@ func (d *Decoder) Decode(r io.Reader) (*Exif, error) {
return nil, nil
}

im, err := exifcommon.NewIfdMappingWithStandard()
if err != nil {
return nil, err
}
ti := _exif.NewTagIndex()

_, index, err := _exif.Collect(im, ti, rawExif)
_, index, err := _exif.Collect(d.idfm, d.ti, rawExif)
if err != nil {
return nil, err
}
Expand Down
39 changes: 39 additions & 0 deletions resources/images/exif/exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
package exif

import (
"context"
"encoding/json"
"errors"
"math/big"
"os"
"path/filepath"
"testing"
"time"

"github.com/gohugoio/hugo/common/para"
"github.com/gohugoio/hugo/htesting/hqt"
"github.com/google/go-cmp/cmp"

Expand Down Expand Up @@ -62,6 +65,42 @@ func TestExif(t *testing.T) {
c.Assert(x2, eq, x)
}

func TestExifParallel(t *testing.T) {
c := qt.New(t)

d, err := NewDecoder(IncludeFields("Lens|Date"))
c.Assert(err, qt.IsNil)

p := para.New(4)
r, _ := p.Start(context.Background())

for i := 0; i < 20; i++ {
r.Run(func() error {
f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg"))
if err != nil {
return err
}
defer f.Close()

x, err := d.Decode(f)
if err != nil {
return err
}

if x.Date.Format("2006-01-02") != "2017-10-27" {
return errors.New("invalid date")
}

return nil

})

}

c.Assert(r.Wait(), qt.IsNil)

}

func TestImageWithoutExifData(t *testing.T) {
c := qt.New(t)
f, err := os.Open(filepath.FromSlash("../../testdata/sunset_without_exif.jpg"))
Expand Down

0 comments on commit 712fabf

Please sign in to comment.