1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

Merge 937a8278a821fe517d22841f199b89a022ac732e into 1534abdb050acaa5aa6ef27f94e91bca97e6faa3

This commit is contained in:
hailaz 2025-04-02 20:03:41 +08:00 committed by GitHub
commit c41b0af353
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 1 deletions

View File

@ -19,9 +19,15 @@ import (
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/grand"
)
// init initializes the type converters for *UploadFile.
func init() {
_ = gconv.RegisterTypeConverterFunc(stringToUploadFile)
}
// UploadFile wraps the multipart uploading file with more and convenient features.
type UploadFile struct {
*multipart.FileHeader `json:"-"`
@ -36,13 +42,18 @@ func (f UploadFile) MarshalJSON() ([]byte, error) {
// UploadFiles is an array type of *UploadFile.
type UploadFiles []*UploadFile
// stringToUploadFile is a custom type converter for converting string to *ghttp.UploadFile.
func stringToUploadFile(in string) (*UploadFile, error) {
return &UploadFile{}, nil
}
// Save saves the single uploading file to directory path and returns the saved file name.
//
// The parameter `dirPath` should be a directory path, or it returns error.
//
// Note that it will OVERWRITE the target file if there's already a same name file exist.
func (f *UploadFile) Save(dirPath string, randomlyRename ...bool) (filename string, err error) {
if f == nil {
if f == nil || f.FileHeader == nil {
return "", gerror.NewCode(
gcode.CodeMissingParameter,
"file is empty, maybe you retrieve it from invalid field name or form enctype",

View File

@ -19,6 +19,7 @@ import (
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gmeta"
"github.com/gogf/gf/v2/util/gtag"
"github.com/gogf/gf/v2/util/guid"
)
@ -726,3 +727,30 @@ func Test_Issue4093(t *testing.T) {
t.Assert(client.PostContent(ctx, "/test"), `{"page":1,"pageSize":10,"pagination":true,"name":"john","number":1}`)
})
}
// https://github.com/gogf/gf/issues/4193
func Test_Issue4193(t *testing.T) {
type Req struct {
gmeta.Meta `method:"post" mime:"multipart/form-data"`
File ghttp.UploadFile `v:"required" type:"file"`
}
type Res struct{}
s := g.Server(guid.S())
s.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse)
s.BindHandler("/upload/single", func(ctx context.Context, req *Req) (res *Res, err error) {
return
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
// normal name
gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
content := client.PostContent(ctx, "/upload/single", g.Map{
"file": "",
})
t.Assert(content, "{\"code\":51,\"message\":\"The File field is required\",\"data\":null}")
})
}