-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_post.go
92 lines (81 loc) · 1.94 KB
/
context_post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
92
package cotton
import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
)
func (ctx *Context) initPostFormCache() {
if nil == ctx.postFormCache {
if nil != ctx.Request {
if e := ctx.Request.ParseMultipartForm(defaultMultipartMemory); e != nil {
if e != http.ErrNotMultipart {
panic(e)
}
}
ctx.postFormCache = ctx.Request.PostForm
} else {
ctx.postFormCache = url.Values{}
}
}
}
// GetAllPostForm get all post form value
func (ctx Context) GetAllPostForm() url.Values {
ctx.initPostFormCache()
return ctx.postFormCache
}
// GetPostForm get postform param
func (ctx *Context) GetPostForm(key string) string {
ctx.initPostFormCache()
if v, ok := ctx.postFormCache[key]; ok {
return v[0]
}
return ""
}
// GetPostFormArray get postform param array
func (ctx *Context) GetPostFormArray(key string) []string {
ctx.initPostFormCache()
if v, ok := ctx.postFormCache[key]; ok {
return v
}
return []string{}
}
// GetPostFormMap get postform param map
func (ctx *Context) GetPostFormMap(key string) (dicts map[string]string, exists bool) {
ctx.initPostFormCache()
return getValue(ctx.postFormCache, key)
}
// GetPostFormFile get postform file
func (ctx *Context) GetPostFormFile(key string) *multipart.FileHeader {
list := ctx.GetPostFormFileArray(key)
if len(list) > 0 {
return list[0]
}
return nil
}
// GetPostFormFileArray get postform files
func (ctx *Context) GetPostFormFileArray(key string) (list []*multipart.FileHeader) {
ctx.initPostFormCache()
if ctx.Request.MultipartForm != nil {
list, _ = ctx.Request.MultipartForm.File[key]
}
return
}
// SavePostFormFile save file
func (ctx *Context) SavePostFormFile(file *multipart.FileHeader, dst string) error {
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
os.MkdirAll(filepath.Dir(dst), 0755)
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, src)
return err
}