-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourceData.go
65 lines (50 loc) · 1.34 KB
/
sourceData.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
package main
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"net/http"
"github.com/AwesomeLogos/bimi-explorer/logosearch"
)
func buildSourceData() ([]byte, error) {
count, _ := countDomains()
domains, dbErr := listDomains(int32(count), 0)
if dbErr != nil {
return nil, dbErr
}
searchIndex := logosearch.GenerateIndex(domains)
jsonData, jsonErr := json.MarshalIndent(searchIndex, "", " ")
if jsonErr != nil {
return nil, jsonErr
}
return jsonData, nil
}
func sourceDataJson(w http.ResponseWriter, r *http.Request) {
sourceData, sourceErr := buildSourceData()
if sourceErr != nil {
http.Error(w, "Unable to generate sourceData", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(sourceData)
}
func sourceDataTgz(w http.ResponseWriter, r *http.Request) {
sourceData, sourceErr := buildSourceData()
if sourceErr != nil {
http.Error(w, "Unable to generate sourceData", http.StatusInternalServerError)
return
}
hdr := &tar.Header{
Name: "sourceData.json",
Mode: 0600,
Size: int64(len(sourceData)),
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename=sourceData.tgz")
gw := gzip.NewWriter(w)
tw := tar.NewWriter(gw)
tw.WriteHeader(hdr)
tw.Write(sourceData)
tw.Close()
gw.Close()
}