-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexiftool.go
163 lines (133 loc) · 3.37 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
package exiftool
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os/exec"
"runtime"
"strings"
"sync"
)
type ExifTool struct {
lock sync.Mutex
cmd *exec.Cmd
stdin io.WriteCloser
mergedOut io.ReadCloser
mergedOutScanner *bufio.Scanner
}
func NewExifTool(path string) (*ExifTool, error) {
et := ExifTool{}
args := []string{"-stay_open", "True", "-@", "-"}
et.cmd = exec.Command(path, args...)
if runtime.GOOS == "windows" {
hideWindow(et.cmd)
}
read, write := io.Pipe()
et.mergedOut = read
et.cmd.Stdout = write
et.cmd.Stderr = write
var err error
et.stdin, err = et.cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("error piping stdin: %w", err)
}
scanBuf := make([]byte, 64*1000)
et.mergedOutScanner = bufio.NewScanner(read)
et.mergedOutScanner.Buffer(scanBuf, 1024*1000)
et.mergedOutScanner.Split(splitByReadyToken)
err = et.cmd.Start()
if err != nil {
return nil, err
}
return &et, nil
}
func (et *ExifTool) Close() error {
et.lock.Lock()
defer et.lock.Unlock()
closeArgs := []string{"-stay_open", "False"}
for _, closeArg := range closeArgs {
_, err := fmt.Fprintln(et.stdin, closeArg)
if err != nil {
return err
}
}
err := et.mergedOut.Close()
if err != nil {
return err
}
err = et.stdin.Close()
if err != nil {
return err
}
err = et.cmd.Wait()
if err != nil {
return err
}
return nil
}
// NOTE: Param -j/-json for JSON formatted output
func (et *ExifTool) Exec(params ...string) (out string, outErrors []string, outWarnings []string, error error) {
et.lock.Lock()
defer et.lock.Unlock()
for _, param := range params {
_, err := fmt.Fprintln(et.stdin, param)
if err != nil {
return "", nil, nil, err
}
}
_, err := fmt.Fprintln(et.stdin, "-execute")
if err != nil {
return "", nil, nil, err
}
scanOk := et.mergedOutScanner.Scan()
scanErr := et.mergedOutScanner.Err()
if scanErr != nil {
return "", nil, nil, fmt.Errorf("error reading from scanner: %w", scanErr)
}
if !scanOk {
return "", nil, nil, fmt.Errorf("error reading from scanner")
}
out = et.mergedOutScanner.Text()
lines := strings.Split(out, "\n")
for _, line := range lines {
if strings.Contains(line, "Warning:") {
warning := strings.TrimSpace(strings.TrimPrefix(line, "Warning:"))
outWarnings = append(outWarnings, warning)
} else if strings.Contains(line, "Error:") {
err := strings.TrimSpace(strings.TrimPrefix(line, "Error:"))
outErrors = append(outErrors, err)
}
}
return out, outErrors, outWarnings, nil
}
func (et *ExifTool) GetMetadata(file ...string) ([]map[string]interface{}, error) {
et.lock.Lock()
defer et.lock.Unlock()
params := []string{"-json"}
params = append(params, file...)
for _, param := range params {
_, err := fmt.Fprintln(et.stdin, param)
if err != nil {
return nil, err
}
}
_, err := fmt.Fprintln(et.stdin, "-execute")
if err != nil {
return nil, err
}
scanOk := et.mergedOutScanner.Scan()
scanErr := et.mergedOutScanner.Err()
if scanErr != nil {
return nil, fmt.Errorf("error reading from scanner: %w", scanErr)
}
if !scanOk {
return nil, fmt.Errorf("error reading from scanner")
}
var resMap []map[string]interface{}
err = json.Unmarshal(et.mergedOutScanner.Bytes(), &resMap)
if err != nil {
return nil, fmt.Errorf("error during unmarshaling (%v): %w)", string(et.mergedOutScanner.Bytes()), err)
}
return resMap, nil
}