-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.go
122 lines (96 loc) · 2.16 KB
/
match.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
package main
import (
"embed"
"errors"
"image"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"slices"
"strings"
"github.com/vitali-fedulov/images4"
)
func matchAggregator(icon images4.IconT) bool {
for _, match := range matchImages {
if images4.Similar(icon, match) {
return true
}
}
return false
}
func matchDuplicates(d []dirEntryResponse) {
for i, search := range d {
zero := i + 1
if zero > len(d) {
break
}
for j, match := range d[zero:] {
if images4.Similar(search.icon, match.icon) {
var path string
var dir = filepath.Dir(search.filename)
if len(d)-j > 2*zero {
path = match.filename
logToFile.Printf("[duplicate image] at %s | %s is a duplicate of %s", dir, path, search.filename)
d = slices.Delete(d, j+zero, j+zero+1)
} else {
path = search.filename
logToFile.Printf("[duplicate image] at %s | %s is a duplicate of %s", dir, path, match.filename)
d = slices.Delete(d, i, i+1)
}
deleteDirEntry(path)
matchDuplicates(d)
}
}
}
}
//go:embed images
var images embed.FS
func initialize() error {
if err := filepath.WalkDir(options.Custom, customInitializer); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Panicf("failed to read custom images, error:\n%s", err.Error())
}
return fs.WalkDir(images, "images", initializer)
}
func initializer(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
submatch := func(m string) bool { return strings.Contains(path, m) }
if !slices.ContainsFunc(options.Sites, submatch) {
return nil
}
data, err := images.Open(path)
if err != nil {
return err
}
defer data.Close()
return parseImage(data)
}
func customInitializer(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !isSupportedImage(d) {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
return parseImage(file)
}
func parseImage(file io.Reader) error {
img, _, err := image.Decode(file)
if err != nil {
return err
}
matchImages = append(matchImages, images4.Icon(img))
return nil
}
var matchImages []images4.IconT