/
multipartwriter.go
129 lines (109 loc) · 2.9 KB
/
multipartwriter.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 xhttpclient
import (
"bytes"
"io"
"mime/multipart"
"net/textproto"
"os"
"path/filepath"
"strings"
"sync"
)
type XMultipartWriter struct {
boundary string
ops []func(*multipart.Writer) error
}
var _xMultipartWriterPool = sync.Pool{
New: func() any {
return &XMultipartWriter{
ops: make([]func(writer *multipart.Writer) error, 0, 2),
}
},
}
func NewMultipartWriter() *XMultipartWriter {
xmw := _xMultipartWriterPool.Get().(*XMultipartWriter)
return xmw.SetBoundary(randomBoundary())
}
func (xmw *XMultipartWriter) Boundary() string {
return xmw.boundary
}
func (xmw *XMultipartWriter) SetBoundary(boundary string) *XMultipartWriter {
if (&multipart.Writer{}).SetBoundary(boundary) == nil {
xmw.boundary = boundary
}
xmw.ops = append(xmw.ops, func(raw *multipart.Writer) error {
return raw.SetBoundary(boundary)
})
return xmw
}
// FormDataContentType returns the Content-Type for an HTTP
// multipart/form-data with this Writer's Boundary.
//
// Copied from mime/multipart/writer.go
func (xmw *XMultipartWriter) FormDataContentType() string {
b := xmw.boundary
// We must quote the boundary if it contains any of the
// tspecials characters defined by RFC 2045, or space.
if strings.ContainsAny(b, `()<>@,;:\"/[]?= `) {
b = `"` + b + `"`
}
return "multipart/form-data; boundary=" + b
}
func (xmw *XMultipartWriter) WriteWithHeader(header textproto.MIMEHeader, r io.Reader) *XMultipartWriter {
xmw.ops = append(xmw.ops, func(raw *multipart.Writer) (err error) {
var pw io.Writer
if pw, err = raw.CreatePart(header); err != nil {
return err
}
_, err = io.Copy(pw, r)
return err
})
return xmw
}
func (xmw *XMultipartWriter) WriteWithFile(fieldname, filename string) *XMultipartWriter {
xmw.ops = append(xmw.ops, func(raw *multipart.Writer) (err error) {
var pw io.Writer
if pw, err = raw.CreateFormFile(fieldname, filepath.Base(filename)); err != nil {
return err
}
var file *os.File
if file, err = os.Open(filename); err != nil {
return err
}
defer file.Close()
_, err = io.Copy(pw, file)
return err
})
return xmw
}
func (xmw *XMultipartWriter) WriteWithField(fieldname string, r io.Reader) *XMultipartWriter {
xmw.ops = append(xmw.ops, func(raw *multipart.Writer) (err error) {
var pw io.Writer
if pw, err = raw.CreateFormField(fieldname); err != nil {
return err
}
_, err = io.Copy(pw, r)
return err
})
return xmw
}
func (xmw *XMultipartWriter) WriteWithFieldValue(fieldname, value string) *XMultipartWriter {
xmw.ops = append(xmw.ops, func(raw *multipart.Writer) (err error) {
return raw.WriteField(fieldname, value)
})
return xmw
}
func (xmw *XMultipartWriter) do(buf *bytes.Buffer) error {
raw := multipart.NewWriter(buf)
for _, op := range xmw.ops {
if err := op(raw); err != nil {
return err
}
}
return raw.Close()
}
func (xmw *XMultipartWriter) free() {
xmw.boundary = ""
xmw.ops = nil
_xMultipartWriterPool.Put(xmw)
}