forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_upload.go
129 lines (117 loc) · 3.29 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
package core
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"github.com/bububa/wechat/internal/debug/api"
"github.com/bububa/wechat/internal/debug/api/retry"
"github.com/bububa/wechat/util"
)
type MultipartFormField struct {
IsFile bool
Name string
FileName string
Value io.Reader
}
// PostMultipartForm 通用上传接口.
//
// --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) {
ErrorStructValue, ErrorErrCodeValue := checkResponse(response)
buffer := mediaBufferPool.Get().(*bytes.Buffer)
buffer.Reset()
defer mediaBufferPool.Put(buffer)
multipartWriter := multipart.NewWriter(buffer)
for i := 0; i < len(fields); i++ {
if field := &fields[i]; field.IsFile {
partWriter, err3 := multipartWriter.CreateFormFile(field.Name, field.FileName)
if err3 != nil {
return err3
}
if _, err3 = io.Copy(partWriter, field.Value); err3 != nil {
return err3
}
} else {
partWriter, err3 := multipartWriter.CreateFormField(field.Name)
if err3 != nil {
return err3
}
if _, err3 = io.Copy(partWriter, field.Value); err3 != nil {
return err3
}
}
}
if err = multipartWriter.Close(); err != nil {
return
}
requestBodyBytes := buffer.Bytes()
requestBodyType := multipartWriter.FormDataContentType()
httpClient := clt.HttpClient
if httpClient == nil {
httpClient = util.DefaultMediaHttpClient
}
token, err := clt.Token()
if err != nil {
return
}
hasRetried := false
RETRY:
finalURL := incompleteURL + url.QueryEscape(token)
if err = httpPostMultipartForm(httpClient, finalURL, requestBodyType, requestBodyBytes, response, clt.Debug()); err != nil {
return
}
switch errCode := ErrorErrCodeValue.Int(); errCode {
case ErrCodeOK:
return
case ErrCodeInvalidCredential, ErrCodeAccessTokenExpired:
errMsg := ErrorStructValue.Field(errorErrMsgIndex).String()
retry.DebugPrintError(errCode, errMsg, token, clt.Debug())
if !hasRetried {
hasRetried = true
ErrorStructValue.Set(errorZeroValue)
if token, err = clt.RefreshToken(token); err != nil {
return
}
retry.DebugPrintNewToken(token, clt.Debug())
goto RETRY
}
retry.DebugPrintFallthrough(token, clt.Debug())
fallthrough
default:
return
}
}
func httpPostMultipartForm(clt *http.Client, url, bodyType string, body []byte, response interface{}, debug bool) error {
api.DebugPrintPostMultipartRequest(url, body, debug)
httpResp, err := clt.Post(url, bodyType, bytes.NewReader(body))
if err != nil {
return err
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
return fmt.Errorf("http.Status: %s", httpResp.Status)
}
return api.DecodeJSONHttpResponse(httpResp.Body, response, debug)
}