/
validate.go
116 lines (92 loc) · 2.37 KB
/
validate.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/AwesomeLogos/bimi-explorer/generated"
)
func fetchImgURL(imgurl string) (string, []byte, error) {
client := http.Client{
Timeout: 15 * time.Second,
}
resp, err := client.Get(imgurl)
if err != nil {
return err.Error(), nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", nil, err
}
return resp.Header.Get("Content-Type"), body, nil
}
func validate(imgurl string) (bool, string, error) {
if imgurl == "" {
return false, "imgurl is empty", nil
}
contentType, _, fetchErr := fetchImgURL(imgurl)
if fetchErr != nil {
return false, "Unable to fetch image", fetchErr
}
if !strings.HasPrefix(contentType, "image/") {
return false, "Not an image", nil
}
//LATER: validate image content
return true, "OK", nil
}
func bulkValidate() {
count, countErr := countUnvalidatedDomains()
if countErr != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to count domains: %v\n", countErr)
return
}
domains, domainErr := listUnvalidatedDomains(int32(count), 0)
if domainErr != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to list domains: %v\n", domainErr)
return
}
validateChanneler(domains)
}
func validateWorker(domainChan chan generated.Domain, wg *sync.WaitGroup) {
defer wg.Done()
for domain := range domainChan {
valid, msg, _ := validate(domain.Imgurl.String)
updateValidation(domain.Domain, valid, msg)
}
}
func validateChanneler(domains []generated.Domain) {
domainChan := make(chan generated.Domain, len(domains))
wg := &sync.WaitGroup{}
maxWorkers, maxErr := strconv.Atoi(os.Getenv("MAXWORKERS"))
if maxErr != nil {
maxWorkers = 10 * runtime.NumCPU()
}
total := 0
for _, domain := range domains {
domainChan <- domain
total++
}
fmt.Fprintf(os.Stderr, "INFO: %d total domains (using %d workers)", total, maxWorkers)
for i := 0; i < maxWorkers; i++ {
wg.Add(1)
go validateWorker(domainChan, wg)
}
close(domainChan)
previousLen := len(domainChan)
for len(domainChan) > 0 {
if previousLen-len(domainChan) > 1000 {
fmt.Fprintf(os.Stderr, "\nINFO: Domains remaining: %d..", len(domainChan))
previousLen = len(domainChan)
}
fmt.Fprintf(os.Stderr, ".")
time.Sleep(100 * time.Millisecond)
}
fmt.Fprintf(os.Stderr, "\nINFO: Total domains: %d\n", total)
wg.Wait()
}