OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / ipfs / go-ipfs-files / multifilereader.go
1 package files
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "mime/multipart"
8         "net/textproto"
9         "net/url"
10         "path"
11         "sync"
12 )
13
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 {
17         io.Reader
18
19         // directory stack for NextFile
20         files []DirIterator
21         path  []string
22
23         currentFile Node
24         buf         bytes.Buffer
25         mpWriter    *multipart.Writer
26         closed      bool
27         mutex       *sync.Mutex
28
29         // if true, the data will be type 'multipart/form-data'
30         // if false, the data will be type 'multipart/mixed'
31         form bool
32 }
33
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 {
38         it := file.Entries()
39
40         mfr := &MultiFileReader{
41                 files: []DirIterator{it},
42                 path:  []string{""},
43                 form:  form,
44                 mutex: &sync.Mutex{},
45         }
46         mfr.mpWriter = multipart.NewWriter(&mfr.buf)
47
48         return mfr
49 }
50
51 func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
52         mfr.mutex.Lock()
53         defer mfr.mutex.Unlock()
54
55         // if we are closed and the buffer is flushed, end reading
56         if mfr.closed && mfr.buf.Len() == 0 {
57                 return 0, io.EOF
58         }
59
60         // if the current file isn't set, advance to the next file
61         if mfr.currentFile == nil {
62                 var entry DirEntry
63
64                 for entry == nil {
65                         if len(mfr.files) == 0 {
66                                 mfr.mpWriter.Close()
67                                 mfr.closed = true
68                                 return mfr.buf.Read(buf)
69                         }
70
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()
74                                 }
75                                 mfr.files = mfr.files[:len(mfr.files)-1]
76                                 mfr.path = mfr.path[:len(mfr.path)-1]
77                                 continue
78                         }
79
80                         entry = mfr.files[len(mfr.files)-1]
81                 }
82
83                 // handle starting a new file part
84                 if !mfr.closed {
85
86                         mfr.currentFile = entry.Node()
87
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))
92
93                         var contentType string
94
95                         switch f := entry.Node().(type) {
96                         case *Symlink:
97                                 contentType = "application/symlink"
98                         case Directory:
99                                 newIt := f.Entries()
100                                 mfr.files = append(mfr.files, newIt)
101                                 mfr.path = append(mfr.path, entry.Name())
102                                 contentType = "application/x-directory"
103                         case File:
104                                 // otherwise, use the file as a reader to read its contents
105                                 contentType = "application/octet-stream"
106                         default:
107                                 return 0, ErrNotSupported
108                         }
109
110                         header.Set("Content-Type", contentType)
111                         if rf, ok := entry.Node().(FileInfo); ok {
112                                 header.Set("abspath", rf.AbsPath())
113                         }
114
115                         _, err := mfr.mpWriter.CreatePart(header)
116                         if err != nil {
117                                 return 0, err
118                         }
119                 }
120         }
121
122         // if the buffer has something in it, read from it
123         if mfr.buf.Len() > 0 {
124                 return mfr.buf.Read(buf)
125         }
126
127         // otherwise, read from file data
128         switch f := mfr.currentFile.(type) {
129         case File:
130                 written, err = f.Read(buf)
131                 if err != io.EOF {
132                         return written, err
133                 }
134         }
135
136         if err := mfr.currentFile.Close(); err != nil {
137                 return written, err
138         }
139
140         mfr.currentFile = nil
141         return written, nil
142 }
143
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()
147 }