forked from bittorrent/go-btfs-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipartfile.go
281 lines (238 loc) · 5.81 KB
/
multipartfile.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package files
import (
"io"
"mime"
"mime/multipart"
"net/url"
"path"
"strconv"
"strings"
)
const (
multipartFormdataType = "multipart/form-data"
multipartMixedType = "multipart/mixed"
applicationDirectory = "application/x-directory"
applicationSymlink = "application/symlink"
applicationFile = "application/octet-stream"
contentTypeHeader = "Content-Type"
contentDispositionHeader = "Content-Disposition"
)
type multipartDirectory struct {
path string
walker *multipartWalker
// part is the part describing the directory. It's nil when implicit.
part *multipart.Part
size int64
}
type multipartWalker struct {
part *multipart.Part
reader *multipart.Reader
currAbsPath string
}
func (m *multipartWalker) consumePart() {
m.part = nil
}
func (m *multipartWalker) getPart() (*multipart.Part, error) {
if m.part != nil {
return m.part, nil
}
if m.reader == nil {
return nil, io.EOF
}
var err error
m.part, err = m.reader.NextPart()
if err == io.EOF {
m.reader = nil
}
return m.part, err
}
// NewFileFromPartReader creates a Directory from a multipart reader.
func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (Directory, error) {
switch mediatype {
case applicationDirectory, multipartFormdataType:
default:
return nil, ErrNotDirectory
}
return &multipartDirectory{
path: "/",
walker: &multipartWalker{
reader: reader,
},
}, nil
}
func IsMultiPartDirectory(d Directory) bool {
if _, ok := d.(*multipartDirectory); ok {
return true
} else {
return false
}
}
func (w *multipartWalker) nextFile() (Node, error) {
part, err := w.getPart()
if err != nil {
return nil, err
}
w.consumePart()
contentType := part.Header.Get(contentTypeHeader)
if contentType != "" {
var err error
contentType, _, err = mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
}
switch contentType {
case multipartFormdataType, applicationDirectory:
return &multipartDirectory{
part: part,
path: fileName(part),
walker: w,
}, nil
case applicationSymlink:
out, err := io.ReadAll(part)
if err != nil {
return nil, err
}
return NewLinkFile(string(out), nil), nil
default:
absPath := part.Header.Get("abspath")
rf := &ReaderFile{
reader: part,
abspath: absPath,
}
cdh := part.Header.Get(contentDispositionHeader)
_, params, err := mime.ParseMediaType(cdh)
if err != nil {
return nil, err
}
// ignore if size is not available
if size, ok := params["size"]; ok {
fsize, err := strconv.ParseInt(size, 10, 64)
if err != nil {
return nil, err
}
rf.fsize = fsize
}
w.currAbsPath = absPath
return rf, nil
}
}
// fileName returns a normalized filename from a part.
func fileName(part *multipart.Part) string {
v := part.Header.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(v)
if err != nil {
return ""
}
filename := params["filename"]
if escaped, err := url.QueryUnescape(filename); err == nil {
filename = escaped
} // if there is a unescape error, just treat the name as unescaped
return path.Clean("/" + filename)
}
// dirName appends a slash to the end of the filename, if not present.
// expects a _cleaned_ path.
func dirName(filename string) string {
if !strings.HasSuffix(filename, "/") {
filename += "/"
}
return filename
}
// isChild checks if child is a child of parent directory.
// expects a _cleaned_ path.
func isChild(child, parent string) bool {
return strings.HasPrefix(child, dirName(parent))
}
// makeRelative makes the child path relative to the parent path.
// expects a _cleaned_ path.
func makeRelative(child, parent string) string {
return strings.TrimPrefix(child, dirName(parent))
}
type multipartIterator struct {
f *multipartDirectory
curFile Node
curName string
err error
absRootPath string
}
func (it *multipartIterator) Name() string {
return it.curName
}
func (it *multipartIterator) Node() Node {
return it.curFile
}
func (it *multipartIterator) Next() bool {
if it.f.walker.reader == nil || it.err != nil {
return false
}
var part *multipart.Part
for {
part, it.err = it.f.walker.getPart()
if it.err != nil {
return false
}
name := fileName(part)
// Is the file in a different directory?
if !isChild(name, it.f.path) {
return false
}
// Have we already entered this directory?
if it.curName != "" && isChild(name, path.Join(it.f.path, it.curName)) {
it.f.walker.consumePart()
continue
}
// Make the path relative to the current directory.
name = makeRelative(name, it.f.path)
// Check if we need to create a fake directory (more than one
// path component).
if idx := strings.IndexByte(name, '/'); idx >= 0 {
it.curName = name[:idx]
it.curFile = &multipartDirectory{
path: path.Join(it.f.path, it.curName),
walker: it.f.walker,
}
return true
}
it.curName = name
// Finally, advance to the next file.
it.curFile, it.err = it.f.walker.nextFile()
return it.err == nil
}
}
func (it *multipartIterator) Err() error {
// We use EOF to signal that this iterator is done. That way, we don't
// need to check every time `Next` is called.
if it.err == io.EOF {
return nil
}
return it.err
}
func (it *multipartIterator) BreadthFirstTraversal() {
}
func (f *multipartDirectory) Entries() DirIterator {
return &multipartIterator{f: f}
}
func (f *multipartDirectory) Close() error {
if f.part != nil {
return f.part.Close()
}
return nil
}
func (f *multipartDirectory) Size() (int64, error) {
return f.size, nil
}
func (f *multipartDirectory) SetSize(size int64) error {
f.size = size
return nil
}
func (f *multipartDirectory) IsReedSolomon() bool {
return false
}
func MultiPartReader(d Directory) *multipart.Reader {
md, ok := d.(*multipartDirectory)
if !ok {
return nil
}
return md.walker.reader
}
var _ Directory = &multipartDirectory{}