-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofile_io.go
201 lines (179 loc) · 4.68 KB
/
gofile_io.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
// gofileioupload contains tools to upload files to gofile.io.
package gofileioupload
/*
This package is not meant as a way to interact with your gofile.io Client.
The vision behind this package is to stay anonymous and still be able to upload
single or multiple files.
*/
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path"
"time"
)
type Region int
const (
_ Region = iota
Europe
NorthAmerica
)
// NewClient creates a new instance of Client.
// client gets the account token and folderID from
// the first file uploaded to gofile.
func NewClient() *Client {
return &Client{token: "", folderID: ""}
}
// AddToken takes an account token / guest token and add it to the Client.
func (c *Client) AddToken(token string) *Client {
c.token = token
return c
}
// AddFolderID takes a FolderID and adds it to the client.
// all the files uploaded using client would be uploaded
// to this folder.
func (c *Client) AddFolderID(id string) *Client {
c.folderID = id
return c
}
func (c *Client) SetRegion(region Region) *Client {
switch region {
case NorthAmerica:
c.region = "na"
case Europe:
c.region = "eu"
}
return c
}
// BestServer returns the best server on gofile to upload file to.
func (c *Client) BestServer() (string, error) {
url := "https://api.gofile.io/servers"
if c.region != "" {
url = fmt.Sprintf("%s?zone=%s", url, c.region)
}
ctx, done := context.WithTimeout(
context.Background(),
5*time.Second) //nolint: gomnd // not reused.
defer done()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("gofileioupload.BestServer: got status: %s", resp.Status)
}
var output goFileResponse[serverData]
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
return "", err
}
if len(output.Data.Servers) == 0 {
return "", errors.New("gofileioupload.BestServer: got no results for best servers")
}
for i := 0; i < len(output.Data.Servers); i++ {
if n := output.Data.Servers[i].Name; n != "" {
return n, nil
}
}
return "", errors.New("gofileioupload.BestServer: got no results for best servers")
}
// UploadFile takes path a file and a server to upload the file to, then it uploads the file to
// that perticular server of gofile.io.
func (c *Client) UploadFile(filePath, server string) (*FileData, error) {
// TODO: split this function, its too big.
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
bufferedFileReader := bufio.NewReader(file)
// this implementation was inspired by this comment:
// https://gist.github.com/mattetti/5914158?permalink_comment_id=3422260#gistcomment-3422260
bodyReader, bodyWriter := io.Pipe()
writer := multipart.NewWriter(bodyWriter)
var (
writeErr error
// errOnce sync.Once
)
setErr := func(err error) {
if err != nil {
if writeErr == nil {
writeErr = err
return
}
writeErr = fmt.Errorf("%w: %w", writeErr, err)
}
}
go func() {
defer func() {
setErr(writer.Close())
setErr(bodyWriter.Close())
}()
part, err := writer.CreateFormFile("file", path.Base(filePath))
if err != nil {
setErr(err)
return
}
_, err = io.Copy(part, bufferedFileReader)
if err != nil {
setErr(err)
return
}
c.otherFormFile(writer)
}()
serUrl := fmt.Sprintf("https://%s.gofile.io/contents/uploadfile", server)
req, err := http.NewRequest("POST", serUrl, bodyReader)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
if writeErr != nil {
return nil, writeErr
}
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("wanted: '200 OK', got status code: %s", resp.Status)
}
var output goFileResponse[FileData]
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
return nil, err
}
c.setClientDetails(&output.Data)
return &output.Data, nil
}
// otherFormFile adds the folderId and account token (if they exist)
// to currently uploading file.
func (c *Client) otherFormFile(multiWriter *multipart.Writer) {
if c.folderID != "" {
multiWriter.WriteField("folderId", c.folderID)
}
if c.token != "" {
multiWriter.WriteField("token", c.token)
}
}
// setClientDetails sets the client details after the first successful
// upload to gofile from a created client.
func (c *Client) setClientDetails(data *FileData) {
if c.folderID == "" {
c.AddFolderID(data.ParentFolder)
}
if c.token == "" {
c.AddToken(data.GuestToken)
}
}