Skip to content

Adding DateFormat and CoordFormat funcs to modify init args #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions exiftool.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,26 @@ func ExtractAllBinaryMetadata() func(*Exiftool) error {
}
}

// DateFormant defines the -dateFormat value to pass to Exiftool, see https://exiftool.org/ExifTool.html#DateFormat
// Sample :
// e, err := NewExiftool(DateFormant("%s"))
func DateFormant(format string) func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-dateFormat", format)
return nil
}
}

// CoordFormant defines the -coordFormat value to pass to Exiftool, see https://exiftool.org/ExifTool.html#CoordFormat
// Sample :
// e, err := NewExiftool(CoordFormant("%+f"))
func CoordFormant(format string) func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-coordFormat", format)
return nil
}
}

// BackupOriginal backs up the original file when writing the file metadata
// instead of overwriting the original (activates Exiftool's '-overwrite_original' parameter)
// Sample :
Expand Down
54 changes: 54 additions & 0 deletions exiftool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -336,6 +337,59 @@ func TestExtractAllBinaryMetadata(t *testing.T) {
assert.True(t, strings.HasPrefix(osn, "base64"))
}

func TestDateFormat(t *testing.T) {
t.Parallel()

eWithout, err := NewExiftool()
assert.Nil(t, err)
defer eWithout.Close()
metas := eWithout.ExtractMetadata("./testdata/20190404_131804.jpg")
assert.Equal(t, 1, len(metas))
assert.Nil(t, metas[0].Err)
createDate, err := metas[0].GetString("CreateDate")
assert.Nil(t, err)
assert.Equal(t, "2019:04:04 13:18:03", createDate) // backward compatibility

eWith, err := NewExiftool(DateFormant("%s"))
assert.Nil(t, err)
defer eWith.Close()
metas = eWith.ExtractMetadata("./testdata/20190404_131804.jpg")
assert.Equal(t, 1, len(metas))
assert.Nil(t, metas[0].Err)
createDateInt, err := metas[0].GetInt("CreateDate")
assert.Nil(t, err)

// create date is being returned based on the timezone of system this test is being run from
// so we normalize the value by adding it's timezone offset
_, offset := time.Unix(createDateInt, 0).Zone()

assert.Equal(t, int64(1554383883), createDateInt+int64(offset))
}

func TestCoordFormat(t *testing.T) {
t.Parallel()

eWithout, err := NewExiftool()
assert.Nil(t, err)
defer eWithout.Close()
metas := eWithout.ExtractMetadata("./testdata/gps.jpg")
assert.Equal(t, 1, len(metas))
assert.Nil(t, metas[0].Err)
latitude, err := metas[0].GetString("GPSLatitude")
assert.Nil(t, err)
assert.Equal(t, `43 deg 28' 2.81" N`, latitude) // backward compatibility

eWith, err := NewExiftool(CoordFormant("%+f"))
assert.Nil(t, err)
defer eWith.Close()
metas = eWith.ExtractMetadata("./testdata/gps.jpg")
assert.Equal(t, 1, len(metas))
assert.Nil(t, metas[0].Err)
latitude, err = metas[0].GetString("GPSLatitude")
assert.Nil(t, err)
assert.Equal(t, "+43.467448", latitude)
}

func TestSetExiftoolBinaryPath(t *testing.T) {
t.Parallel()

Expand Down
Binary file added testdata/gps.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.