forked from TRON-US/go-btfs-files
-
Notifications
You must be signed in to change notification settings - Fork 7
/
multifilereader.go
181 lines (150 loc) · 4.29 KB
/
multifilereader.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
package files
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/textproto"
"net/url"
"path"
"strconv"
"strings"
"sync"
)
// MultiFileReader reads from a `commands.Node` (which can be a directory of files
// or a regular file) as HTTP multipart encoded data.
type MultiFileReader struct {
io.Reader
// directory stack for NextFile
files []DirIterator
path []string
currentFile Node
buf bytes.Buffer
mpWriter *multipart.Writer
closed bool
mutex *sync.Mutex
// if true, the content disposition will be "form-data"
// if false, the content disposition will be "attachment"
form bool
}
// NewMultiFileReader constructs a MultiFileReader. `file` can be any `commands.Directory`.
// If `form` is set to true, the Content-Disposition will be "form-data".
// Otherwise, it will be "attachment".
func NewMultiFileReader(file Directory, form bool) *MultiFileReader {
it := file.Entries()
mfr := &MultiFileReader{
files: []DirIterator{it},
path: []string{""},
form: form,
mutex: &sync.Mutex{},
}
mfr.mpWriter = multipart.NewWriter(&mfr.buf)
return mfr
}
func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
mfr.mutex.Lock()
defer mfr.mutex.Unlock()
// if we are closed and the buffer is flushed, end reading
if mfr.closed && mfr.buf.Len() == 0 {
return 0, io.EOF
}
// if the current file isn't set, advance to the next file
if mfr.currentFile == nil {
var entry DirEntry
for entry == nil {
if len(mfr.files) == 0 {
mfr.mpWriter.Close()
mfr.closed = true
return mfr.buf.Read(buf)
}
if !mfr.files[len(mfr.files)-1].Next() {
if mfr.files[len(mfr.files)-1].Err() != nil {
return 0, mfr.files[len(mfr.files)-1].Err()
}
mfr.files = mfr.files[:len(mfr.files)-1]
mfr.path = mfr.path[:len(mfr.path)-1]
continue
}
entry = mfr.files[len(mfr.files)-1]
}
// handle starting a new file part
if !mfr.closed {
mfr.currentFile = entry.Node()
// write the boundary and headers
header := make(textproto.MIMEHeader)
filename := url.QueryEscape(path.Join(path.Join(mfr.path...), entry.Name()))
mfr.addContentDisposition(header, filename)
var contentType string
switch f := entry.Node().(type) {
case *Symlink:
contentType = "application/symlink"
case Directory:
newIt := f.Entries()
mfr.files = append(mfr.files, newIt)
mfr.path = append(mfr.path, entry.Name())
contentType = "application/x-directory"
case File:
// otherwise, use the file as a reader to read its contents
contentType = "application/octet-stream"
default:
return 0, ErrNotSupported
}
header.Set("Content-Type", contentType)
if rf, ok := entry.Node().(FileInfo); ok {
header.Set("abspath", rf.AbsPath())
// attach file size to content-disposition when available
// according to https://tools.ietf.org/html/rfc2183
}
_, err := mfr.mpWriter.CreatePart(header)
if err != nil {
return 0, err
}
}
}
// if the buffer has something in it, read from it
if mfr.buf.Len() > 0 {
return mfr.buf.Read(buf)
}
// otherwise, read from file data
switch f := mfr.currentFile.(type) {
case File:
written, err = f.Read(buf)
if err != io.EOF {
return written, err
}
}
if err := mfr.currentFile.Close(); err != nil {
return written, err
}
mfr.currentFile = nil
return written, nil
}
func (mfr *MultiFileReader) addContentDisposition(header textproto.MIMEHeader, filename string) {
sb := &strings.Builder{}
params := url.Values{}
if mode := mfr.currentFile.Mode(); mode != 0 {
params.Add("mode", "0"+strconv.FormatUint(uint64(mode), 8))
}
if mtime := mfr.currentFile.ModTime(); !mtime.IsZero() {
params.Add("mtime", strconv.FormatInt(mtime.Unix(), 10))
if n := mtime.Nanosecond(); n > 0 {
params.Add("mtime-nsecs", strconv.FormatInt(int64(n), 10))
}
}
sb.Grow(120)
if mfr.form {
sb.WriteString("form-data; name=\"file")
if len(params) > 0 {
fmt.Fprintf(sb, "?%s", params.Encode())
}
sb.WriteString("\"")
} else {
sb.WriteString("attachment")
}
fmt.Fprintf(sb, "; filename=\"%s\"", url.QueryEscape(filename))
header.Set(contentDispositionHeader, sb.String())
}
// Boundary returns the boundary string to be used to separate files in the multipart data
func (mfr *MultiFileReader) Boundary() string {
return mfr.mpWriter.Boundary()
}