2022-05-28 02:42:28 +00:00
|
|
|
// Copyright 2019 Gin Core Team. All rights reserved.
|
2019-06-18 11:49:10 +00:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type multipartRequest http.Request
|
|
|
|
|
|
|
|
var _ setter = (*multipartRequest)(nil)
|
|
|
|
|
2021-06-01 23:35:30 +00:00
|
|
|
var (
|
|
|
|
// ErrMultiFileHeader multipart.FileHeader invalid
|
|
|
|
ErrMultiFileHeader = errors.New("unsupported field type for multipart.FileHeader")
|
|
|
|
|
|
|
|
// ErrMultiFileHeaderLenInvalid array for []*multipart.FileHeader len invalid
|
|
|
|
ErrMultiFileHeaderLenInvalid = errors.New("unsupported len of array for []*multipart.FileHeader")
|
|
|
|
)
|
|
|
|
|
2019-06-18 11:49:10 +00:00
|
|
|
// TrySet tries to set a value by the multipart request with the binding a form file
|
2021-06-01 23:35:30 +00:00
|
|
|
func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (bool, error) {
|
2019-06-18 11:49:10 +00:00
|
|
|
if files := r.MultipartForm.File[key]; len(files) != 0 {
|
|
|
|
return setByMultipartFormFile(value, field, files)
|
|
|
|
}
|
|
|
|
|
|
|
|
return setByForm(value, field, r.MultipartForm.Value, key, opt)
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:04:28 +00:00
|
|
|
func setByMultipartFormFile(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error) {
|
2019-06-18 11:49:10 +00:00
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
switch value.Interface().(type) {
|
|
|
|
case *multipart.FileHeader:
|
|
|
|
value.Set(reflect.ValueOf(files[0]))
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
switch value.Interface().(type) {
|
|
|
|
case multipart.FileHeader:
|
|
|
|
value.Set(reflect.ValueOf(*files[0]))
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
slice := reflect.MakeSlice(value.Type(), len(files), len(files))
|
2021-09-30 02:04:28 +00:00
|
|
|
isSet, err = setArrayOfMultipartFormFiles(slice, field, files)
|
|
|
|
if err != nil || !isSet {
|
|
|
|
return isSet, err
|
2019-06-18 11:49:10 +00:00
|
|
|
}
|
|
|
|
value.Set(slice)
|
|
|
|
return true, nil
|
|
|
|
case reflect.Array:
|
|
|
|
return setArrayOfMultipartFormFiles(value, field, files)
|
|
|
|
}
|
2021-06-01 23:35:30 +00:00
|
|
|
return false, ErrMultiFileHeader
|
2019-06-18 11:49:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 02:04:28 +00:00
|
|
|
func setArrayOfMultipartFormFiles(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error) {
|
2019-06-18 11:49:10 +00:00
|
|
|
if value.Len() != len(files) {
|
2021-06-01 23:35:30 +00:00
|
|
|
return false, ErrMultiFileHeaderLenInvalid
|
2019-06-18 11:49:10 +00:00
|
|
|
}
|
|
|
|
for i := range files {
|
2021-09-30 02:04:28 +00:00
|
|
|
set, err := setByMultipartFormFile(value.Index(i), field, files[i:i+1])
|
|
|
|
if err != nil || !set {
|
|
|
|
return set, err
|
2019-06-18 11:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|