Skip to content
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
37 changes: 19 additions & 18 deletions exiftool.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var ErrNotExist = errors.New("file does not exist")

// Exiftool is the exiftool utility wrapper
type Exiftool struct {
lock sync.Mutex
stdin io.WriteCloser
stdout io.ReadCloser
scanout *bufio.Scanner
lock sync.Mutex
stdin io.WriteCloser
stdMergedOut io.ReadCloser
scanMergedOut *bufio.Scanner
}

// NewExiftool instanciates a new Exiftool with configuration functions. If anything went
Expand All @@ -44,18 +44,19 @@ func NewExiftool(opts ...func(*Exiftool) error) (*Exiftool, error) {
}

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

cmd.Stdout = w
cmd.Stderr = w

var err error
if e.stdin, err = cmd.StdinPipe(); err != nil {
return nil, fmt.Errorf("error when piping stdin: %w", err)
}

if e.stdout, err = cmd.StdoutPipe(); err != nil {
return nil, fmt.Errorf("error when piping stdout: %w", err)
}

e.scanout = bufio.NewScanner(e.stdout)
e.scanout.Split(splitReadyToken)
e.scanMergedOut = bufio.NewScanner(r)
e.scanMergedOut.Split(splitReadyToken)

if err = cmd.Start(); err != nil {
return nil, fmt.Errorf("error when executing commande: %w", err)
Expand All @@ -77,8 +78,8 @@ func (e *Exiftool) Close() error {
}

var errs []error
if err := e.stdout.Close(); err != nil {
errs = append(errs, fmt.Errorf("error while closing stdout: %w", err))
if err := e.stdMergedOut.Close(); err != nil {
errs = append(errs, fmt.Errorf("error while closing stdMergedOut: %w", err))
}

if err := e.stdin.Close(); err != nil {
Expand Down Expand Up @@ -120,19 +121,19 @@ func (e *Exiftool) ExtractMetadata(files ...string) []FileMetadata {
fmt.Fprintln(e.stdin, f)
fmt.Fprintln(e.stdin, executeArg)

if !e.scanout.Scan() {
fms[i].Err = fmt.Errorf("nothing on stdout")
if !e.scanMergedOut.Scan() {
fms[i].Err = fmt.Errorf("nothing on stdMergedOut")
continue
}

if e.scanout.Err() != nil {
fms[i].Err = fmt.Errorf("error while reading stdout: %w", e.scanout.Err())
if e.scanMergedOut.Err() != nil {
fms[i].Err = fmt.Errorf("error while reading stdMergedOut: %w", e.scanMergedOut.Err())
continue
}

var m []map[string]interface{}
if err := json.Unmarshal(e.scanout.Bytes(), &m); err != nil {
fms[i].Err = fmt.Errorf("error during unmarshaling (%v): %w)", e.scanout.Bytes(), err)
if err := json.Unmarshal(e.scanMergedOut.Bytes(), &m); err != nil {
fms[i].Err = fmt.Errorf("error during unmarshaling (%v): %w)", e.scanMergedOut.Bytes(), err)
continue
}

Expand Down
6 changes: 3 additions & 3 deletions exiftool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestCloseNominal(t *testing.T) {

r := readWriteCloserMock{closed: &rClosed}
w := readWriteCloserMock{closed: &wClosed}
e := Exiftool{stdin: r, stdout: w}
e := Exiftool{stdin: r, stdMergedOut: w}

assert.Nil(t, e.Close())
assert.True(t, rClosed)
Expand All @@ -149,7 +149,7 @@ func TestCloseErrorOnStdin(t *testing.T) {

r := readWriteCloserMock{closed: &rClosed, closeErr: fmt.Errorf("error")}
w := readWriteCloserMock{closed: &wClosed}
e := Exiftool{stdin: r, stdout: w}
e := Exiftool{stdin: r, stdMergedOut: w}

assert.NotNil(t, e.Close())
assert.True(t, rClosed)
Expand All @@ -161,7 +161,7 @@ func TestCloseErrorOnStdout(t *testing.T) {

r := readWriteCloserMock{closed: &rClosed}
w := readWriteCloserMock{closed: &wClosed, closeErr: fmt.Errorf("error")}
e := Exiftool{stdin: r, stdout: w}
e := Exiftool{stdin: r, stdMergedOut: w}

assert.NotNil(t, e.Close())
assert.True(t, rClosed)
Expand Down