-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
351 lines (280 loc) · 7.24 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/djherbis/times"
"github.com/dsoprea/go-exif/v2"
heic "github.com/dsoprea/go-heic-exif-extractor"
jpeg "github.com/dsoprea/go-jpeg-image-structure"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)
const ExifDateFormat = "2006:01:02 15:04:05"
var skipFiles = []string{
"exe",
"bat",
"ini",
"dropbox",
"app",
}
type Args struct {
InputPath string
OutputPath string
}
type FileInfo struct {
FileName string
FileType string
OriginalPath string
NewPath string
CreatedAt time.Time
// exif data
HasExif bool
CameraMaker string
CameraModel string
TimeOriginal time.Time
TimeDigitized time.Time
FileDateTime time.Time
}
func main() {
var err error
var args Args
flag.StringVar(&args.InputPath, "in", "", "path to working dir")
flag.StringVar(&args.OutputPath, "out", "", "path to working dir")
flag.Parse()
if args.InputPath == "" {
dir, err := os.Executable()
args.InputPath = path.Dir(dir)
if err != nil {
log.Fatalln(err)
}
}
if args.OutputPath == "" {
args.OutputPath = args.InputPath + "/sorted"
}
log.Println("Input path:", args.InputPath)
log.Println("Output path:", args.OutputPath)
if _, err := os.Stat(args.InputPath); os.IsNotExist(err) {
log.Println("input dir does not exist. stopping.")
log.Fatalln(err)
}
if _, err := os.Stat(args.OutputPath); os.IsNotExist(err) {
log.Println("output dir does not exist. trying to create.")
err = os.MkdirAll(args.OutputPath, 0750)
if err != nil {
log.Println("error creating output dir. exiting")
log.Fatalln(err)
}
}
log.Println("LET'S GO")
files, err := ioutil.ReadDir(args.InputPath)
if err != nil {
log.Fatalln(err)
}
//var exifData []byte
var info *FileInfo
for _, file := range files {
if file.IsDir() || !file.Mode().IsRegular() {
continue
}
info = new(FileInfo)
info.OriginalPath = args.InputPath
info.NewPath = args.OutputPath
info.FileName = file.Name()
info.FileType = strings.TrimLeft(strings.ToLower(filepath.Ext(file.Name())), ".")
skip := false
for _, ext := range skipFiles {
if ext == info.FileType {
skip = true
break
}
}
if skip {
continue
}
if t, err := times.Stat(info.GetOriginalFilePath()); err == nil {
info.CreatedAt = t.BirthTime()
} else {
info.CreatedAt = file.ModTime()
}
switch info.FileType {
case "jpeg", "jpg", "jpe":
_ = processExif(info)
case "heic":
_ = processHeic(info)
}
log.Println(info.GetOriginalFilePath(), "->", info.GetOutputFileName())
err = os.MkdirAll(info.GetOutputDir(), 0750)
if err != nil {
log.Println(err)
}
err = os.Rename(info.GetOriginalFilePath(), info.GetOutputFileName())
if err != nil {
log.Println(err)
}
}
}
func processHeic(info *FileInfo) error {
mc, parseErr := heic.NewHeicExifMediaParser().ParseFile(info.GetOriginalFilePath())
if mc != nil {
_, data, err := mc.Exif()
if err != nil {
return err
}
et, err := exif.GetFlatExifData(data)
if err != nil {
return err
}
//tags := rootIfd.EntriesByTagId
//log.Println(tags)
processExifTags(info, et)
} else if parseErr == nil {
// We should never get a `nil` `mc` value back *and* a `nil`
// `parseErr`.
return fmt.Errorf("could not parse JPEG even partially")
} else {
return parseErr
}
return nil
}
func processExif(info *FileInfo) error {
intfc, parseErr := jpeg.NewJpegMediaParser().ParseFile(info.GetOriginalFilePath())
var et []exif.ExifTag
if intfc != nil {
// If the parse failed, we should always still get all of the segments
// that we've encountered so far. It should never be empty, and it
// should be impossible for it to be `nil`. So, if the parse failed but
// we still found EXIF data, just ignore the failure and proceed. We had
// still got what we needed.
sl := intfc.(*jpeg.SegmentList)
if len(sl.Segments()) > 0 {
info.HasExif = true
}
var err error
_, _, et, err = sl.DumpExif()
// There was a parse error and we couldn't find/parse EXIF data. If the
// extraction had already failed above and we were just trying for a
// contingency, fail with that error first.
if err != nil {
if parseErr != nil {
return parseErr
}
return err
}
} else if parseErr == nil {
// We should never get a `nil` `intfc` value back *and* a `nil`
// `parseErr`.
return fmt.Errorf("could not parse JPEG even partially")
} else {
return parseErr
}
// If we get here, we either parsed the JPEG file well or at least parsed
// enough to find EXIF data.
if et == nil {
// The JPEG image parsed fine (if it didn't and we haven't yet
// terminated, we already extracted the EXIF tags above).
sl := intfc.(*jpeg.SegmentList)
var err error
_, _, et, err = sl.DumpExif()
if err != nil {
if err == exif.ErrNoExif {
log.Printf("No EXIF.\n")
}
return err
}
}
processExifTags(info, et)
return nil
}
func processExifTags(info *FileInfo, et []exif.ExifTag) {
if len(et) == 0 {
fmt.Printf("EXIF data is present but empty.\n")
} else {
info.HasExif = true
for _, tag := range et {
tagName := strings.TrimSpace(tag.TagName)
switch tagName {
case "Make":
info.CameraMaker = tag.FormattedFirst
case "Model":
info.CameraModel = tag.FormattedFirst
case "DateTimeOriginal":
info.TimeOriginal, _ = time.Parse(ExifDateFormat, tag.FormattedFirst)
case "DateTimeDigitized":
info.TimeDigitized, _ = time.Parse(ExifDateFormat, tag.FormattedFirst)
case "FileDateTime":
atoi, _ := strconv.Atoi(tag.FormattedFirst)
info.FileDateTime = time.Unix(int64(atoi), 0)
}
}
}
}
func (i *FileInfo) CameraName() string {
result := ""
if i.CameraMaker != "" {
result += strings.TrimSpace(i.CameraMaker) + " "
}
if i.CameraModel != "" {
result += strings.TrimSpace(i.CameraModel)
}
result = strings.TrimSpace(result)
return result
}
// getDateDirName return dir structure for file
func (i *FileInfo) getDateDirName() string {
date := i.getBestDate()
return date.Format("2006") + "/" + date.Format("2006-01-02")
}
// getBestDate Get best file date - either when the image was taken or when it was created
func (i *FileInfo) getBestDate() time.Time {
if !i.TimeOriginal.IsZero() {
return i.TimeOriginal
}
if !i.TimeDigitized.IsZero() {
return i.TimeDigitized
}
if !i.TimeOriginal.IsZero() {
return i.TimeOriginal
}
if !i.CreatedAt.IsZero() {
return i.CreatedAt
}
return time.Now()
}
func (i *FileInfo) GetOutputDir() string {
result := i.NewPath + "/"
if !i.HasExif {
result += "misc/" + i.FileType + "/"
}
result += i.getDateDirName() + "/"
if cam := i.CameraName(); cam != "" {
result += cam + "/"
}
return result
}
func (i *FileInfo) GetOutputFileName() string {
result := path.Clean(i.GetOutputDir() + i.FileName)
c := 0
originalFileName := strings.TrimSuffix(i.FileName, filepath.Ext(i.FileName))
for {
if _, err := os.Stat(result); os.IsNotExist(err) {
break
}
c++
result = path.Clean(i.GetOutputDir() + "/" + originalFileName + "-" + strconv.Itoa(c) + filepath.Ext(i.FileName))
}
return result
}
func (i *FileInfo) GetOriginalFilePath() string {
return i.OriginalPath + "/" + i.FileName
}
func (i *FileInfo) String() string {
jsonData, _ := json.Marshal(i)
return string(jsonData)
}