Added Form() FormValue() FormFile() => file.Save()

pull/6/head
Fenny 2020-01-08 00:49:12 -05:00
parent d2d91227dd
commit 5a1ae0be1b
1 changed files with 51 additions and 0 deletions

View File

@ -3,7 +3,9 @@ package fiber
import ( import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"errors"
"mime" "mime"
"mime/multipart"
"path/filepath" "path/filepath"
"strings" "strings"
@ -11,6 +13,12 @@ import (
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
// Context struct
type FileHeader struct {
*multipart.FileHeader
Type string
}
// Next : // Next :
func (ctx *Ctx) Next() { func (ctx *Ctx) Next() {
ctx.next = true ctx.next = true
@ -69,6 +77,49 @@ func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
return cs[:s], cs[s+1:], true return cs[:s], cs[s+1:], true
} }
// Form :
func (ctx *Ctx) Form() (*multipart.Form, error) {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return nil, err
}
return form, nil
}
// FormValue :
func (ctx *Ctx) FormValue(key string) string {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return ""
}
if len(form.Value[key]) == 0 {
return ""
}
return form.Value[key][0]
}
// FormFile :
func (ctx *Ctx) FormFile(key string) (*FileHeader, error) {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return nil, err
}
files := form.File[key]
if len(files) == 0 {
return nil, errors.New("there is no uploaded file associated with the given key")
}
fh := &FileHeader{
FileHeader: files[0],
Type: files[0].Header["Content-Type"][0],
}
return fh, nil
}
// SaveFile :
func (fh *FileHeader) Save(path string) {
fasthttp.SaveMultipartFile(fh.FileHeader, path)
}
// Body : // Body :
func (ctx *Ctx) Body(args ...interface{}) string { func (ctx *Ctx) Body(args ...interface{}) string {
if len(args) == 0 { if len(args) == 0 {