-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmedia.go
141 lines (128 loc) · 3.59 KB
/
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
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 wework
import (
"fmt"
"net/http"
"os"
"github.com/go-laoji/wecom-go-sdk/v2/internal"
)
type MediaType string
const (
MediaImage MediaType = "image"
MediaVoice MediaType = "voice"
MediaVideo MediaType = "video"
MediaFile MediaType = "file"
)
type Media struct {
Type MediaType `json:"type" validate:"required,oneof=image voice video file"`
AttachmentType int `json:"attachment_type" validate:"required,oneof=1 2"`
FilePath string `json:"file_path" validate:"required"`
FileName string `json:"file_name"`
}
type MediaUploadResponse struct {
internal.BizResponse
Type string `json:"type"`
MediaId string `json:"media_id"`
CreateAt uint64 `json:"create_at"`
}
// MediaUploadAttachment 上传附件资源
// 不同的附件类型用于不同的场景。1:朋友圈;2:商品图册
// https://open.work.weixin.qq.com/api/doc/90001/90143/95178
func (ww *weWork) MediaUploadAttachment(corpId uint, attrs Media) (resp MediaUploadResponse) {
if ok := validate.Struct(attrs); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
if !isExists(attrs.FilePath) {
resp.ErrCode = 500
resp.ErrorMsg = fmt.Sprintf("%s 文件不存在!", attrs.FilePath)
return
}
_, err := ww.getRequest(corpId).SetQueryParam("media_type", string(attrs.Type)).
SetQueryParam("attachment_type", fmt.Sprintf("%v", attrs.AttachmentType)).
SetFile("media", attrs.FilePath).SetResult(&resp).
Post("/cgi-bin/media/upload_attachment")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
func isExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
// MediaUpload 上传临时素材
// https://open.work.weixin.qq.com/api/doc/90001/90143/90389
func (ww *weWork) MediaUpload(corpId uint, fileType MediaType, filePath string) (resp MediaUploadResponse) {
if !isExists(filePath) {
resp.ErrCode = 500
resp.ErrorMsg = "文件路径不存在"
return
}
_, err := ww.getRequest(corpId).SetQueryParam("type", string(fileType)).
SetFile("media", filePath).SetResult(&resp).
Post("/cgi-bin/media/upload")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type MediaUploadImgResponse struct {
internal.BizResponse
Url string `json:"url"`
}
// MediaUploadImg 上传图片
// https://open.work.weixin.qq.com/api/doc/90001/90143/90392
func (ww *weWork) MediaUploadImg(corpId uint, filePath string) (resp MediaUploadImgResponse) {
if !isExists(filePath) {
resp.ErrCode = 500
resp.ErrorMsg = "文件路径不存在"
return
}
_, err := ww.getRequest(corpId).
SetFile("media", filePath).SetResult(&resp).
Post("/cgi-bin/media/uploadimg")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type MediaGetResponse struct {
internal.BizResponse
File []byte
Headers http.Header
}
// MediaGet 获取临时素材, TODO: 支持断点下载(分块下载)
// https://developer.work.weixin.qq.com/document/path/90254
func (ww *weWork) MediaGet(corpId uint, mediaId string) (resp MediaGetResponse) {
if mediaId == "" {
resp.ErrCode = 404
resp.ErrorMsg = "资源不存在"
return
}
httpResp, err := ww.getRequest(corpId).
SetQueryParam("media_id", mediaId).
Get("/cgi-bin/media/get")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
// TODO: 处理 response 返回的错误信息; 即 response 包含 errcode 和 errmsg.
resp.File = httpResp.Body()
resp.Headers = httpResp.Header()
}
return
}