-
-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathmultipart.go
More file actions
192 lines (159 loc) · 4.65 KB
/
Copy pathmultipart.go
File metadata and controls
192 lines (159 loc) · 4.65 KB
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
// Copyright (c) 2015-present Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
package resty
import (
"fmt"
"io"
"net/http"
"net/textproto"
"os"
"path/filepath"
"strings"
)
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
// MultipartField describes a multipart/form-data field and its optional file
// upload metadata.
type MultipartField struct {
// Name is the multipart field name expected by the server.
Name string
// FileName is the filename sent to the server.
FileName string
// ContentType is the multipart file content type. It is recommended to set
// this explicitly when known to avoid auto-detection.
ContentType string
// Reader is the [io.Reader] source for multipart upload. It is optional if
// [MultipartField.FilePath] is set.
Reader io.Reader
// FilePath is the file path used for multipart upload. It is optional if
// [MultipartField.Reader] is set.
FilePath string
// FileSize is the file size in bytes, reported via
// [MultipartFieldCallbackFunc].
FileSize int64
// ProgressCallback receives live upload progress details for this field.
//
// NOTE: When using [MultipartField.Reader] with this callback, set
// [MultipartField.FileSize] if known so [MultipartFieldProgress] includes a
// meaningful total size.
ProgressCallback MultipartFieldCallbackFunc
// Values is used to provide ordered multipart form-data values for a field.
//
// It is primarily intended for ordered form field use cases.
Values []string
// tempBuf is used to preserve the byte(s) read from the file to detect the content type.
// Or any possible read error early.
tempBuf []byte
}
// Clone returns a copy of m, except [MultipartField.Reader] which is shared.
func (mf *MultipartField) Clone() *MultipartField {
mf2 := new(MultipartField)
*mf2 = *mf
return mf2
}
func (mf *MultipartField) resetReader() error {
if rs, ok := mf.Reader.(io.ReadSeeker); ok {
_, err := rs.Seek(0, io.SeekStart)
return err
}
return ErrReaderNotSeekable
}
func (mf *MultipartField) isValues() bool {
return len(mf.Values) > 0
}
func (mf *MultipartField) close() {
closeq(mf.Reader)
}
func (mf *MultipartField) createHeader() textproto.MIMEHeader {
h := make(textproto.MIMEHeader)
if isStringEmpty(mf.FileName) {
h.Set(hdrContentDisposition,
fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(mf.Name)))
} else {
h.Set(hdrContentDisposition,
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(mf.Name), escapeQuotes(mf.FileName)))
}
if !isStringEmpty(mf.ContentType) {
h.Set(hdrContentTypeKey, mf.ContentType)
}
return h
}
func (mf *MultipartField) openFile() error {
if isStringEmpty(mf.FilePath) || mf.Reader != nil {
return nil
}
file, err := os.Open(mf.FilePath)
if err != nil {
return err
}
if isStringEmpty(mf.FileName) {
mf.FileName = filepath.Base(mf.FilePath)
}
// if file open is success, stat will succeed
fileStat, _ := file.Stat()
mf.Reader = file
mf.FileSize = fileStat.Size()
return nil
}
func (mf *MultipartField) detectContentType() error {
if !isStringEmpty(mf.ContentType) || mf.Reader == nil {
return nil
}
p := make([]byte, 512)
size, err := mf.Reader.Read(p)
if err != nil && err != io.EOF {
return err
}
mf.tempBuf = p[:size]
mf.ContentType = http.DetectContentType(mf.tempBuf)
return nil
}
func (mf *MultipartField) wrapProgressCallbackIfPresent(pw io.Writer) io.Writer {
if mf.ProgressCallback == nil {
return pw
}
return &multipartProgressWriter{
w: pw,
f: func(pb int64) {
mf.ProgressCallback(MultipartFieldProgress{
Name: mf.Name,
FileName: mf.FileName,
FileSize: mf.FileSize,
Written: pb,
})
},
}
}
// MultipartFieldCallbackFunc receives live multipart upload progress updates.
type MultipartFieldCallbackFunc func(MultipartFieldProgress)
// MultipartFieldProgress contains upload progress details for a multipart field.
type MultipartFieldProgress struct {
Name string
FileName string
FileSize int64
Written int64
}
// String returns the string representation of [MultipartFieldProgress].
func (mfp MultipartFieldProgress) String() string {
return fmt.Sprintf("FieldName: %s, FileName: %s, FileSize: %v, Written: %v",
mfp.Name, mfp.FileName, mfp.FileSize, mfp.Written)
}
type multipartProgressWriter struct {
w io.Writer
pb int64
f func(int64)
}
func (mpw *multipartProgressWriter) Write(p []byte) (n int, err error) {
n, err = mpw.w.Write(p)
if n <= 0 {
return
}
mpw.pb += int64(n)
mpw.f(mpw.pb)
return
}