forked from wangluozhe/requests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Files.go
172 lines (159 loc) · 3.99 KB
/
Files.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package url
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/textproto"
"os"
"strings"
)
// 初始化Files结构体
func NewFiles() *Files {
return &Files{}
}
// Files结构体
type Files struct {
files []map[string][]map[string]string
indexKey []string
}
// Files设置Field参数
func (fs *Files) SetField(name, value string) {
f := map[string][]map[string]string{
name: {{
"type": "field",
"value": value,
}},
}
index := SearchStrings(fs.indexKey, name)
if len(fs.indexKey) == 0 || index == -1 {
fs.files = append(fs.files, f)
fs.indexKey = append(fs.indexKey, name)
} else {
fs.files[index] = f
}
}
// Files设置File参数
func (fs *Files) SetFile(name, fileName, filePath, contentType string) {
if contentType == "" {
contentType = "application/octet-stream"
}
f := map[string][]map[string]string{
name: {{
"type": "file",
"value": fileName,
"path": filePath,
"contentType": contentType,
}},
}
index := SearchStrings(fs.indexKey, name)
if len(fs.indexKey) == 0 || index == -1 {
fs.files = append(fs.files, f)
fs.indexKey = append(fs.indexKey, name)
} else {
fs.files[index] = f
}
}
// 获取Files参数值
func (fs *Files) Get(name string) map[string]string {
if len(fs.files) != 0 {
index := SearchStrings(fs.indexKey, name)
if index != -1 {
return fs.files[index][name][0]
}
}
return nil
}
// Files添加Field参数
func (fs *Files) AddField(name, value string) {
f := map[string]string{
"type": "field",
"value": value,
}
index := SearchStrings(fs.indexKey, name)
if len(fs.indexKey) == 0 || index == -1 {
fs.SetField(name, value)
} else {
fs.files[index][name] = append(fs.files[index][name], f)
}
}
// Files添加File参数
func (fs *Files) AddFile(name, fileName, filePath, contentType string) {
if contentType == "" {
contentType = "application/octet-stream"
}
f := map[string]string{
"type": "file",
"value": fileName,
"path": filePath,
"contentType": contentType,
}
index := SearchStrings(fs.indexKey, name)
if len(fs.indexKey) == 0 || index == -1 {
fs.SetFile(name, fileName, filePath, contentType)
} else {
fs.files[index][name] = append(fs.files[index][name], f)
}
}
// 删除Files参数
func (fs *Files) Del(name string) bool {
index := SearchStrings(fs.indexKey, name)
if len(fs.indexKey) == 0 || index == -1 {
return false
}
fs.files = append(fs.files[:index], fs.files[index+1:]...)
fs.indexKey = append(fs.indexKey[:index], fs.indexKey[index+1:]...)
return true
}
// Files结构体转FormFile
func (fs *Files) Encode() (*bytes.Buffer, string, error) {
var uploadWriter io.Writer
var uploadFile *os.File
var err error
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for index, name := range fs.indexKey {
itemList := fs.files[index][name]
for _, item := range itemList {
if item["type"] == "field" {
writer.WriteField(name, item["value"])
} else {
contentType := item["contentType"]
if contentType == "" {
contentType = "application/octet-stream"
}
h := fs.createFormFileHeader(name, item["value"], contentType)
uploadWriter, err = writer.CreatePart(h)
if err != nil {
return nil, "", err
}
uploadFile, err = os.Open(item["path"])
if err != nil {
return nil, "", err
}
_, err = io.Copy(uploadWriter, uploadFile)
if err != nil {
return nil, "", err
}
err = uploadFile.Close()
if err != nil {
return nil, "", err
}
err = writer.Close()
if err != nil {
return nil, "", err
}
}
}
}
return body, writer.FormDataContentType(), nil
}
// 创建文件Header
func (fs *Files) createFormFileHeader(name, fileName, contentType string) textproto.MIMEHeader {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
strings.NewReplacer("\\", "\\\\", `"`, "\\\"").Replace(name),
strings.NewReplacer("\\", "\\\\", `"`, "\\\"").Replace(fileName)))
h.Set("Content-Type", contentType)
return h
}