-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload_media.go
71 lines (60 loc) · 1.83 KB
/
upload_media.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
package wecombot
import (
"bytes"
"fmt"
"mime/multipart"
"net/textproto"
)
// FileType 文件类型
type FileType string
const (
// NormalFile 普通文件
NormalFile FileType = "file"
// VoiceFile 语音文件
VoiceFile FileType = "voice"
)
func (bot *Bot) getUploadMediaURL(tpe FileType) string {
return fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=%s&type=%s", bot.key, string(tpe))
}
// UploadMedia 文件上传。详见 https://developer.work.weixin.qq.com/document/path/91770#%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E6%8E%A5%E5%8F%A3
func (bot *Bot) UploadMedia(tpe FileType, f []byte, filename string) (*UploadedMedia, error) {
var reqBody *bytes.Buffer
if bot.threadSafe {
reqBody = bytes.NewBuffer(nil)
} else {
reqBody = bot.reqbuf
}
defer reqBody.Reset()
writer := multipart.NewWriter(reqBody)
h := make(textproto.MIMEHeader, 2)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="media"; filename="%s"; filelength=%d`, filename, len(f)))
h.Set("Content-Type", "application/octet-stream")
part, err := writer.CreatePart(h)
if err != nil {
return nil, err
}
if _, err = part.Write(f); err != nil {
return nil, err
}
writer.Close() // finishes the multipart message and writes the trailing boundary end line to the output.
var resData UploadedMedia
if err = bot.doPost(bot.getUploadMediaURL(tpe), map[string]string{"Content-Type": writer.FormDataContentType()}, reqBody, &resData); err != nil {
return nil, err
}
if err = resData.ToError(); err != nil {
return nil, err
}
return &resData, nil
}
// UploadedMedia 上传媒体文件结果
type UploadedMedia struct {
resData
MediaID string `json:"media_id"`
CreatedAt string `json:"created_at"`
}
func (um *UploadedMedia) ToError() error {
if um.ErrCode == 0 {
return nil
}
return NewResError(um.ErrCode, um.ErrMsg)
}