-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.go
100 lines (91 loc) · 2.33 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
package httpClientKit
import (
"bytes"
"github.com/richelieu-yang/chimera/v2/src/component/web/httpKit"
"github.com/richelieu-yang/chimera/v2/src/file/fileKit"
"github.com/richelieu-yang/chimera/v2/src/urlKit"
"io"
"mime/multipart"
"net/http"
"os"
)
// Upload Deprecated: Use reqKit instead.
func Upload(url string, fileParams map[string]string, options ...Option) (int, []byte, error) {
resp, err := UploadForResponse(url, fileParams, options...)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, data, nil
}
// UploadForResponse Deprecated: Use reqKit instead.
/*
@param fileParams (1)可以为nil或空;
(2)key: 键, value: 要上传文件的路径.
@return !!!: 第一个返回值如果不为nil的话,一般来说需要手动调用 "resp.Body.Close()"
*/
func UploadForResponse(url string, fileParams map[string]string, options ...Option) (*http.Response, error) {
opts := loadOptions(options...)
// url
if err := httpKit.AssertHttpUrl(url); err != nil {
return nil, err
}
url, err := urlKit.PolyfillUrl(url, opts.queryParams)
if err != nil {
return nil, err
}
// payload
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
// 可能会多次关闭 multipart.Writer实例
defer writer.Close()
for k, s := range opts.postParams {
for _, v := range s {
// PS: 此处无需对v进行编码操作
if err := writer.WriteField(k, v); err != nil {
return nil, err
}
}
}
for field, path := range fileParams {
if err := fileKit.AssertExistAndIsFile(path); err != nil {
return nil, err
}
err := func() error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
part, err := writer.CreateFormFile(field, fileKit.GetFileName(path))
if err != nil {
return err
}
_, err = io.Copy(part, file)
if err != nil {
return err
}
return nil
}()
if err != nil {
return nil, err
}
}
if err := writer.Close(); err != nil {
return nil, err
}
// req
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
req.Header.Add("Content-Type", "charset=utf-8")
// client
client := opts.newHttpClient()
return client.Do(req)
}