-
Notifications
You must be signed in to change notification settings - Fork 8
/
multipartutil.go
199 lines (175 loc) · 5.84 KB
/
multipartutil.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Package multipartutil provides helper functionality for using multipart.Writer.
// Steps are to call NewMultipartBuilder(), write parts, call builder.Close(), and
// retrieve Content-Type header from builder.Writer.FormDataContentType().
package multipartutil
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path/filepath"
"strings"
hum "github.com/grokify/mogo/net/httputilmore"
)
func NewReaderBodyBytes(body []byte, boundary string) *multipart.Reader {
return multipart.NewReader(bytes.NewReader(body), boundary)
}
func NewMultipartReaderForHttpResponse(resp *http.Response) (*multipart.Reader, error) {
contentType := resp.Header.Get(hum.HeaderContentType)
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
} else if !strings.HasPrefix(mediaType, "multipart/") {
return nil, fmt.Errorf("MediaType is not multipart [%v]", mediaType)
}
if boundary, ok := params["boundary"]; !ok {
return nil, fmt.Errorf("MIME Boundary not found in Content-Type header [%v]", contentType)
} else {
return multipart.NewReader(resp.Body, boundary), nil
}
}
// MultipartBuilder is a multipart helper.
type MultipartBuilder struct {
Buffer *bytes.Buffer
Writer *multipart.Writer
}
// NewMultipartBuilder instantiates a new MultipartBuilder.
func NewMultipartBuilder() MultipartBuilder {
builder := MultipartBuilder{}
var b bytes.Buffer
builder.Buffer = &b
builder.Writer = multipart.NewWriter(&b)
return builder
}
// WriteURLValues writes simple header key value strings using `url.Values`
// as an input parameter.
func (builder *MultipartBuilder) WriteURLValues(params url.Values) error {
for key, vals := range params {
for _, val := range vals {
err := builder.WriteFieldString(key, val)
if err != nil {
return err
}
}
}
return nil
}
// WriteFieldString adds a text part.
func (builder *MultipartBuilder) WriteFieldString(partName string, data string) error {
return builder.Writer.WriteField(partName, data)
}
// WriteFieldAsJSON adds a JSON part.
func (builder *MultipartBuilder) WriteFieldAsJSON(partName string, data interface{}, base64Encode bool) error {
jsonBytes, err := json.Marshal(data)
if err != nil {
return err
}
header := textproto.MIMEHeader{}
header.Add(hum.HeaderContentDisposition, fmt.Sprintf(`form-data; name="%s"`, partName))
header.Add(hum.HeaderContentType, hum.ContentTypeAppJsonUtf8)
if base64Encode {
header.Add(hum.HeaderContentTransferEncoding, "base64")
}
partWriter, err := builder.Writer.CreatePart(header)
if err != nil {
return err
}
if base64Encode {
str := base64.StdEncoding.EncodeToString(jsonBytes)
_, err = bytes.NewBuffer([]byte(str)).WriteTo(partWriter)
} else {
_, err = bytes.NewBuffer(jsonBytes).WriteTo(partWriter)
}
return err
}
// WriteFilepathPlus adds a file part given a filename with the Content Type and
// other associated headers as needed. After builder.Close() has been called,
// use like `req, err := http.NewRequest("POST", url, builder.Buffer)`.
// Content-Disposition uses optional attribute as defined here:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
func (builder *MultipartBuilder) WriteFilePathPlus(partName, srcFilepath string, base64Encode bool) error {
partName = strings.TrimSpace(partName)
_, filename := filepath.Split(srcFilepath)
filename = strings.TrimSpace(filename)
mimeType := mime.TypeByExtension(filepath.Ext(srcFilepath))
header := textproto.MIMEHeader{}
cd := []string{"form-data"}
if len(partName) > 0 {
cd = append(cd, fmt.Sprintf(`name="%s"`, partName))
}
if len(filename) > 0 {
cd = append(cd, fmt.Sprintf(`filename="%s"`, filename))
}
header.Add(hum.HeaderContentDisposition, strings.Join(cd, "; "))
if len(mimeType) > 0 {
header.Add(hum.HeaderContentType, mimeType)
}
if base64Encode {
header.Add(hum.HeaderContentTransferEncoding, "base64")
}
partWriter, err := builder.Writer.CreatePart(header)
if err != nil {
return err
}
fileBytes, err := ioutil.ReadFile(srcFilepath)
if err != nil {
return err
}
if base64Encode {
str := base64.StdEncoding.EncodeToString(fileBytes)
_, err = bytes.NewBuffer([]byte(str)).WriteTo(partWriter)
} else {
_, err = bytes.NewBuffer(fileBytes).WriteTo(partWriter)
}
return err
}
// WriteFilePath adds a file part given a filename.
func (builder *MultipartBuilder) WriteFilePath(partName, srcFilepath string) error {
file, err := os.Open(srcFilepath)
if err != nil {
return err
}
defer file.Close()
_, filename := filepath.Split(srcFilepath)
return builder.WriteFileReader(partName, filename, file)
}
// WriteFileHeader adds a file part given a part name and *multipart.FileHeader.
// See more at http://sanatgersappa.blogspot.com/2013/03/handling-multiple-file-uploads-in-go.html
// and https://gist.github.com/sanatgersappa/5127317#file-app-go
func (builder *MultipartBuilder) WriteFileHeader(partName string, fileHeader *multipart.FileHeader) error {
file, err := fileHeader.Open()
if err != nil {
return err
}
defer file.Close()
return builder.WriteFileReader(partName, fileHeader.Filename, file)
}
// WriteFileReader adds a file part given a filename and `io.Reader`.
func (builder *MultipartBuilder) WriteFileReader(partName, filename string, src io.Reader) error {
fw, err := builder.Writer.CreateFormFile(partName, filename)
if err != nil {
return err
}
_, err = io.Copy(fw, src)
return err
}
// Close closes the `multipart.Writer`.
func (builder *MultipartBuilder) Close() error {
return builder.Writer.Close()
}
// ContentType returns the content type for the `Content-Type` header.
func (builder *MultipartBuilder) ContentType() string {
return builder.Writer.FormDataContentType()
}
// String returns the MIME parts as a string.
func (builder *MultipartBuilder) String() string {
return builder.Buffer.String()
}