forked from Dri0m/flashpoint-submission-system
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidatorservice.go
138 lines (113 loc) · 3.21 KB
/
validatorservice.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
package service
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"time"
"github.com/FlashpointProject/flashpoint-submission-system/types"
"github.com/FlashpointProject/flashpoint-submission-system/utils"
"github.com/kofalt/go-memoize"
)
var cache = memoize.NewMemoizer(10*time.Minute, 60*time.Minute)
type curationValidator struct {
validatorServerURL string
}
func NewValidator(validatorServerURL string) *curationValidator {
return &curationValidator{
validatorServerURL: validatorServerURL,
}
}
func (c *curationValidator) ProvideArchiveForRepacking(filePath string) (*types.ValidatorRepackResponse, error) {
filePath, err := filepath.Abs(filePath)
if err != nil {
return nil, err
}
client := http.Client{Timeout: 86400 * time.Second}
resp, err := client.Post(fmt.Sprintf("%s/pack-path?path=%s", c.validatorServerURL, url.QueryEscape(filePath)), "application/json;charset=utf-8", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Check the response
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("provide to remote error: %s", string(bytes))
}
var vr types.ValidatorRepackResponse
err = json.Unmarshal(bytes, &vr)
if err != nil {
return nil, err
}
return &vr, nil
}
func (c *curationValidator) ProvideArchiveForValidation(filePath string) (*types.ValidatorResponse, error) {
filePath, err := filepath.Abs(filePath)
if err != nil {
return nil, err
}
client := http.Client{Timeout: 86400 * time.Second}
resp, err := client.Post(fmt.Sprintf("%s/provide-path?path=%s", c.validatorServerURL, url.QueryEscape(filePath)), "application/json;charset=utf-8", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Check the response
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("provide to remote error: %s", string(bytes))
}
var vr types.ValidatorResponse
err = json.Unmarshal(bytes, &vr)
if err != nil {
return nil, err
}
return &vr, nil
}
func (c *curationValidator) Validate(ctx context.Context, file io.Reader, filename string) (*types.ValidatorResponse, error) {
resp, err := utils.UploadMultipartFile(ctx, c.validatorServerURL+"/upload", file, filename)
if err != nil {
return nil, err
}
var vr types.ValidatorResponse
err = json.Unmarshal(resp, &vr)
if err != nil {
return nil, err
}
return &vr, nil
}
func (c *curationValidator) GetTags(ctx context.Context) ([]types.Tag, error) {
f := func() (interface{}, error) {
return c.getTags(ctx)
}
resp, err, cached := cache.Memoize("GetTags", f)
if err != nil {
utils.LogCtx(ctx).Error(err)
return nil, err
}
utils.LogCtx(ctx).WithField("cached", utils.BoolToString(cached)).Debug("getting tags from validator")
tags := resp.([]types.Tag)
return tags, nil
}
func (c *curationValidator) getTags(ctx context.Context) ([]types.Tag, error) {
resp, err := utils.GetURL(c.validatorServerURL + "/tags")
if err != nil {
return nil, err
}
var tr types.ValidatorTagResponse
err = json.Unmarshal(resp, &tr)
if err != nil {
return nil, err
}
return tr.Tags, nil
}