diff options
Diffstat (limited to 'http/files.go')
-rw-r--r-- | http/files.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/http/files.go b/http/files.go new file mode 100644 index 00000000..06e5fd24 --- /dev/null +++ b/http/files.go @@ -0,0 +1,58 @@ +package http + +import ( + "mime/multipart" + "strings" + "github.com/sirupsen/logrus" +) + +type fileData map[string]interface{} + +type FileUpload struct { + Name string `json:"name"` + MimeType string `json:"mimetype"` +} + +func (d fileData) push(k string, v []*multipart.FileHeader) { + if len(v) == 0 { + // doing nothing + return + } + + chunks := make([]string, 0) + for _, chunk := range strings.Split(k, "[") { + chunks = append(chunks, strings.Trim(chunk, "]")) + } + + d.pushChunk(chunks, v) +} + +func (d fileData) pushChunk(k []string, v []*multipart.FileHeader) { + logrus.Print(v) + if len(v) == 0 || v[0] == nil { + return + } + + head := k[0] + tail := k[1:] + if len(k) == 1 { + d[head] = FileUpload{ + Name: v[0].Filename, + MimeType: v[0].Header.Get("Content-Type"), + } + return + } + + // unnamed array + if len(tail) == 1 && tail[0] == "" { + d[head] = v + return + } + + if p, ok := d[head]; !ok { + d[head] = make(fileData) + d[head].(fileData).pushChunk(tail, v) + } else { + p.(fileData).pushChunk(tail, v) + } +} |