summaryrefslogtreecommitdiff
path: root/psr7/files.go
diff options
context:
space:
mode:
Diffstat (limited to 'psr7/files.go')
-rw-r--r--psr7/files.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/psr7/files.go b/psr7/files.go
new file mode 100644
index 00000000..31ddfec8
--- /dev/null
+++ b/psr7/files.go
@@ -0,0 +1,58 @@
+package psr7
+
+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)
+ }
+}