1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 20:13:22 +00:00

Support MultipartForm

This commit is contained in:
Fenny 2020-01-08 01:48:31 -05:00
parent 5a1ae0be1b
commit 9323a2654d

View File

@ -3,7 +3,6 @@ package fiber
import (
"bytes"
"encoding/base64"
"errors"
"mime"
"mime/multipart"
"path/filepath"
@ -13,12 +12,6 @@ import (
"github.com/valyala/fasthttp"
)
// Context struct
type FileHeader struct {
*multipart.FileHeader
Type string
}
// Next :
func (ctx *Ctx) Next() {
ctx.next = true
@ -78,46 +71,36 @@ func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
}
// Form :
func (ctx *Ctx) Form() (*multipart.Form, error) {
func (ctx *Ctx) MultipartForm() *multipart.Form {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return nil, err
return nil
}
return form, nil
return form
}
// 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
}
// // FormValue :
// func (ctx *Ctx) FormValues(key string) (values []string) {
// form, err := ctx.Fasthttp.MultipartForm()
// if err != nil {
// return values
// }
// return form.Value[key]
// }
//
// // FormFile :
// func (ctx *Ctx) FormFiles(key string) (files []*multipart.FileHeader) {
// form, err := ctx.Fasthttp.MultipartForm()
// if err != nil {
// return files
// }
// files = form.File[key]
// return files
// }
// SaveFile :
func (fh *FileHeader) Save(path string) {
fasthttp.SaveMultipartFile(fh.FileHeader, path)
func (ctx *Ctx) SaveFile(fh *multipart.FileHeader, path string) {
fasthttp.SaveMultipartFile(fh, path)
}
// Body :