-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (126 loc) · 3.96 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var (
destDir, prevDir, unknownDir, mediainfo, renameString, excludeString string
movieExts, picExts []string
debug, noRenameDest, renameSource bool
byteCount int64
excludeRegex *regexp.Regexp
)
// object refers to the file we are examining in walkFunc
type object struct {
FullSourcePath string
SourceName string
SourcePath string
SourceSize int64
FullDestPath string
DestName string
DestPath string
FH *os.File
}
func main() {
var startDir, pics, movs string
flag.StringVar(&startDir, "startDir", "", "Start traversing from here")
flag.StringVar(&destDir, "destDir", "", "Put renamed files here")
flag.StringVar(&unknownDir, "unknownDir", "", "Where to put files with unknown metadata")
flag.StringVar(&pics, "picExts", "jpg,jpeg,gif,png,aae,tif,thm", "Comma delimited list of picture extensions")
flag.StringVar(&movs, "movExts", "mov,mp4,avi,mod,m4a,m4v,lrv", "Comma delimited list of movie extensions")
flag.StringVar(&excludeString, "exclude", "", "regex for file excludes")
flag.StringVar(&mediainfo, "mediainfo", "/usr/bin/mediainfo", "Path to mediainfo binary")
flag.BoolVar(&debug, "debug", false, "Set for more logging")
flag.BoolVar(&noRenameDest, "noRenameDest", false, "Do not rename files on destination - keep existing source filename")
flag.BoolVar(&renameSource, "renameSource", false, "Rename source file after successful copy")
flag.Parse()
if len(startDir) == 0 || len(destDir) == 0 {
log.Fatalf("You did not specify either a starting or destination dir")
}
if len(excludeString) > 0 {
excludeRegex = regexp.MustCompile(excludeString)
}
_, err := os.Stat(mediainfo)
if err != nil {
log.Fatalf("Did not find mediainfo at: %s", mediainfo)
}
// if we want to rename source file after copying, define our suffix
if renameSource {
renameString = fmt.Sprintf("copied-%d", time.Now().Unix())
}
// set this to our destination dir initially - it will be modified the next time we can successfully extract the time/date from a file
prevDir = destDir
picExts = parseExtensions(pics)
movieExts = parseExtensions(movs)
t1 := time.Now()
err = filepath.Walk(startDir, walkFunc)
if err != nil {
log.Fatalf("filepath.Walk returned error: %v.\n", err)
}
throughput := float64(byteCount) / time.Since(t1).Seconds()
log.Printf("Finished script. Copied %d bytes in %v. %.2f bytes/sec", byteCount, time.Since(t1), throughput)
}
func walkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Got error entering walkFunc for file %s - skipping, error: %v", path, err)
return nil
}
if info.Mode() == os.ModeSymlink {
if debug {
log.Printf("Skipping %s - symlink", path)
}
return nil
}
if info.IsDir() {
if debug {
log.Printf("Skipping dir: %s", path)
}
return nil
}
// ignore ._IMG files
if info.Size() == 4096 && strings.HasPrefix(filepath.Base(path), "._") {
if debug {
log.Printf("Skipping file: %s", path)
}
return nil
}
if excludeRegex != nil {
if excludeRegex.MatchString(path) {
if debug {
log.Printf("Skipping %s - found excludeString: %s", path, excludeString)
}
return nil
}
}
o := &object{
SourceName: filepath.Base(path),
SourcePath: filepath.Dir(path),
SourceSize: info.Size(),
FullSourcePath: path,
}
fields := strings.Split(o.SourceName, `.`)
extension := fields[len(fields)-1]
lowerExtension := strings.ToLower(extension)
for _, e := range picExts {
if lowerExtension == e {
examinePic(o)
return nil
}
}
for _, e := range movieExts {
if lowerExtension == e {
examineMov(o)
return nil
}
}
if debug {
log.Printf("Not doing anything with %s - unknown extension", path)
}
return nil
}