-
Notifications
You must be signed in to change notification settings - Fork 69
/
main.go
163 lines (131 loc) · 4.47 KB
/
main.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
// This tool dumps EXIF information from images.
//
// Example command-line:
//
// exif-read-tool -filepath <file-path>
//
// Example Output:
//
// IFD=[IfdIdentity<PARENT-NAME=[] NAME=[IFD]>] ID=(0x010f) NAME=[Make] COUNT=(6) TYPE=[ASCII] VALUE=[Canon]
// IFD=[IfdIdentity<PARENT-NAME=[] NAME=[IFD]>] ID=(0x0110) NAME=[Model] COUNT=(22) TYPE=[ASCII] VALUE=[Canon EOS 5D Mark III]
// IFD=[IfdIdentity<PARENT-NAME=[] NAME=[IFD]>] ID=(0x0112) NAME=[Orientation] COUNT=(1) TYPE=[SHORT] VALUE=[1]
// IFD=[IfdIdentity<PARENT-NAME=[] NAME=[IFD]>] ID=(0x011a) NAME=[XResolution] COUNT=(1) TYPE=[RATIONAL] VALUE=[72/1]
// ...
package main
import (
"fmt"
"os"
"encoding/json"
"io/ioutil"
"github.com/dsoprea/go-logging"
"github.com/jessevdk/go-flags"
"github.com/dsoprea/go-exif/v2"
"github.com/dsoprea/go-exif/v2/common"
)
const (
thumbnailFilenameIndexPlaceholder = "<index>"
)
var (
mainLogger = log.NewLogger("main.main")
)
// IfdEntry is a JSON model for representing a single tag.
type IfdEntry struct {
IfdPath string `json:"ifd_path"`
FqIfdPath string `json:"fq_ifd_path"`
IfdIndex int `json:"ifd_index"`
TagId uint16 `json:"tag_id"`
TagName string `json:"tag_name"`
TagTypeId exifcommon.TagTypePrimitive `json:"tag_type_id"`
TagTypeName string `json:"tag_type_name"`
UnitCount uint32 `json:"unit_count"`
Value interface{} `json:"value"`
ValueString string `json:"value_string"`
}
type parameters struct {
Filepath string `short:"f" long:"filepath" required:"true" description:"File-path of image"`
PrintAsJson bool `short:"j" long:"json" description:"Print out as JSON"`
IsVerbose bool `short:"v" long:"verbose" description:"Print logging"`
ThumbnailOutputFilepath string `short:"t" long:"thumbnail-output-filepath" description:"File-path to write thumbnail to (if present)"`
DoNotPrintTags bool `short:"n" long:"no-tags" description:"Do not actually print tags. Good for auditing the logs or merely checking the EXIF structure for errors."`
}
var (
arguments = new(parameters)
)
func main() {
defer func() {
if errRaw := recover(); errRaw != nil {
err := errRaw.(error)
log.PrintError(err)
os.Exit(-2)
}
}()
_, err := flags.Parse(arguments)
if err != nil {
os.Exit(-1)
}
if arguments.IsVerbose == true {
cla := log.NewConsoleLogAdapter()
log.AddAdapter("console", cla)
scp := log.NewStaticConfigurationProvider()
scp.SetLevelName(log.LevelNameDebug)
log.LoadConfiguration(scp)
}
f, err := os.Open(arguments.Filepath)
log.PanicIf(err)
data, err := ioutil.ReadAll(f)
log.PanicIf(err)
rawExif, err := exif.SearchAndExtractExif(data)
if err != nil {
if err == exif.ErrNoExif {
fmt.Printf("No EXIF data.\n")
os.Exit(1)
}
log.Panic(err)
}
mainLogger.Debugf(nil, "EXIF blob is (%d) bytes.", len(rawExif))
// Run the parse.
entries, err := exif.GetFlatExifData(rawExif)
log.PanicIf(err)
// Write the thumbnail is requested and present.
thumbnailOutputFilepath := arguments.ThumbnailOutputFilepath
if thumbnailOutputFilepath != "" {
im := exif.NewIfdMappingWithStandard()
ti := exif.NewTagIndex()
_, index, err := exif.Collect(im, ti, rawExif)
log.PanicIf(err)
var thumbnail []byte
if ifd, found := index.Lookup[exif.ThumbnailFqIfdPath]; found == true {
thumbnail, err = ifd.Thumbnail()
if err != nil && err != exif.ErrNoThumbnail {
log.Panic(err)
}
}
if thumbnail == nil {
mainLogger.Debugf(nil, "No thumbnails found.")
} else {
if arguments.PrintAsJson == false {
fmt.Printf("Writing (%d) bytes for thumbnail: [%s]\n", len(thumbnail), thumbnailOutputFilepath)
fmt.Printf("\n")
}
err := ioutil.WriteFile(thumbnailOutputFilepath, thumbnail, 0644)
log.PanicIf(err)
}
}
if arguments.DoNotPrintTags == false {
if arguments.PrintAsJson == true {
data, err := json.MarshalIndent(entries, "", " ")
log.PanicIf(err)
fmt.Println(string(data))
} else {
thumbnailTags := 0
for _, entry := range entries {
fmt.Printf("IFD-PATH=[%s] ID=(0x%04x) NAME=[%s] COUNT=(%d) TYPE=[%s] VALUE=[%s]\n", entry.IfdPath, entry.TagId, entry.TagName, entry.UnitCount, entry.TagTypeName, entry.Formatted)
}
fmt.Printf("\n")
if thumbnailTags == 2 {
fmt.Printf("There is a thumbnail.\n")
fmt.Printf("\n")
}
}
}
}