-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (125 loc) · 3.16 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
package main
// TODO tests?
// TODO split to cmd and pkg
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
func main() {
flag.Parse()
start := time.Now()
if gShowVersion {
fmt.Fprintln(os.Stderr, GitVersion)
os.Exit(0)
}
if gTidyCache {
purged, err := tidyCache(gCachePath)
if err != nil {
fmt.Fprintf(os.Stderr, "> tried to tidy cache, but got - %s\n", au.Red(err))
} else {
fmt.Fprintf(os.Stderr, "> purged %d from cache, took %s\n",
au.Cyan(purged), au.Green(time.Since(start)))
}
os.Exit(0)
}
var files []string
var pics []Image
if gInput == "-" {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
file := strings.TrimSpace(scanner.Text())
if fpAbs, err := filepath.Abs(file); err == nil {
files = append(files, fpAbs)
}
}
} else {
if link, err := filepath.EvalSymlinks(gInput); err == nil {
files, _ = getFiles(link)
} else {
fmt.Fprintf(os.Stderr, "> %s\n", au.Red(err))
os.Exit(1)
}
}
files = filterFiles(files, func(fp string) bool {
// making sure it's image formats go supports & not system files
ext := strings.ToLower(filepath.Ext(fp))
return containsStr(gSearchExt, ext) && !isHidden(fp)
})
// deduplicate input
files = mkSetStr(files)
if gVerbose {
fmt.Fprintf(os.Stderr, "> found %d images, took %s\n",
au.Cyan(len(files)), au.Green(time.Since(start)))
}
if len(files) <= 1 {
os.Exit(0)
}
start = time.Now()
usefulCache := 0
if gUseCache {
cachedPics, err := loadCache(gCachePath)
if err != nil {
fmt.Fprintf(os.Stderr, "> tried to load cache, but got - %s\n", au.Red(err))
} else {
files, pics = filterCache(files, cachedPics)
if gVerbose {
usefulCache = len(pics)
fmt.Fprintf(os.Stderr,
"> loaded from cache %d images total, %d will be used, took %s\n",
au.Cyan(len(cachedPics)), au.Cyan(usefulCache), au.Green(time.Since(start)))
}
}
}
start = time.Now()
// calculating image similarity hashes
for pic := range makeImages(files) {
pics = append(pics, pic)
}
if gVerbose {
fmt.Fprintf(os.Stderr, "> processed %d images from disk, %d from cache, took %s\n",
au.Cyan(len(pics)-usefulCache), au.Cyan(usefulCache), au.Green(time.Since(start)))
}
start = time.Now()
if gUseCache && len(pics)-usefulCache > 0 {
cachedPics, _ := loadCache(gCachePath)
for _, img := range pics {
cachedPics[img.fp] = img
}
err := storeCache(gCachePath, cachedPics)
if err != nil {
fmt.Fprintf(os.Stderr, "> tried to update cache, but got - %s\n", au.Red(err))
} else {
if gVerbose {
fmt.Fprintf(os.Stderr, "> updated cache, took %s\n", au.Green(time.Since(start)))
}
}
}
start = time.Now()
// searching for similar images
count := 0
var groups [][]string
for group := range findDups(pics) {
groups = append(groups, group)
count += len(group)
}
if gExportJSON {
byt, _ := json.MarshalIndent(groups, "", " ")
fmt.Println(string(byt))
} else {
for _, group := range groups {
for _, fp := range group {
fmt.Println(fp)
}
}
}
if gVerbose {
fmt.Fprintf(os.Stderr, "> found %d similar images, took %s\n",
au.Cyan(count), au.Green(time.Since(start)))
}
}