forked from winterssy/ghttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart.go
172 lines (149 loc) · 3.66 KB
/
multipart.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 ghttp
import (
"bufio"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"path/filepath"
"strings"
"sync"
)
type (
// FormData is a multipart container that uses io.Pipe to reduce memory used
// while uploading files.
FormData struct {
files Files
form Form
pr *io.PipeReader
pw *io.PipeWriter
mw *multipart.Writer
once sync.Once
}
// Files maps a string key to a *File type value, used for files of multipart payload.
Files map[string]*File
// File is a struct defines a file of a multipart section to upload.
File struct {
body io.ReadCloser
filename string
mime string
}
)
// NewMultipart returns a new multipart container.
func NewMultipart(files Files) *FormData {
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
return &FormData{
mw: mw,
pr: pr,
pw: pw,
files: files,
}
}
// WithForm specifies form for fd.
// If you only want to send form payload, use Request.SetForm or ghttp.WithForm instead.
func (fd *FormData) WithForm(form Form) *FormData {
fd.form = form
return fd
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
func (fd *FormData) writeFiles() {
const (
fileFormat = `form-data; name="%s"; filename="%s"`
unknownFilename = "???"
)
var (
part io.Writer
err error
)
for k, v := range fd.files {
filename := valueOrDefault(v.filename, unknownFilename)
r := bufio.NewReader(v)
mime := v.mime
if mime == "" {
data, _ := r.Peek(512)
mime = http.DetectContentType(data)
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(fileFormat, escapeQuotes(k), escapeQuotes(filename)))
h.Set("Content-Type", mime)
part, _ = fd.mw.CreatePart(h)
_, err = io.Copy(part, r)
if err != nil {
log.Printf("ghttp: can't bind multipart section (%s=@%s): %s", k, filename, err.Error())
}
v.Close()
}
}
func (fd *FormData) writeForm() {
for k, vs := range fd.form.Decode() {
for _, v := range vs {
fd.mw.WriteField(k, v)
}
}
}
// Read implements io.Reader interface.
func (fd *FormData) Read(b []byte) (int, error) {
fd.once.Do(func() {
go func() {
defer fd.pw.Close()
defer fd.mw.Close() // must close the multipart writer first!
fd.writeFiles()
if len(fd.form) > 0 {
fd.writeForm()
}
}()
})
return fd.pr.Read(b)
}
// ContentType returns the Content-Type for an HTTP
// multipart/form-data with this multipart Container's Boundary.
func (fd *FormData) ContentType() string {
return fd.mw.FormDataContentType()
}
// WithFilename specifies f's filename.
func (f *File) WithFilename(filename string) *File {
f.filename = filename
return f
}
// WithMIME specifies f's Content-Type.
// By default ghttp detects automatically using http.DetectContentType.
func (f *File) WithMIME(mime string) *File {
f.mime = mime
return f
}
// Read implements io.Reader interface.
func (f *File) Read(b []byte) (int, error) {
return f.body.Read(b)
}
// Close implements io.Closer interface.
func (f *File) Close() error {
return f.body.Close()
}
// FileFromReader constructors a new File from a reader.
func FileFromReader(body io.Reader) *File {
return &File{body: toReadCloser(body)}
}
// Open opens the named file and returns a File with filename specified.
func Open(filename string) (*File, error) {
body, err := os.Open(filename)
if err != nil {
return nil, err
}
return FileFromReader(body).WithFilename(filepath.Base(filename)), nil
}
// MustOpen is like Open, but if there is an error, it will panic.
func MustOpen(filename string) *File {
file, err := Open(filename)
if err != nil {
panic(err)
}
return file
}