From 2fcf231551bca8469208e087c34da5ca56ee5797 Mon Sep 17 00:00:00 2001 From: iawia002 Date: Tue, 12 Jan 2021 20:18:45 +0800 Subject: [PATCH] refactor(*): return FileHeader in the File method --- service/context.go | 8 ++++---- service/source.go | 2 +- service/source_test.go | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/service/context.go b/service/context.go index d6df489b..c9a7a027 100644 --- a/service/context.go +++ b/service/context.go @@ -77,7 +77,7 @@ type ValueContainer interface { // or "multipart/form-data". Form(key string) ([]string, bool) // File returns a file reader when "Content-Type" is "multipart/form-data". - File(key string) (multipart.File, bool) + File(key string) (multipart.File, *multipart.FileHeader, bool) // Body returns a reader to read data from request body. // The reader only can read once. Body() (reader io.ReadCloser, contentType string, ok bool) @@ -152,9 +152,9 @@ func (c *container) removeEmpties(values []string) ([]string, bool) { } // File returns a file reader when "Content-Type" is "multipart/form-data". -func (c *container) File(key string) (multipart.File, bool) { - file, _, err := c.request.FormFile(key) - return file, err == nil +func (c *container) File(key string) (multipart.File, *multipart.FileHeader, bool) { + file, fileHeader, err := c.request.FormFile(key) + return file, fileHeader, err == nil } // Body returns a reader to read data from request body. diff --git a/service/source.go b/service/source.go index 741599ee..a9cff3fd 100644 --- a/service/source.go +++ b/service/source.go @@ -252,7 +252,7 @@ func (g *FileParameterGenerator) Validate(name string, defaultValue interface{}, // Generate generates an object by data from value container. func (g *FileParameterGenerator) Generate(ctx context.Context, vc ValueContainer, consumers []Consumer, name string, target reflect.Type) (interface{}, error) { - file, ok := vc.File(name) + file, _, ok := vc.File(name) if !ok { return nil, nil } diff --git a/service/source_test.go b/service/source_test.go index a245e15d..cc09b378 100644 --- a/service/source_test.go +++ b/service/source_test.go @@ -90,11 +90,11 @@ func (f *file) Close() error { return nil } -func (v *vc) File(key string) (multipart.File, bool) { +func (v *vc) File(key string) (multipart.File, *multipart.FileHeader, bool) { if key == testKey { - return &file{[]byte("test file"), 0}, true + return &file{[]byte("test file"), 0}, &multipart.FileHeader{Filename: "test"}, true } - return nil, false + return nil, nil, false } func (v *vc) Body() (reader io.ReadCloser, contentType string, ok bool) {