Skip to content

Commit

Permalink
refactor(*): return FileHeader in the File method
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jan 12, 2021
1 parent 1ecabda commit 2fcf231
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion service/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions service/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2fcf231

Please sign in to comment.