Repository navigation
Expand file tree
/
Copy pathoshi.go
More file actions
308 lines (247 loc) · 6.06 KB
/
Copy pathoshi.go
File metadata and controls
308 lines (247 loc) · 6.06 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package oshi
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)
const (
defaultEndpoint = "https://oshi.at"
expire = "expire"
autodestroy = "autodestroy"
randomizefn = "randomizefn"
filename = "filename"
shorturl = "shorturl"
onion = "onion"
hashsum = "hashsum"
hashsumMatches = 3
)
var uploadResponseRegex = regexp.MustCompile(`(https?://[^\s]+)\s+\[([^\]]+)\]`)
var hashsumResponseRegex = regexp.MustCompile(`([0-9a-zA-Z]+)\s+\(([^\]]+)\)`)
var ErrWrongResponse = errors.New("wrong response")
type Error struct {
StatusCode int
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("Status %d: %s", e.StatusCode, e.Message)
}
type Image struct {
file io.Reader
filename string
expire uint64
autodestroy bool
randomizefn bool
shorturl bool
}
func NewImage(
file []byte,
filename string,
expire uint64,
autodestroy bool,
randomizefn bool,
shorturl bool,
) *Image {
return &Image{
file: bytes.NewReader(file),
filename: filename,
expire: expire,
autodestroy: autodestroy,
randomizefn: randomizefn,
shorturl: shorturl,
}
}
type UploadResponse struct {
Admin string
Download string
TorDownload string
}
type GetHashsumResponse struct {
Algorithm string
Hashsum string
}
// Option helps to confgurate oshi client.
type Option func(client *Client)
// WithEndpoint sets new custom endpoint to oshi client.
func WithEndpoint(endpoint string) Option {
return func(client *Client) {
client.endpoint = endpoint
}
}
// Client is an oshi.at api client.
type Client struct {
endpoint string
httpClient *http.Client
}
// NewClient create a new oshi.at api client.
func NewClient(httpClient *http.Client, opts ...Option) *Client {
client := &Client{
endpoint: defaultEndpoint,
httpClient: httpClient,
}
for _, o := range opts {
o(client)
}
return client
}
// GetTorEndpoint gets tor endpoint.
func (c *Client) GetTorEndpoint(ctx context.Context) (string, error) {
u, err := url.JoinPath(c.endpoint, onion)
if err != nil {
return "", fmt.Errorf("%w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return "", fmt.Errorf("%w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("%w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("%w", err)
}
if resp.StatusCode != http.StatusOK {
return "", &Error{
StatusCode: resp.StatusCode,
Message: string(body),
}
}
return string(body), nil
}
// GetHashsum gets file hashsum and algorithm.
func (c *Client) GetHashsum(ctx context.Context, file string) (GetHashsumResponse, error) {
u, err := url.JoinPath(c.endpoint, hashsum, file)
if err != nil {
return GetHashsumResponse{}, fmt.Errorf("%w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return GetHashsumResponse{}, fmt.Errorf("%w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return GetHashsumResponse{}, fmt.Errorf("%w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return GetHashsumResponse{}, fmt.Errorf("%w", err)
}
if resp.StatusCode != http.StatusOK {
return GetHashsumResponse{}, &Error{
StatusCode: resp.StatusCode,
Message: string(body),
}
}
return c.parseHashsumResponse(body)
}
func (c *Client) parseHashsumResponse(body []byte) (GetHashsumResponse, error) {
matches := hashsumResponseRegex.FindStringSubmatch(string(body))
if len(matches) < hashsumMatches {
return GetHashsumResponse{}, fmt.Errorf("%w: %s", ErrWrongResponse, string(body))
}
return GetHashsumResponse{
Algorithm: matches[2],
Hashsum: matches[1],
}, nil
}
// Delete delete file.
func (c *Client) Delete(ctx context.Context, adminURL string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, adminURL, nil)
if err != nil {
return fmt.Errorf("%w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("%w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("%w", err)
}
return &Error{
StatusCode: resp.StatusCode,
Message: string(body),
}
}
return nil
}
// Upload uploads file.
func (c *Client) Upload(ctx context.Context, img *Image) (UploadResponse, error) {
url, err := c.prepareUploadURL(img)
if err != nil {
return UploadResponse{}, fmt.Errorf("%w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, img.file)
if err != nil {
return UploadResponse{}, fmt.Errorf("%w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return UploadResponse{}, fmt.Errorf("%w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return UploadResponse{}, fmt.Errorf("%w", err)
}
if resp.StatusCode != http.StatusOK {
return UploadResponse{}, &Error{
StatusCode: resp.StatusCode,
Message: string(body),
}
}
return c.parseUploadResponse(body), nil
}
func (c *Client) prepareUploadURL(img *Image) (string, error) {
url, err := url.Parse(c.endpoint)
if err != nil {
return "", fmt.Errorf("%w", err)
}
query := url.Query()
if img.filename != filename {
query.Set(filename, img.filename)
}
if img.expire > 0 {
query.Set(expire, strconv.FormatUint(img.expire, 10))
}
if img.autodestroy {
query.Set(autodestroy, "1")
}
if img.randomizefn {
query.Set(randomizefn, "1")
}
if img.shorturl {
query.Set(shorturl, "1")
}
url.RawQuery = query.Encode()
return url.String(), nil
}
func (c *Client) parseUploadResponse(body []byte) UploadResponse {
matches := uploadResponseRegex.FindAllStringSubmatch(string(body), -1)
response := UploadResponse{}
for _, match := range matches {
url := match[1]
label := strings.ToLower(match[2])
switch label {
case "admin":
response.Admin = url
case "download":
response.Download = url
case "tor download":
response.TorDownload = url
}
}
return response
}