forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
204 lines (180 loc) · 5.38 KB
/
upload.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
package material
import (
"bytes"
"io"
"os"
"path/filepath"
"github.com/masiqi/wechat.v2/json"
"github.com/masiqi/wechat.v2/mp/core"
)
const (
MaterialTypeImage = "image"
MaterialTypeVoice = "voice"
MaterialTypeVideo = "video"
MaterialTypeThumb = "thumb"
MaterialTypeNews = "news"
)
// image ===============================================================================================================
// UploadImage 上传多媒体图片
func UploadImage(clt *core.Client, _filepath string) (mediaId, url string, err error) {
file, err := os.Open(_filepath)
if err != nil {
return
}
defer file.Close()
return UploadImageFromReader(clt, filepath.Base(_filepath), file)
}
// UploadImageFromReader 上传多媒体图片
// NOTE: 参数 filename 不是文件路径, 是 multipart/form-data 里面 filename 的值.
func UploadImageFromReader(clt *core.Client, filename string, reader io.Reader) (mediaId, url string, err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=image&access_token="
var fields = []core.MultipartFormField{
{
IsFile: true,
Name: "media",
FileName: filename,
Value: reader,
},
}
var result struct {
core.Error
MediaId string `json:"media_id"`
URL string `json:"url"`
}
if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
mediaId = result.MediaId
url = result.URL
return
}
// thumb ===============================================================================================================
// UploadThumb 上传多媒体缩略图
func UploadThumb(clt *core.Client, _filepath string) (mediaId, url string, err error) {
file, err := os.Open(_filepath)
if err != nil {
return
}
defer file.Close()
return UploadThumbFromReader(clt, filepath.Base(_filepath), file)
}
// UploadThumbFromReader 上传多媒体缩略图
// NOTE: 参数 filename 不是文件路径, 是 multipart/form-data 里面 filename 的值.
func UploadThumbFromReader(clt *core.Client, filename string, reader io.Reader) (mediaId, url string, err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=thumb&access_token="
var fields = []core.MultipartFormField{
{
IsFile: true,
Name: "media",
FileName: filename,
Value: reader,
},
}
var result struct {
core.Error
MediaId string `json:"media_id"`
URL string `json:"url"`
}
if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
mediaId = result.MediaId
url = result.URL
return
}
// voice ===============================================================================================================
// UploadVoice 上传多媒体语音
func UploadVoice(clt *core.Client, _filepath string) (mediaId string, err error) {
file, err := os.Open(_filepath)
if err != nil {
return
}
defer file.Close()
return UploadVoiceFromReader(clt, filepath.Base(_filepath), file)
}
// UploadVoiceFromReader 上传多媒体语音
// NOTE: 参数 filename 不是文件路径, 是 multipart/form-data 里面 filename 的值.
func UploadVoiceFromReader(clt *core.Client, filename string, reader io.Reader) (mediaId string, err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=voice&access_token="
var fields = []core.MultipartFormField{
{
IsFile: true,
Name: "media",
FileName: filename,
Value: reader,
},
}
var result struct {
core.Error
MediaId string `json:"media_id"`
}
if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
mediaId = result.MediaId
return
}
// video ===============================================================================================================
// UploadVideo 上传多媒体视频.
func UploadVideo(clt *core.Client, _filepath string, title, introduction string) (mediaId string, err error) {
file, err := os.Open(_filepath)
if err != nil {
return
}
defer file.Close()
return UploadVideoFromReader(clt, filepath.Base(_filepath), file, title, introduction)
}
// UploadVideoFromReader 上传多媒体缩视频.
// NOTE: 参数 filename 不是文件路径, 是 multipart/form-data 里面 filename 的值.
func UploadVideoFromReader(clt *core.Client, filename string, reader io.Reader, title, introduction string) (mediaId string, err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=video&access_token="
var description = struct {
Title string `json:"title"`
Introduction string `json:"introduction"`
}{
Title: title,
Introduction: introduction,
}
descriptionBytes, err := json.Marshal(&description)
if err != nil {
return
}
var fields = []core.MultipartFormField{
{
IsFile: true,
Name: "media",
FileName: filename,
Value: reader,
},
{
IsFile: false,
Name: "description",
Value: bytes.NewReader(descriptionBytes),
},
}
var result struct {
core.Error
MediaId string `json:"media_id"`
}
if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
mediaId = result.MediaId
return
}