-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
212 lines (176 loc) · 4.53 KB
/
core.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package imhashdb
import (
"encoding/json"
"github.com/go-redis/redis/v7"
"github.com/jackc/pgx"
"github.com/pkg/errors"
"github.com/simon987/fastimagehash-go"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"strings"
)
const UserAgent = "imhashdb/v1.0"
var ImageSuffixes = []string{
".jpeg", ".jpg", ".png",
".jpeg:orig", ".jpg:orig", ".png:orig",
".bmp", ".webp",
}
type Config struct {
PgUser string
PgPassword string
PgDb string
PgHost string
PgPort int
RedisAddr string
RedisPassword string
RedisDb int
ApiAddr string
HasherConcurrency int
QueryConcurrency int
Store string
ImgurClientId string
HasherPattern string
}
var ImageBlackList = []string{}
var Rdb *redis.Client
var Pgdb *pgx.ConnPool
var Logger *zap.Logger
var Conf Config
func Init() {
Logger, _ = zap.NewDevelopment()
Rdb = redis.NewClient(&redis.Options{
Addr: Conf.RedisAddr,
Password: Conf.RedisPassword,
DB: Conf.RedisDb,
})
_, err := Rdb.Ping().Result()
if err != nil {
Logger.Fatal("Could not connect to redis server")
}
Pgdb = DbConnect(
Conf.PgHost,
Conf.PgPort,
Conf.PgUser,
Conf.PgPassword,
Conf.PgDb,
)
DbInit(Pgdb)
}
func ComputeHash(data []byte) (*Hashes, error) {
h := &Hashes{}
var code fastimagehash.Code
h.DHash8, code = fastimagehash.DHashMem(data, 8)
if code != fastimagehash.Ok {
return nil, errors.Errorf("dHash error: %d", int(code))
}
h.DHash16, code = fastimagehash.DHashMem(data, 16)
if code != fastimagehash.Ok {
return nil, errors.Errorf("dHash error: %d", int(code))
}
h.DHash32, code = fastimagehash.DHashMem(data, 32)
if code != fastimagehash.Ok {
return nil, errors.Errorf("dHash error: %d", int(code))
}
h.MHash8, code = fastimagehash.MHashMem(data, 8)
if code != fastimagehash.Ok {
return nil, errors.Errorf("mHash error: %d", int(code))
}
h.MHash16, code = fastimagehash.MHashMem(data, 16)
if code != fastimagehash.Ok {
return nil, errors.Errorf("mHash error: %d", int(code))
}
h.MHash32, code = fastimagehash.MHashMem(data, 32)
if code != fastimagehash.Ok {
return nil, errors.Errorf("mHash error: %d", int(code))
}
h.PHash8, code = fastimagehash.PHashMem(data, 8, 4)
if code != fastimagehash.Ok {
return nil, errors.Errorf("pHash error: %d", int(code))
}
h.PHash16, code = fastimagehash.PHashMem(data, 16, 4)
if code != fastimagehash.Ok {
return nil, errors.Errorf("pHash error: %d", int(code))
}
h.PHash32, code = fastimagehash.PHashMem(data, 32, 4)
if code != fastimagehash.Ok {
return nil, errors.Errorf("pHash error: %d", int(code))
}
h.WHash8, code = fastimagehash.WHashMem(data, 8, 0, true, fastimagehash.Haar)
if code != fastimagehash.Ok {
return nil, errors.Errorf("wHash error: %d", int(code))
}
h.WHash16, code = fastimagehash.WHashMem(data, 16, 0, true, fastimagehash.Haar)
if code != fastimagehash.Ok {
return nil, errors.Errorf("wHash error: %d", int(code))
}
h.WHash32, code = fastimagehash.WHashMem(data, 32, 0, true, fastimagehash.Haar)
if code != fastimagehash.Ok {
return nil, errors.Errorf("wHash error: %d", int(code))
}
return h, nil
}
func TransformLink(link string, meta *[]Meta) []string {
for _, str := range ImageBlackList {
if strings.Contains(link, str) {
return nil
}
}
links := handleImgurLink(link, meta)
if links != nil {
return links
}
return []string{link}
}
func isHttpOk(code int) bool {
return code >= 200 && code < 300
}
func FetchJson(link string, v interface{}, raw *[]byte, headers ...[]string) error {
body, err := Fetch(link, headers...)
if err != nil {
return err
}
err = json.Unmarshal(body, v)
if err != nil {
return err
}
*raw = body
return nil
}
func Fetch(link string, headers ...[]string) ([]byte, error) {
client := &fasthttp.Client{}
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(link)
req.Header.Add("User-Agent", UserAgent)
for _, h := range headers {
req.Header.Add(h[0], h[1])
}
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := client.Do(req, resp)
if err != nil {
Logger.Warn(
"Error during HTTP request",
zap.String("link", link),
zap.String("err", err.Error()),
)
return nil, err
}
code := resp.StatusCode()
if !isHttpOk(code) {
Logger.Debug(
"Got HTTP error code",
zap.String("link", link),
zap.Int("code", code),
)
return nil, errors.Errorf("HTTP %d", code)
}
body := make([]byte, len(resp.Body()))
copy(body, resp.Body())
Logger.Debug(
"HTTP Get",
zap.String("link", link),
zap.Int("size", len(body)),
)
return body, nil
}