-
Notifications
You must be signed in to change notification settings - Fork 0
/
smms.go
141 lines (130 loc) · 2.92 KB
/
smms.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
)
// SmmsData is upload result
type SmmsData struct {
Filename string // 上传的文件名
Size int // 文件大小Byte
Width int // 宽度
Height int // 高度
Delete string // 删除地址
URL string // 外链地址
}
// SmmsResponse is the response of sm.ms upload api
type SmmsResponse struct {
Code string // success | error
Data SmmsData // upload result
}
func parseJSON(s string) *SmmsResponse {
var r SmmsResponse
// r := &SmmsResponse{}
err := json.Unmarshal([]byte(s), &r)
if err != nil {
panic(err)
}
return &r
}
func newUploadFileRequest(path string) (*http.Request, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
body := &bytes.Buffer{}
w := multipart.NewWriter(body)
part, err := w.CreateFormFile("smfile", filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, f)
err = w.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "https://sm.ms/api/upload", body)
req.Header.Set("Content-Type", w.FormDataContentType())
return req, err
}
func uploadFile(path string) (result string, err error) {
req, err := newUploadFileRequest(path)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
return "", err
}
t := parseJSON(body.String())
s := fmt.Sprintf("Filename: %s\nSize : %d\nW × H : %d × %d\n"+
"Delete : %s\nURL : %s\n===========",
t.Data.Filename, t.Data.Size, t.Data.Width, t.Data.Height,
t.Data.Delete, t.Data.URL)
return s, nil
}
func hasSuffixs(s string, sfxs []string) bool {
for _, sfx := range sfxs {
if strings.HasSuffix(s, sfx) {
return true
}
}
return false
}
func isPathExist(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
func isDir(path string) bool {
f, err := os.Stat(path)
if err != nil {
return false
}
return f.IsDir()
}
func main() {
var path = os.Args[1]
if !isPathExist(path) {
panic("path doesn't exsit")
}
// allowed suffixs
sfxs := []string{".jpg", ".jpeg", ".png", "gif"}
if isDir(path) {
files, err := ioutil.ReadDir(path)
if err != nil {
panic(err)
}
for _, file := range files {
if n := file.Name(); hasSuffixs(n, sfxs) {
var fullPath string
if strings.HasSuffix(path, "/") {
fullPath = path + file.Name()
} else {
fullPath = path + "/" + file.Name()
}
result, _ := uploadFile(fullPath)
fmt.Println(result)
}
}
} else {
result, _ := uploadFile(path)
fmt.Println(result)
}
}