summaryrefslogtreecommitdiff
path: root/http/files.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-05-31 14:10:59 +0300
committerWolfy-J <[email protected]>2018-05-31 14:10:59 +0300
commit48f4f7a39a2336be24cc74b4116c02cc941dbd9a (patch)
tree3de1d379e43fe0772fb699852eac08ded6bbe644 /http/files.go
parentec2af29c17402145547699e719902d0f3f2ec8ec (diff)
http support
Diffstat (limited to 'http/files.go')
-rw-r--r--http/files.go58
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)
+ }
+}