-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.go
250 lines (241 loc) · 6.81 KB
/
files.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package gomodio
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// Modfiles struct which maps to the JSON of Get Modfiles
type Modfiles struct {
Data []File `json:"data"`
ResultCount int `json:"result_count"`
ResultLimit int `json:"result_limit"`
ResultTotal int `json:"result_total"`
ResultOffset int `json:"result_offset"`
}
// File struct which maps to the JSON of Get/Add/Delete File
type File struct {
ID int `json:"id"`
ModID int `json:"mod_id"`
DateAdded int `json:"date_added"`
DateScanned int `json:"date_scanned"`
VirusStatus int `json:"virus_status"`
VirusPositive int `json:"virus_positive"`
VirustotalHash string `json:"virustotal_hash"`
Filesize int `json:"filesize"`
Filehash struct {
Md5 string `json:"md5"`
} `json:"filehash"`
Filename string `json:"filename"`
Version string `json:"version"`
Changelog string `json:"changelog"`
MetadataBlob string `json:"metadata_blob"`
Download struct {
BinaryURL string `json:"binary_url"`
DateExpires int `json:"date_expires"`
} `json:"download"`
}
// GetModfiles grabs modfiles and returns a Modfiles struct
func GetModfiles(modID int, gameID int, options map[string]string, user *User) (f *Modfiles, err error) {
options["api_key"] = user.APIKey()
queryStr := ParseArgsGet(options)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID)+"?"+queryStr, nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return f, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return f, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 201 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return f, err
}
err = json.Unmarshal(body, &f)
if err != nil {
return f, err
}
return f, nil
}
// GetModfile grabs a modfile and returns a File struct
func GetModfile(fileID int, modID int, gameID int, user *User) (f *File, err error) {
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID)+"?api_key="+user.APIKey(), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return f, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return f, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return f, err
}
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
err = json.Unmarshal(body, &f)
if err != nil {
return f, err
}
return f, nil
}
// EditModfile sends a PUT request to edit a mod file
func EditModfile(modID int, gameID int, options map[string]string, user *User) (f *File, err error) {
if user.OAuth2Token() == "" {
return f, errors.New("requires OAuth2 token")
}
client := http.Client{Timeout: time.Duration(5 * time.Second)}
reqBody := ParseArgsBody(options)
req, err := http.NewRequest("PUT", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID)+"/files", strings.NewReader(reqBody.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return f, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return f, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return f, err
}
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(b, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
err = json.Unmarshal(b, &f)
if err != nil {
return f, err
}
return f, nil
}
// DeleteModfile sends a DELETE request to delete a mod file
func DeleteModfile(fileID int, modID int, gameID int, user *User) (err error) {
if user.OAuth2Token() == "" {
return errors.New("requires OAuth2 token")
}
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("DELETE", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID)+"/files/"+strconv.Itoa(fileID), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 204 {
var errObj ErrorCase
e := json.Unmarshal(b, &errObj)
if e != nil {
log.Fatalln(e)
}
return HandleResponseError(errObj)
}
return nil
}
// AddModfile sends a POST request to upload a mod file
func (user *User) AddModfile(modID int, gameID int, fp string, options map[string]string) (f *File, err error) {
if user.OAuth2Token() == "" {
return f, errors.New("requires OAuth2 token")
}
file, err := os.Open(fp)
if err != nil {
return f, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("filedata", filepath.Base(fp))
if err != nil {
return f, err
}
_, err = io.Copy(part, file)
if options != nil {
for k, v := range options {
_ = writer.WriteField(k, v)
}
}
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("POST", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"/mods/"+strconv.Itoa(modID)+"/files", body)
req.Header.Set("Content-Type", "multipart/form-data")
if err != nil {
return f, err
}
if user.OAuth2Token() != "" || len(user.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+user.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return f, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return f, err
}
if resp.StatusCode != 201 {
var errObj ErrorCase
e := json.Unmarshal(b, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
err = json.Unmarshal(b, &f)
if err != nil {
return f, err
}
return f, err
}