forked from chanxuehong/wechat
/
client_upload.go
142 lines (122 loc) · 3.51 KB
/
client_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
// @description wechat 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://github.com/chanxuehong/wechat for the canonical source repository
// @license https://github.com/chanxuehong/wechat/blob/master/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
// +build !wechatdebug
package corp
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"reflect"
)
type MultipartFormField struct {
ContentType int // 0:文件field, 1:普通的文本field
FieldName string
FileName string // ContentType == 0 时有效
Value io.Reader
}
// 通用上传接口.
//
// --BOUNDARY
// Content-Disposition: form-data; name="FIELDNAME"; filename="FILENAME"
// Content-Type: application/octet-stream
//
// FILE-CONTENT
// --BOUNDARY
// Content-Disposition: form-data; name="FIELDNAME"
//
// JSON-DESCRIPTION
// --BOUNDARY--
//
//
// NOTE:
// 1. 一般不需要调用这个方法, 请直接调用高层次的封装方法;
// 2. 最终的 URL == incompleteURL + access_token;
// 3. response 格式有要求, 要么是 *Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field):
// struct {
// Error
// ...
// }
func (clt *Client) PostMultipartForm(incompleteURL string, fields []MultipartFormField, response interface{}) (err error) {
bodyBuf := mediaBufferPool.Get().(*bytes.Buffer)
bodyBuf.Reset()
defer mediaBufferPool.Put(bodyBuf)
multipartWriter := multipart.NewWriter(bodyBuf)
for _, field := range fields {
switch field.ContentType {
case 0: // 文件
partWriter, err := multipartWriter.CreateFormFile(field.FieldName, field.FileName)
if err != nil {
return err
}
if _, err = io.Copy(partWriter, field.Value); err != nil {
return err
}
case 1: // 文本
partWriter, err := multipartWriter.CreateFormField(field.FieldName)
if err != nil {
return err
}
if _, err = io.Copy(partWriter, field.Value); err != nil {
return err
}
}
}
if err = multipartWriter.Close(); err != nil {
return
}
bodyBytes := bodyBuf.Bytes()
token, err := clt.Token()
if err != nil {
return
}
hasRetried := false
RETRY:
finalURL := incompleteURL + url.QueryEscape(token)
httpResp, err := clt.HttpClient.Post(finalURL, multipartWriter.FormDataContentType(), bytes.NewReader(bodyBytes))
if err != nil {
return
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
err = fmt.Errorf("http.Status: %s", httpResp.Status)
return
}
if err = json.NewDecoder(httpResp.Body).Decode(response); err != nil {
return
}
var ErrorStructValue reflect.Value // Error
// 下面的代码对 response 有特定要求, 见此函数 NOTE
responseStructValue := reflect.ValueOf(response).Elem()
if v := responseStructValue.Field(0); v.Kind() == reflect.Struct {
ErrorStructValue = v
} else {
ErrorStructValue = responseStructValue
}
switch ErrCode := ErrorStructValue.Field(0).Int(); ErrCode {
case ErrCodeOK:
return
case ErrCodeAccessTokenExpired:
ErrMsg := ErrorStructValue.Field(1).String()
LogInfoln("[WECHAT_RETRY] err_code:", ErrCode, ", err_msg:", ErrMsg)
LogInfoln("[WECHAT_RETRY] current token:", token)
if !hasRetried {
hasRetried = true
if token, err = clt.TokenRefresh(); err != nil {
return
}
LogInfoln("[WECHAT_RETRY] new token:", token)
responseStructValue.Set(reflect.New(responseStructValue.Type()).Elem())
goto RETRY
}
LogInfoln("[WECHAT_RETRY] fallthrough, current token:", token)
fallthrough
default:
return
}
}