From 6aa5c2b2ef7683c63d3f9b9521e58899a52e4446 Mon Sep 17 00:00:00 2001 From: houseme Date: Mon, 23 May 2022 22:09:11 +0800 Subject: [PATCH] Fix/1748 issues #1748 (#1817) --- .golangci.yml | 2 +- net/ghttp/ghttp_request_param_file.go | 6 ++++ .../ghttp_z_unit_feature_request_file_test.go | 36 +++++++++++++++++++ util/gconv/gconv.go | 4 +-- util/gconv/gconv_scan.go | 1 - util/gutil/gutil.go | 2 +- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index cb39e8053..af6a88165 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,6 @@ ## This file contains all available configuration options ## with their default values. -# + # See https://github.com/golangci/golangci-lint#config-file run: issues-exit-code: 1 #Default diff --git a/net/ghttp/ghttp_request_param_file.go b/net/ghttp/ghttp_request_param_file.go index 3e1b33966..d32ea9c71 100644 --- a/net/ghttp/ghttp_request_param_file.go +++ b/net/ghttp/ghttp_request_param_file.go @@ -16,6 +16,7 @@ import ( "github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/internal/intlog" + "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/grand" @@ -27,6 +28,11 @@ type UploadFile struct { ctx context.Context } +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (f UploadFile) MarshalJSON() ([]byte, error) { + return json.Marshal(f.FileHeader) +} + // UploadFiles is an array type of *UploadFile. type UploadFiles []*UploadFile diff --git a/net/ghttp/ghttp_z_unit_feature_request_file_test.go b/net/ghttp/ghttp_z_unit_feature_request_file_test.go index 53f020045..44dac9815 100644 --- a/net/ghttp/ghttp_z_unit_feature_request_file_test.go +++ b/net/ghttp/ghttp_z_unit_feature_request_file_test.go @@ -216,3 +216,39 @@ func Test_Params_Strict_Route_File_Single(t *testing.T) { t.Assert(gfile.GetContents(dstPath), gfile.GetContents(srcPath)) }) } + +func Test_Params_File_Upload_Required(t *testing.T) { + type Req struct { + gmeta.Meta `method:"post" mime:"multipart/form-data"` + File *ghttp.UploadFile `type:"file" v:"required#upload file is required"` + } + type Res struct{} + + dstDirPath := gfile.Temp(gtime.TimestampNanoStr()) + s := g.Server(guid.S()) + s.BindHandler("/upload/required", func(ctx context.Context, req *Req) (res *Res, err error) { + var ( + r = g.RequestFromCtx(ctx) + ) + + file := req.File + if name, err := file.Save(dstDirPath); err == nil { + r.Response.WriteExit(name) + } + r.Response.WriteExit("upload failed") + return + }) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + time.Sleep(100 * time.Millisecond) + // file is empty + gtest.C(t, func(t *gtest.T) { + client := g.Client() + client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) + _, err := client.Post(ctx, "/upload/required") + if err != nil { + t.Assert(err.Error(), "upload file is required") + } + }) +} diff --git a/util/gconv/gconv.go b/util/gconv/gconv.go index b2de0d507..1b4d753ed 100644 --- a/util/gconv/gconv.go +++ b/util/gconv/gconv.go @@ -79,7 +79,7 @@ func Bytes(any interface{}) []byte { ok = true bytes = make([]byte, originValueAndKind.OriginValue.Len()) ) - for i, _ := range bytes { + for i := range bytes { int32Value := Int32(originValueAndKind.OriginValue.Index(i).Interface()) if int32Value < 0 || int32Value > math.MaxUint8 { ok = false @@ -112,7 +112,7 @@ func Runes(any interface{}) []rune { } // String converts `any` to string. -// It's most commonly used converting function. +// It's most commonly used converting function func String(any interface{}) string { if any == nil { return "" diff --git a/util/gconv/gconv_scan.go b/util/gconv/gconv_scan.go index 426b699e8..fcad56616 100644 --- a/util/gconv/gconv_scan.go +++ b/util/gconv/gconv_scan.go @@ -53,7 +53,6 @@ func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) pointerType, ) } - } // Direct assignment checks! var ( diff --git a/util/gutil/gutil.go b/util/gutil/gutil.go index 09c5046be..c140c2e5b 100644 --- a/util/gutil/gutil.go +++ b/util/gutil/gutil.go @@ -66,7 +66,7 @@ func IsEmpty(value interface{}) bool { func Keys(mapOrStruct interface{}) (keysOrAttrs []string) { keysOrAttrs = make([]string, 0) if m, ok := mapOrStruct.(map[string]interface{}); ok { - for k, _ := range m { + for k := range m { keysOrAttrs = append(keysOrAttrs, k) } return