-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
253 lines (214 loc) · 5.77 KB
/
main.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
251
252
253
package main
// Important neeeded modules
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/mrdemonwolf/share-cli/pkg/config"
)
// ResponseFile ...
type ResponseFile struct {
Name string `json:"name"`
Ext string `json:"ext"`
Size string `json:"size"`
URL string `json:"url"`
Delete string `json:"delete"`
DeleteKey string `json:"deleteKey"`
}
// ResponseLink ...
type ResponseLink struct {
URL string `json:"url"`
Code string `json:"code"`
Limit string `json:"limit"`
NewURL string `json:"newurl"`
Delete string `json:"delete"`
DeleteKey string `json:"deleteKey"`
}
// ResponseErrors ...
type ResponseErrors struct {
Code string `json:"code"`
URL string `json:"url"`
Tags string `json:"tags"`
}
// Response Json ...
type Response struct {
Auth bool `json:"auth"`
Success bool `json:"success"`
Error string `json:"error"`
Errors ResponseErrors `json:"errors"`
File ResponseFile `json:"file"`
Link ResponseLink `json:"link"`
Status int `json:"status"`
}
// LinkFormData ...
type LinkFormData struct {
URL string
Code string
Limit string
}
// Creates a new file upload http request with optional extra params
func newfileUploadRequest(uri string, headers map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
err = writer.Close()
if err != nil {
return nil, err
}
// Does the request with the headers
req, err := http.NewRequest("POST", uri+"/api/v2/upload", body)
for key, val := range headers {
req.Header.Set(key, val)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("User-Agent", "Share-CLI/1.0")
return req, err
}
func newLinkRequest(uri string, headers map[string]string, formData *LinkFormData) (*http.Request, error) {
data := url.Values{}
if formData.URL != "" {
data.Set("url", formData.URL)
}
if formData.Code != "" {
data.Set("code", formData.Code)
}
if formData.Limit != "" {
data.Set("limit", formData.Limit)
}
// Does the request with the headers
req, err := http.NewRequest("POST", uri+"/api/v2/link", bytes.NewBuffer([]byte(data.Encode())))
for key, val := range headers {
req.Header.Set(key, val)
}
req.Header.Set("User-Agent", "Share-CLI/1.0")
return req, err
}
func main() {
cfg, err := config.GetConfig()
if err != nil {
log.Println(err)
return
}
// Setup the args for functions and there flags
// This is for uploading a file
fileUploadCommand := flag.NewFlagSet("upload", flag.ExitOnError)
filePtr := fileUploadCommand.String("f", "", "File to upload to your share server. (Required)")
pathPtr := fileUploadCommand.Bool("p", true, "If this is false it will not add the path to the file.")
// This is for shortening alink
linkShotenCommand := flag.NewFlagSet("link", flag.ExitOnError)
urlPtr := linkShotenCommand.String("u", "", "File to upload to your share server. (Required)")
limitPtr := linkShotenCommand.String("l", "", "Limit on link clicks.")
codePtr := linkShotenCommand.String("c", "", "Custom short link code.")
if len(os.Args) < 2 {
fmt.Println("upload or link subcommand is required")
return
}
switch os.Args[1] {
case "upload":
fileUploadCommand.Parse(os.Args[2:])
case "link":
linkShotenCommand.Parse(os.Args[2:])
default:
flag.PrintDefaults()
return
}
if fileUploadCommand.Parsed() {
if *filePtr == "" {
fileUploadCommand.PrintDefaults()
return
}
path, err := os.Getwd()
if err != nil {
log.Println(err)
return
}
var file = *filePtr
if *pathPtr == true {
file = path + "/" + *filePtr
}
headers := map[string]string{
"Authorization": "Bearer" + " " + cfg.Creds.APIKEY,
}
request, err := newfileUploadRequest(cfg.Server.URL, headers, "file", file)
if err != nil {
log.Println(err)
return
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Println(err)
} else {
defer resp.Body.Close()
data := new(Response)
json.NewDecoder(resp.Body).Decode(data)
if data.Status == 200 {
fmt.Print("Your file has been uploaded")
fmt.Printf("URL: %s \n", data.File.URL)
fmt.Printf("Delete URL: %s \n", data.File.Delete)
} else {
fmt.Printf("Error: %s \n", data.Error)
}
}
}
if linkShotenCommand.Parsed() {
if *urlPtr == "" {
linkShotenCommand.PrintDefaults()
return
}
headers := map[string]string{
"Authorization": "Bearer" + " " + cfg.Creds.APIKEY,
"Content-Type": "application/x-www-form-urlencoded",
}
data := &LinkFormData{
URL: *urlPtr,
Limit: *limitPtr,
Code: *codePtr,
}
request, err := newLinkRequest(cfg.Server.URL, headers, data)
if err != nil {
log.Println(err)
return
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Println(err)
} else {
defer resp.Body.Close()
data := new(Response)
json.NewDecoder(resp.Body).Decode(data)
if data.Status == 200 {
// fmt.Print("Your shorted link has been created")
// fmt.Printf("URL: %s \n", data.File.URL)
// fmt.Printf("Delete URL: %s \n", data.File.Delete)
// }
fmt.Print("Your shorted link has been created \n")
fmt.Printf("New URL: %s \n", data.Link.NewURL)
if data.Link.Code != "" {
fmt.Printf("Click Limit: %s \n", data.Link.Limit)
}
fmt.Printf("Delete URL: %s \n", data.Link.Delete)
} else {
fmt.Printf("Error: %s %s %s \n", data.Errors.Code, data.Errors.URL, data.Errors.Tags)
}
}
}
}