-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilar.go
180 lines (168 loc) · 3.91 KB
/
similar.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
179
180
package ma
import (
"context"
"errors"
goimage "image"
"io/fs"
"strconv"
"github.com/hashicorp/go-metrics"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
"github.com/vitali-fedulov/images4"
"golang.org/x/sync/errgroup"
)
type analyzer struct {
afs afero.Fs
met *metrics.Metrics
enc Encoder
}
type iconPath struct {
icon images4.IconT
path string
}
func (y *analyzer) icon(path string) (iconPath, error) {
file, err := y.afs.Open(path)
if err != nil {
return iconPath{}, err
}
defer file.Close()
img, _, err := goimage.Decode(file)
if err != nil {
return iconPath{}, err
}
return iconPath{images4.Icon(img), path}, nil
}
func (y *analyzer) icons(ctx context.Context, paths <-chan string, icons chan<- iconPath) error {
for path := range paths {
log.Info().Str("path", path).Msg("reading")
icon, err := y.icon(path)
if err != nil {
if !errors.Is(err, goimage.ErrFormat) {
log.Error().Err(err).Str("path", path).Msg("icons")
y.met.IncrCounter([]string{"similar", "icon", "error"}, 1)
return err
}
log.Warn().Err(err).Str("path", path).Msg("reading")
y.met.IncrCounter([]string{"similar", "icon", "skipped"}, 1)
continue
}
select {
case <-ctx.Done():
return ctx.Err()
case icons <- icon:
y.met.IncrCounter([]string{"similar", "icon", "success"}, 1)
}
}
return nil
}
func (y *analyzer) paths(ctx context.Context, paths chan<- string, root ...string) error {
f := func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
select {
case <-ctx.Done():
log.Error().Err(ctx.Err()).Msg("paths")
return ctx.Err()
case paths <- path:
y.met.IncrCounter([]string{"similar", "path"}, 1)
}
return nil
}
for i := 0; i < len(root); i++ {
if err := afero.Walk(y.afs, root[i], f); err != nil {
return err
}
}
return nil
}
func (y *analyzer) analyze(iconss []iconPath) error {
n := len(iconss)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
b := images4.Similar(iconss[i].icon, iconss[j].icon)
if b {
log.Info().
Str("A", iconss[i].path).
Str("B", iconss[j].path).
Bool("similar", b).
Msg("similar")
}
if err := y.enc.Encode(map[string]any{
"A": iconss[i].path,
"B": iconss[j].path,
"Similar": b}); err != nil {
return err
}
y.met.IncrCounter([]string{"similar", "analyze", strconv.FormatBool(b)}, 1)
}
}
return nil
}
func similar(c *cli.Context) error {
y := &analyzer{
afs: runtime(c).Fs,
met: runtime(c).Metrics,
enc: runtime(c).Encoder,
}
pathsc := make(chan string)
iconsc := make(chan iconPath)
igrp, ictx := errgroup.WithContext(c.Context)
for i := 0; i < c.Int("concurrency"); i++ {
igrp.Go(func() error {
defer func() {
log.Debug().Msg("done creating icons")
}()
return y.icons(ictx, pathsc, iconsc)
})
}
grp, ctx := errgroup.WithContext(ictx)
grp.Go(func() error {
defer close(iconsc)
defer func() {
log.Debug().Msg("closing icon channel")
}()
return igrp.Wait()
})
grp.Go(func() error {
defer close(pathsc)
defer func() {
log.Debug().Msg("closing path channel")
}()
return y.paths(ctx, pathsc, c.Args().Slice()...)
})
icons := make([]iconPath, 0)
grp.Go(func() error {
for icon := range iconsc {
log.Debug().Str("path", icon.path).Msg("gathering")
icons = append(icons, icon)
}
log.Info().Int("num", len(icons)).Msg("images")
return nil
})
if err := grp.Wait(); err != nil {
return err
}
return y.analyze(icons)
}
func CommandSimilar() *cli.Command {
return &cli.Command{
Name: "similar",
HelpName: "similar",
Usage: "Identify similar images",
ArgsUsage: "<file-or-directory> [<file-or-directory>, ...]",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "concurrency",
Aliases: []string{"c"},
Usage: "the number of concurrent image reads",
Value: 4,
},
},
Action: similar,
}
}