-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (85 loc) · 2.23 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
package main
import (
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/mknote"
"os"
"io/ioutil"
"fmt"
"flag"
"text/template"
"path/filepath"
"bytes"
)
type data struct {
Filename string
Fullname string
Exif *exif.Exif
}
func (b data) String() string {
return fmt.Sprintf("Filename: %s\nFullname: %s\nExif: {\n%s\n}", b.Filename, b.Fullname, b.Exif.String())
}
var Renames map[string]string = make(map[string]string)
var UnittestMode = false
func (v data) Format(fmt string) string {
dt,_ := v.Exif.DateTime()
return dt.Format(fmt)
}
func main() {
testMode := flag.Bool("test", true, "Do not execute changes")
templateString := flag.String("template", `{{.Format ("2006/01")}}/{{.Filename}}`, "Template to use, e.g. " + `{{.Format ("2006-01-02")}}-{{.Format ("2006-01-02 03:04:05"}}`)
flag.Parse()
exif.RegisterParsers(mknote.All...)
t, err := template.New("filename").Parse(*templateString)
if err != nil {
panic(err)
}
fmt.Printf("Test mode is %t\n", *testMode)
files := scanDir(".")
for index := range (files) {
fileName := files[index]
processFile(t, *testMode, fileName)
}
}
func processFile(template *template.Template, testMode bool, fileName string) {
f, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer f.Close()
x, err := exif.Decode(f)
if err != nil {
fmt.Printf("SKIPPED: %s: %s\n", fileName, err)
} else {
context := data{
Exif: x,
Filename: filepath.Base(fileName),
Fullname: fileName,
}
buf := bytes.Buffer{}
err := (*template).Execute(&buf, context)
if err != nil {
panic(err)
}
targetName := buf.String()
fmt.Printf("%s -> %s\n", fileName, targetName)
if !testMode {
os.MkdirAll(filepath.Dir(targetName), os.ModeDir | 0777)
os.Rename(fileName, targetName)
} else if UnittestMode {
Renames[fileName] = targetName
}
}
}
func scanDir(dir string) []string {
listing, _ := ioutil.ReadDir(dir)
result := []string{}
for index := range (listing) {
if (listing[index].IsDir()) {
add := scanDir(dir + string(os.PathSeparator) + listing[index].Name())
result = append(result, add...)
} else if (listing[index].Name()[0] != '.') {
result = append(result, dir + string(os.PathSeparator) + listing[index].Name())
}
}
return result
}