-
Notifications
You must be signed in to change notification settings - Fork 45
/
exiftool.go
443 lines (383 loc) · 11.7 KB
/
exiftool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package exiftool
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"time"
)
const writeMetadataSuccessTokenLen = len(writeMetadataSuccessToken)
var executeArg = "-execute"
var initArgs = []string{"-stay_open", "True", "-@", "-"}
var extractArgs = []string{"-j"}
var closeArgs = []string{"-stay_open", "False", executeArg}
var readyTokenLen = len(readyToken)
// WaitTimeout specifies the duration to wait for exiftool to exit when closing before timing out
var WaitTimeout = time.Second
// ErrNotExist is a sentinel error for non existing file
var ErrNotExist = errors.New("file does not exist")
// ErrNotFile is a sentinel error that is returned when a folder is provided instead of a rerular file
var ErrNotFile = errors.New("can't extract metadata from folder")
// ErrBufferTooSmall is a sentinel error that is returned when the buffer used to store Exiftool's output is too small.
var ErrBufferTooSmall = errors.New("exiftool's buffer too small (see Buffer init option)")
// 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
exiftoolBinPath string
cmd *exec.Cmd
backupOriginal bool
clearFieldsBeforeWriting bool
}
// 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{
exiftoolBinPath: exiftoolBinary,
}
for _, opt := range opts {
if err := opt(&e); err != nil {
return nil, fmt.Errorf("error when configuring exiftool: %w", err)
}
}
args := append([]string(nil), initArgs...)
if len(e.extraInitArgs) > 0 {
args = append(args, "-common_args")
args = append(args, e.extraInitArgs...)
}
e.cmd = exec.Command(e.exiftoolBinPath, args...)
r, w := io.Pipe()
e.stdMergedOut = r
e.cmd.Stdout = w
e.cmd.Stderr = w
var err error
if e.stdin, err = e.cmd.StdinPipe(); err != nil {
return nil, fmt.Errorf("error when piping stdin: %w", err)
}
e.scanMergedOut = bufio.NewScanner(r)
if e.bufferSet {
e.scanMergedOut.Buffer(e.buffer, e.bufferMaxSize)
}
e.scanMergedOut.Split(splitReadyToken)
if err = e.cmd.Start(); err != nil {
return nil, fmt.Errorf("error when executing command: %w", err)
}
return &e, nil
}
// Close closes exiftool. If anything went wrong, a non empty error will be returned
func (e *Exiftool) Close() error {
e.lock.Lock()
defer e.lock.Unlock()
for _, v := range closeArgs {
_, err := fmt.Fprintln(e.stdin, v)
if err != nil {
return err
}
}
var errs []error
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 {
errs = append(errs, fmt.Errorf("error while closing stdin: %w", err))
}
ch := make(chan struct{})
go func() {
if e.cmd != nil {
if err := e.cmd.Wait(); err != nil {
errs = append(errs, fmt.Errorf("error while waiting for exiftool to exit: %w", err))
}
}
ch <- struct{}{}
close(ch)
}()
// Wait for wait to finish or timeout
select {
case <-ch:
case <-time.After(WaitTimeout):
errs = append(errs, errors.New("Timed out waiting for exiftool to exit"))
}
if len(errs) > 0 {
return fmt.Errorf("error while closing exiftool: %v", errs)
}
return nil
}
// ExtractMetadata extracts metadata from files
func (e *Exiftool) ExtractMetadata(files ...string) []FileMetadata {
e.lock.Lock()
defer e.lock.Unlock()
fms := make([]FileMetadata, len(files))
for i, f := range files {
fms[i].File = f
s, err := os.Stat(f)
if err != nil {
fms[i].Err = err
if os.IsNotExist(err) {
fms[i].Err = ErrNotExist
}
continue
}
if s.IsDir() {
fms[i].Err = ErrNotFile
continue
}
for _, curA := range extractArgs {
if _, err := fmt.Fprintln(e.stdin, curA); err != nil {
fms[i].Err = err
continue
}
}
if _, err := fmt.Fprintln(e.stdin, f); err != nil {
fms[i].Err = err
continue
}
if _, err := fmt.Fprintln(e.stdin, executeArg); err != nil {
fms[i].Err = err
continue
}
scanOk := e.scanMergedOut.Scan()
scanErr := e.scanMergedOut.Err()
if scanErr != nil {
if scanErr == bufio.ErrTooLong {
fms[i].Err = ErrBufferTooSmall
continue
}
fms[i].Err = fmt.Errorf("error while reading stdMergedOut: %w", e.scanMergedOut.Err())
continue
}
if !scanOk {
fms[i].Err = fmt.Errorf("error while reading stdMergedOut: EOF")
continue
}
var m []map[string]interface{}
if err := json.Unmarshal(e.scanMergedOut.Bytes(), &m); err != nil {
fms[i].Err = fmt.Errorf("error during unmarshaling (%v): %w)", string(e.scanMergedOut.Bytes()), err)
continue
}
fms[i].Fields = m[0]
}
return fms
}
// WriteMetadata writes the given metadata for each file.
// Any errors will be saved to FileMetadata.Err
// Note: If you're reusing an existing FileMetadata instance,
// you should nil the Err before passing it to WriteMetadata
func (e *Exiftool) WriteMetadata(fileMetadata []FileMetadata) {
e.lock.Lock()
defer e.lock.Unlock()
for i, md := range fileMetadata {
fileMetadata[i].Err = nil
if _, err := os.Stat(md.File); err != nil {
if os.IsNotExist(err) {
fileMetadata[i].Err = ErrNotExist
continue
}
fileMetadata[i].Err = err
continue
}
if !e.backupOriginal {
if _, err := fmt.Fprintln(e.stdin, "-overwrite_original"); err != nil {
fileMetadata[i].Err = err
continue
}
}
if e.clearFieldsBeforeWriting {
if _, err := fmt.Fprintln(e.stdin, "-All="); err != nil {
fileMetadata[i].Err = err
continue
}
}
for k, v := range md.Fields {
switch v.(type) {
case nil:
if _, err := fmt.Fprintln(e.stdin, "-"+k+"="); err != nil {
fileMetadata[i].Err = err
continue
}
default:
strTab, err := md.GetStrings(k)
if err != nil {
fileMetadata[i].Err = err
continue
}
for _, str := range strTab {
// TODO: support writing an empty string via '^='
if _, err := fmt.Fprintln(e.stdin, "-"+k+"="+str); err != nil {
fileMetadata[i].Err = err
continue
}
}
}
}
if _, err := fmt.Fprintln(e.stdin, md.File); err != nil {
fileMetadata[i].Err = err
continue
}
if _, err := fmt.Fprintln(e.stdin, executeArg); err != nil {
fileMetadata[i].Err = err
continue
}
scanOk := e.scanMergedOut.Scan()
scanErr := e.scanMergedOut.Err()
if scanErr != nil {
if scanErr == bufio.ErrTooLong {
fileMetadata[i].Err = ErrBufferTooSmall
continue
}
fileMetadata[i].Err = fmt.Errorf("error while reading stdMergedOut: %w", e.scanMergedOut.Err())
continue
}
if !scanOk {
fileMetadata[i].Err = fmt.Errorf("error while reading stdMergedOut: EOF")
continue
}
if err := handleWriteMetadataResponse(e.scanMergedOut.Text()); err != nil {
fileMetadata[i].Err = fmt.Errorf("Error writing metadata: %w", err)
continue
}
}
}
func splitReadyToken(data []byte, atEOF bool) (int, []byte, error) {
idx := bytes.Index(data, readyToken)
if idx == -1 {
if atEOF && len(data) > 0 {
return 0, data, fmt.Errorf("no final token found")
}
return 0, nil, nil
}
return idx + readyTokenLen, data[:idx], nil
}
func handleWriteMetadataResponse(resp string) error {
if strings.HasSuffix(resp, writeMetadataSuccessToken) {
return nil
}
return errors.New(strings.TrimSpace(resp))
}
// 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 := 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
}
}
// Charset defines the -charset value to pass to Exiftool, see https://exiftool.org/faq.html#Q10 and https://exiftool.org/faq.html#Q18
// Sample :
// e, err := NewExiftool(Charset("filename=utf8"))
func Charset(charset string) func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-charset", charset)
return nil
}
}
// Api defines an -api value to pass to Exiftool, see https://www.exiftool.org/exiftool_pod.html#Advanced-options
// Sample :
// e, err := NewExiftool(Api("QuickTimeUTC"))
func Api(apiValue string) func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-api", apiValue)
return nil
}
}
// NoPrintConversion enables 'No print conversion' mode, see https://exiftool.org/exiftool_pod.html.
// Sample :
// e, err := NewExiftool(NoPrintConversion())
func NoPrintConversion() func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-n")
return nil
}
}
// ExtractEmbedded extracts embedded metadata from files (activates Exiftool's '-ee' paramater)
// Sample :
// e, err := NewExiftool(ExtractEmbedded())
func ExtractEmbedded() func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-ee")
return nil
}
}
// ExtractAllBinaryMetadata extracts all binary metadata (activates Exiftool's '-b' paramater)
// Sample :
// e, err := NewExiftool(ExtractAllBinaryMetadata())
func ExtractAllBinaryMetadata() func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-b")
return nil
}
}
// 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
}
}
// PrintGroupNames prints the group names for each tag based on the pass group number(s), (activates Exiftool's '-G' paramater)
// Sample :
// e, err := NewExiftool(PrintGroupNames("0"))
func PrintGroupNames(groupNumbers string) func(*Exiftool) error {
return func(e *Exiftool) error {
e.extraInitArgs = append(e.extraInitArgs, "-G"+groupNumbers)
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 :
// e, err := NewExiftool(BackupOriginal())
func BackupOriginal() func(*Exiftool) error {
return func(e *Exiftool) error {
e.backupOriginal = true
return nil
}
}
// ClearFieldsBeforeWriting will clear existing fields (e.g. tags) in the file before writing any
// new tags
// Sample :
// e, err := NewExiftool(ClearFieldsBeforeWriting())
func ClearFieldsBeforeWriting() func(*Exiftool) error {
return func(e *Exiftool) error {
e.clearFieldsBeforeWriting = true
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
}
}