multifilereader.go 2.97 KB
Newer Older
1 2 3 4
package http

import (
	"bytes"
5
	"fmt"
6 7
	"io"
	"mime/multipart"
8
	"net/textproto"
9
	"sync"
10

11
	files "github.com/jbenet/go-ipfs/commands/files"
12 13
)

14 15
// MultiFileReader reads from a `commands.File` (which can be a directory of files
// or a regular file) as HTTP multipart encoded data.
16 17 18
type MultiFileReader struct {
	io.Reader

19
	files       files.File
20
	currentFile io.Reader
21 22 23
	buf         bytes.Buffer
	mpWriter    *multipart.Writer
	closed      bool
24
	mutex       *sync.Mutex
25 26 27 28

	// if true, the data will be type 'multipart/form-data'
	// if false, the data will be type 'multipart/mixed'
	form bool
29 30
}

31 32 33
// NewMultiFileReader constructs a MultiFileReader. `file` can be any `commands.File`.
// If `form` is set to true, the multipart data will have a Content-Type of 'multipart/form-data',
// if `form` is false, the Content-Type will be 'multipart/mixed'.
34
func NewMultiFileReader(file files.File, form bool) *MultiFileReader {
35 36
	mfr := &MultiFileReader{
		files: file,
37
		form:  form,
38
		mutex: &sync.Mutex{},
39 40 41 42 43 44 45
	}
	mfr.mpWriter = multipart.NewWriter(&mfr.buf)

	return mfr
}

func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
46 47 48
	mfr.mutex.Lock()
	defer mfr.mutex.Unlock()

49
	// if we are closed and the buffer is flushed, end reading
50 51 52 53 54 55
	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 {
56
		file, err := mfr.files.NextFile()
57
		if err == io.EOF {
58 59 60 61 62 63
			mfr.mpWriter.Close()
			mfr.closed = true
		} else if err != nil {
			return 0, err
		}

64
		// handle starting a new file part
65
		if !mfr.closed {
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
			if file.IsDirectory() {
				// if file is a directory, create a multifilereader from it
				// (using 'multipart/mixed')
				mfr.currentFile = NewMultiFileReader(file, false)
			} else {
				// otherwise, use the file as a reader to read its contents
				mfr.currentFile = file
			}

			// write the boundary and headers
			header := make(textproto.MIMEHeader)
			if mfr.form {
				contentDisposition := fmt.Sprintf("form-data; name=\"file\"; filename=\"%s\"", file.FileName())
				header.Set("Content-Disposition", contentDisposition)
			} else {
				header.Set("Content-Disposition", fmt.Sprintf("file; filename=\"%s\"", file.FileName()))
			}

			if file.IsDirectory() {
				boundary := mfr.currentFile.(*MultiFileReader).Boundary()
				header.Set("Content-Type", fmt.Sprintf("multipart/mixed; boundary=%s", boundary))
			} else {
				header.Set("Content-Type", "application/octet-stream")
			}

			_, err := mfr.mpWriter.CreatePart(header)
92 93 94 95 96 97
			if err != nil {
				return 0, err
			}
		}
	}

98
	// if the buffer has something in it, read from it
99
	if mfr.buf.Len() > 0 {
100
		return mfr.buf.Read(buf)
101 102
	}

103 104 105
	// otherwise, read from file data
	written, err = mfr.currentFile.Read(buf)
	if err == io.EOF {
106
		mfr.currentFile = nil
107
		return written, nil
108 109 110 111
	}
	return written, err
}

112
// Boundary returns the boundary string to be used to separate files in the multipart data
113 114 115
func (mfr *MultiFileReader) Boundary() string {
	return mfr.mpWriter.Boundary()
}