Skip to content

Commit

Permalink
Configures exiftool stdout and stderr command buffer (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
barasher committed Aug 25, 2020
1 parent 3055e41 commit 8b99a50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions exiftool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Exiftool struct {
stdin io.WriteCloser
stdMergedOut io.ReadCloser
scanMergedOut *bufio.Scanner
bufferSet bool
buffer []byte
bufferMaxSize int
}

// NewExiftool instanciates a new Exiftool with configuration functions. If anything went
Expand All @@ -55,6 +58,9 @@ func NewExiftool(opts ...func(*Exiftool) error) (*Exiftool, error) {
}

e.scanMergedOut = bufio.NewScanner(r)
if e.bufferSet {
e.scanMergedOut.Buffer(e.buffer, e.bufferMaxSize)
}
e.scanMergedOut.Split(splitReadyToken)

if err = cmd.Start(); err != nil {
Expand Down Expand Up @@ -154,3 +160,16 @@ func splitReadyToken(data []byte, atEOF bool) (int, []byte, error) {

return idx + readyTokenLen, data[:idx], nil
}

// Buffer defines the buffer used to read from stdout and stderr, see https://golang.org/pkg/bufio/#Scanner.Buffer
// Sample :
// buf := make([]byte, 128*1000)
// e, err := e, err := NewExiftool(Buffer(buf, 64*1000))
func Buffer(buf []byte, max int) func(*Exiftool) error {
return func(e *Exiftool) error {
e.bufferSet = true
e.buffer = buf
e.bufferMaxSize = max
return nil
}
}
24 changes: 24 additions & 0 deletions exiftool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,27 @@ func (e readWriteCloserMock) Close() error {
*(e.closed) = true
return e.closeErr
}

func TestBuffer(t *testing.T) {
e, err := NewExiftool()
assert.Nil(t, err)
defer e.Close()
assert.Equal(t, false, e.bufferSet)

buf := make([]byte, 128)
assert.Nil(t, Buffer(buf, 64)(e))
assert.Equal(t, true, e.bufferSet)
assert.Equal(t, buf, e.buffer)
assert.Equal(t, 64, e.bufferMaxSize)
}

func TestNewExifTool_WithBuffer(t *testing.T) {
buf := make([]byte, 128*1000)
e, err := NewExiftool(Buffer(buf, 64*1000))
assert.Nil(t, err)
defer e.Close()

metas := e.ExtractMetadata("./testdata/20190404_131804.jpg")
assert.Equal(t, 1, len(metas))
assert.Nil(t, metas[0].Err)
}

0 comments on commit 8b99a50

Please sign in to comment.