-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (104 loc) · 2.2 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"sync"
)
// File struct from the response, we will only use URL.
type File struct {
Name string `json:"name"`
Size int `json:"size"`
URL string `json:"url"`
}
// Response struct of the request
type Response struct {
Success bool `json:"success"`
Files []File `json:"files"`
}
func main() {
var wg sync.WaitGroup
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Println("Please provide arguments. Usage: gong [files]")
}
// Upload each files, one per one.
for _, p := range flag.Args() {
wg.Add(1)
go func(file string) {
if exists(file) {
// Upload the file
link, err := upload(file)
if err != nil {
fmt.Println(file+":", "upload failed:", err)
}
fmt.Println(file+":", link)
} else {
fmt.Println("file doesn't exist:", file)
}
wg.Done()
}(p)
}
wg.Wait()
}
// Actually upload the file
func upload(filepath string) (string, error) {
// Open the file, reading only.
file, err := os.Open(filepath)
if err != nil {
return "", err
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Get file info
fi, err := os.Stat(filepath)
if err != nil {
return "", err
}
// Create form
form, err := w.CreateFormFile("files[]", fi.Name())
if err != nil {
return "", err
}
// Write to form
if _, err := io.Copy(form, file); err != nil {
return "", err
}
// Close file and writer.
file.Close()
w.Close()
req, err := http.NewRequest("POST", "https://gang.moe/api/upload", &b)
if err != nil {
return "", err
}
// Set the correct Content-Type
req.Header.Set("Content-Type", w.FormDataContentType())
// Prepare client and call API
client := &http.Client{}
// Unmarshal and return response.
resp, err := client.Do(req)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
var r Response
json.Unmarshal(body, &r)
// Close the body
resp.Body.Close()
if r.Success {
return r.Files[0].URL, nil
}
// Unknown error. Check your network.
return "Upload failed. Check your network.", nil
}
// Check if the file exists
func exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}