-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgy.go
More file actions
101 lines (89 loc) · 2.57 KB
/
vgy.go
File metadata and controls
101 lines (89 loc) · 2.57 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
package vgy
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
// Response returned information from vgy.me after posting an image
type Response struct {
Error bool
URL string // direct URL of the image
Image string
Extension string
Filesize int64 // File size in bytes
DeleteURL string `json:"delete"` // URL to delete the image from vgy.me hosting
UploadList []string `json:"upload_list"` // List of URL when upload several images
}
// Print returns string formated Response
func (r Response) Print() string {
result := "URL: " + r.URL + "\n"
result += "Delete URL: " + r.DeleteURL + "\n"
result += "Image: " + r.Image + "." + r.Extension + "\n"
result += fmt.Sprintf("Filesize: %d", r.Filesize)
return result
}
// UserKey must be created at https://vgy.me/account/details
// It's not mandatory. But if you don't set it you won't get the uploaded image attached to your vgy.me account
var UserKey string
// urlUpload based vgy.me API url
const urlUpload = "https://vgy.me/upload"
// getRequest returns http request
func getRequest(fileName string) (req *http.Request, err error) {
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Add your image file
f, err := os.Open(fileName)
if err != nil {
return
}
defer f.Close()
fw, _ := w.CreateFormFile("file", fileName)
if _, err = io.Copy(fw, f); err != nil {
return
}
// Add User Key if provided
if UserKey != "" {
w.CreateFormField("userkey")
fw.Write([]byte(UserKey))
}
// Don't forget to close the multipart writer.
// If you don't close it, your request will be missing the terminating boundary.
w.Close()
// Now that you have a form, you can submit it to your handler.
req, _ = http.NewRequest("POST", urlUpload, &b)
// Don't forget to set the content type, this will contain the boundary.
req.Header.Set("Content-Type", w.FormDataContentType())
return
}
// UploadImageFile uploads an image (.png, .jpg) to vgy.me
func UploadImageFile(fileName string) (response Response, err error) {
req, err := getRequest(fileName)
if err != nil {
return
}
// Submit the request
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return
}
// Check the response
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("bad status: %s", res.Status)
return
}
defer res.Body.Close()
if respBody, err := ioutil.ReadAll(res.Body); err == nil {
if err = json.Unmarshal(respBody, &response); err == nil && response.Error {
err = errors.New("error")
}
}
return
}