Skip to content

Commit

Permalink
Merge pull request #37 from barasher/35_binaryLocation
Browse files Browse the repository at this point in the history
Windows support (exiftool binary location)
  • Loading branch information
barasher authored Jul 4, 2021
2 parents 5df6a0f + c84f73c commit c84dfd8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
37 changes: 26 additions & 11 deletions exiftool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"errors"
)

var binary = "exiftool"
var executeArg = "-execute"
var initArgs = []string{"-stay_open", "True", "-@", "-", "-common_args"}
var extractArgs = []string{"-j"}
Expand All @@ -25,20 +24,23 @@ var ErrNotExist = errors.New("file does not exist")

// Exiftool is the exiftool utility wrapper
type Exiftool struct {
lock sync.Mutex
stdin io.WriteCloser
stdMergedOut io.ReadCloser
scanMergedOut *bufio.Scanner
bufferSet bool
buffer []byte
bufferMaxSize int
extraInitArgs []string
lock sync.Mutex
stdin io.WriteCloser
stdMergedOut io.ReadCloser
scanMergedOut *bufio.Scanner
bufferSet bool
buffer []byte
bufferMaxSize int
extraInitArgs []string
exiftoolBinPath string
}

// NewExiftool instanciates a new Exiftool with configuration functions. If anything went
// wrong, a non empty error will be returned.
func NewExiftool(opts ...func(*Exiftool) error) (*Exiftool, error) {
e := Exiftool{}
e := Exiftool{
exiftoolBinPath: exiftoolBinary,
}

for _, opt := range opts {
if err := opt(&e); err != nil {
Expand All @@ -47,7 +49,7 @@ func NewExiftool(opts ...func(*Exiftool) error) (*Exiftool, error) {
}

args := append(initArgs, e.extraInitArgs...)
cmd := exec.Command(binary, args...)
cmd := exec.Command(e.exiftoolBinPath, args...)
r, w := io.Pipe()
e.stdMergedOut = r

Expand Down Expand Up @@ -215,3 +217,16 @@ func ExtractAllBinaryMetadata() func(*Exiftool) error {
return nil
}
}

// SetExiftoolBinaryPath sets exiftool's binary path. When not specified, the binary will have to be in $PATH
// Sample :
// e, err := NewExiftool(SetExiftoolBinaryPath("/usr/bin/exiftool"))
func SetExiftoolBinaryPath(p string) func(*Exiftool) error {
return func(e *Exiftool) error {
if _, err := os.Stat(p); err != nil {
return fmt.Errorf("error while checking if path '%v' exists: %w", p, err)
}
e.exiftoolBinPath = p
return nil
}
}
27 changes: 27 additions & 0 deletions exiftool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"testing"
"os/exec"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -296,3 +297,29 @@ func TestExtractAllBinaryMetadata(t *testing.T) {
assert.Nil(t, err)
assert.True(t, strings.HasPrefix(osn, "base64"))
}

func TestSetExiftoolBinaryPath(t *testing.T) {
// default
eDefault, err := NewExiftool()
assert.Nil(t, err)
defer eDefault.Close()
f := eDefault.ExtractMetadata("./testdata/20190404_131804.jpg")
assert.Equal(t, 1, len(f))
assert.Nil(t, f[0].Err)

// set path
exiftoolPath, err := exec.LookPath(exiftoolBinary)
assert.Nil(t, err)
t.Logf("exiftool path: %v", exiftoolPath)
eSet, err := NewExiftool(SetExiftoolBinaryPath(exiftoolPath))
assert.Nil(t, err)
defer eSet.Close()
f = eSet.ExtractMetadata("./testdata/20190404_131804.jpg")
assert.Equal(t, 1, len(f))
assert.Nil(t, f[0].Err)

// error on init
_, err = NewExiftool(SetExiftoolBinaryPath("/non/existing/path"))
assert.NotNil(t, err)

}
1 change: 1 addition & 0 deletions platform_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package exiftool

var readyToken = []byte("{ready}\n")
var exiftoolBinary = "exiftool"
1 change: 1 addition & 0 deletions platform_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package exiftool

var readyToken = []byte("{ready}\n")
var exiftoolBinary = "exiftool"
1 change: 1 addition & 0 deletions platform_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package exiftool

var readyToken = []byte("{ready}\n")
var exiftoolBinary = "exiftool"
1 change: 1 addition & 0 deletions platform_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package exiftool

var readyToken = []byte("{ready}\r\n")
var exiftoolBinary = "exiftool.exe"

0 comments on commit c84dfd8

Please sign in to comment.