/
main.go
178 lines (141 loc) · 3.44 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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package main
import (
"flag"
"fmt"
"github.com/jteeuwen/imghash"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"os"
"path"
"path/filepath"
"runtime/pprof"
"strings"
"time"
)
var (
db = imghash.NewDatabase()
hasher = imghash.Average
dbfile = flag.String("db", "", "")
cpu = flag.String("cpu", "", "")
)
func main() {
parseArgs()
// Enable CPU profiling of requested.
if len(*cpu) > 0 {
fd, err := os.Create(*cpu)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
defer fd.Close()
pprof.StartCPUProfile(fd)
defer pprof.StopCPUProfile()
}
// Load database file.
if err := db.Load(*dbfile); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
// Do not quit here. The file may yet have to be created.
}
defer db.Save(*dbfile)
// Begin hashing of files
var fileCount, fileSize uint64
start := time.Now()
fmt.Printf("* Indexing %s...\n", db.Root)
filepath.Walk(db.Root, func(file string, stat os.FileInfo, e error) (err error) {
if e != nil {
return e
}
if stat.IsDir() {
return
}
switch strings.ToLower(path.Ext(file)) {
case ".png", ".jpg", ".jpeg", ".gif":
if writeDB(file, stat.ModTime().Unix()) {
fileCount++
fileSize += uint64(stat.Size())
}
}
return
})
// Print some statistics.
fmt.Println("* Done.")
fmt.Printf("* %d image(s) (%s) hashed in %s\n",
fileCount, prettySize(fileSize), time.Since(start))
}
// writeDB creates a hash for the given file and inserts/updates
// the database with its info if necessary.
func writeDB(file string, modtime int64) bool {
fmt.Printf("* %s\n", file)
sfile, err := filepath.Abs(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", file, err)
return false
}
sfile = strings.Replace(sfile, db.Root, "", 1)
if !db.IsNew(sfile, modtime) {
return false
}
hash, err := getHash(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", file, err)
return false
}
db.Set(sfile, modtime, hash)
return true
}
// getHash creates a perceptual hash for the given file.
func getHash(file string) (uint64, error) {
fd, err := os.Open(file)
if err != nil {
return 0, err
}
defer fd.Close()
img, _, err := image.Decode(fd)
if err != nil {
return 0, err
}
return hasher(img), nil
}
// prettySize returns a human-friendly version of the given
// file size in bytes.
func prettySize(size uint64) string {
var unit int
fsize := float64(size)
for fsize >= 1024.0 {
fsize /= 1024
unit++
}
units := [...]string{"byte(s)", "kb", "mb", "gb", "tb", "pb", "yb"}
return fmt.Sprintf("%.2f %s", fsize, units[unit])
}
// parseArgs processes and validates commandline arguments.
func parseArgs() {
var err error
flag.Usage = func() {
fmt.Printf("Usage: %s [options] <directory path>\n\n", os.Args[0])
fmt.Printf(" -db: Location for the hash database. Alternatively, this can be set\n" +
" in the IMGHASH_DB environment variable.\n")
fmt.Printf(" -cpu: File to write CPU profile to.\n")
fmt.Printf(" -v: Display version information.\n")
}
version := flag.Bool("v", false, "Display version information.")
flag.Parse()
if *version {
fmt.Printf("%s\n", Version())
os.Exit(0)
}
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
db.Root = filepath.Clean(flag.Arg(0))
db.Root, err = filepath.Abs(db.Root)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}