-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.go
143 lines (117 loc) · 2.87 KB
/
uploader.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
const apiUrl = "https://api.imgbb.com/1/upload?"
type Uploader struct {
apiKey string
apiUrl string
imagePath string
client *http.Client
errorMsg string
}
// init to initialize the uploader
func (u *Uploader) init(imagePath string) error {
u.apiKey = os.Getenv("IMGBB_API_KEY")
if u.apiKey == "" {
msg := fmt.Sprintf("api key not setted")
return errors.New(msg)
}
if !u.fileExists(imagePath) {
msg := fmt.Sprintf("the file '%s' doesn't exist", imagePath)
return errors.New(msg)
}
u.apiUrl = fmt.Sprintf("%s&key=%s", apiUrl, u.apiKey)
u.imagePath = imagePath
u.client = &http.Client{}
u.errorMsg = ""
return nil
}
// UploadImage just do that
func (u *Uploader) UploadImage() (string, error) {
// pr = pipeReader, pw =pipeWriter
pr, pw := io.Pipe()
// creating the form multipart writer
form := multipart.NewWriter(pw)
go func() {
defer pw.Close()
file, err := os.Open(u.imagePath) // path to image file
if err != nil {
msg := fmt.Sprintf("opening image '%s': %v", u.imagePath, err)
fmt.Println(msg)
u.errorMsg = msg
return
}
w, err := form.CreateFormFile("image", u.imagePath)
if err != nil {
msg := fmt.Sprintf("creating form file '%s': %v", u.imagePath, err)
fmt.Println(msg)
u.errorMsg = msg
return
}
_, err = io.Copy(w, file)
if err != nil {
msg := fmt.Sprintf("copying image '%s': %v", u.imagePath, err)
fmt.Println(msg)
u.errorMsg = msg
return
}
form.Close()
}()
// if error were produced
if u.errorMsg != "" {
fmt.Println(u.errorMsg)
return "", errors.New(u.errorMsg)
}
// creating the request
request, err := http.NewRequest(http.MethodPost, u.apiUrl, pr)
if err != nil {
msg := fmt.Sprintf("creating request': %v", err)
fmt.Println(msg)
return "", errors.New(msg)
}
// setting the header
request.Header.Set("Content-Type", form.FormDataContentType())
// making the request
response, err := u.client.Do(request)
if err != nil {
msg := fmt.Sprintf("making the request: %v", err)
fmt.Println(msg)
return "", errors.New(msg)
}
// generating the output
output, err := u.generateOutput(response)
return output, nil
}
// generateOutput will parse the response into a json output to print in the terminal
func (u *Uploader) generateOutput(response *http.Response) (string, error) {
var output Output
// returning output
bytes, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(bytes, &output)
if err != nil {
return "", err
}
indent, err := json.MarshalIndent(output, "", " ")
if err != nil {
return "", err
}
return string(indent), nil
}
// fileExists check if the file exists
func (u *Uploader) fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}