14 // MultiFileReader reads from a `commands.Node` (which can be a directory of files
15 // or a regular file) as HTTP multipart encoded data.
16 type MultiFileReader struct {
19 // directory stack for NextFile
25 mpWriter *multipart.Writer
29 // if true, the data will be type 'multipart/form-data'
30 // if false, the data will be type 'multipart/mixed'
34 // NewMultiFileReader constructs a MultiFileReader. `file` can be any `commands.Directory`.
35 // If `form` is set to true, the multipart data will have a Content-Type of 'multipart/form-data',
36 // if `form` is false, the Content-Type will be 'multipart/mixed'.
37 func NewMultiFileReader(file Directory, form bool) *MultiFileReader {
40 mfr := &MultiFileReader{
41 files: []DirIterator{it},
46 mfr.mpWriter = multipart.NewWriter(&mfr.buf)
51 func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
53 defer mfr.mutex.Unlock()
55 // if we are closed and the buffer is flushed, end reading
56 if mfr.closed && mfr.buf.Len() == 0 {
60 // if the current file isn't set, advance to the next file
61 if mfr.currentFile == nil {
65 if len(mfr.files) == 0 {
68 return mfr.buf.Read(buf)
71 if !mfr.files[len(mfr.files)-1].Next() {
72 if mfr.files[len(mfr.files)-1].Err() != nil {
73 return 0, mfr.files[len(mfr.files)-1].Err()
75 mfr.files = mfr.files[:len(mfr.files)-1]
76 mfr.path = mfr.path[:len(mfr.path)-1]
80 entry = mfr.files[len(mfr.files)-1]
83 // handle starting a new file part
86 mfr.currentFile = entry.Node()
88 // write the boundary and headers
89 header := make(textproto.MIMEHeader)
90 filename := url.QueryEscape(path.Join(path.Join(mfr.path...), entry.Name()))
91 header.Set("Content-Disposition", fmt.Sprintf("file; filename=\"%s\"", filename))
93 var contentType string
95 switch f := entry.Node().(type) {
97 contentType = "application/symlink"
100 mfr.files = append(mfr.files, newIt)
101 mfr.path = append(mfr.path, entry.Name())
102 contentType = "application/x-directory"
104 // otherwise, use the file as a reader to read its contents
105 contentType = "application/octet-stream"
107 return 0, ErrNotSupported
110 header.Set("Content-Type", contentType)
111 if rf, ok := entry.Node().(FileInfo); ok {
112 header.Set("abspath", rf.AbsPath())
115 _, err := mfr.mpWriter.CreatePart(header)
122 // if the buffer has something in it, read from it
123 if mfr.buf.Len() > 0 {
124 return mfr.buf.Read(buf)
127 // otherwise, read from file data
128 switch f := mfr.currentFile.(type) {
130 written, err = f.Read(buf)
136 if err := mfr.currentFile.Close(); err != nil {
140 mfr.currentFile = nil
144 // Boundary returns the boundary string to be used to separate files in the multipart data
145 func (mfr *MultiFileReader) Boundary() string {
146 return mfr.mpWriter.Boundary()