caicloud / nirvana

Golang Restful API Framework for Productivity

Home Page:https://caicloud.github.io/nirvana/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

updown file API can not get fileHeader

zjj2wry opened this issue · comments

Is this a BUG REPORT or FEATURE REQUEST?:

Uncomment only one, leave it on its own line:

/kind bug
/kind feature

c.request.FormFile(key) should return fileHeader, because fileHeader have content and filename. but file only have content

// 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
}

What happened:

What you expected to happen:

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

/assign @kdada

take a look, i can create PR fix this

You can bypass it through extracting request from context directly.

If you think the API has weak adaptability, Please consider about implementing a stronger interface to replace multipart.File.

If you think the API has weak adaptability, Please consider about implementing a stronger interface to replace multipart.File.

make sense,but i think it would be better have a mechanism for definition to pass multiple key values. Or let the user customize the definition.

For APIs, users don't need to know how to get data from http requests. They should only care about business logic.

An example with RESTful style:

API: POST /files?name=xxx
Function: func CreateFile(name string, data io.ReadCloser) (SomeObject, error)

Of course, sometimes APIs need to adapt to strange demands. So you can get meta from http request directly. But for convenient, we can extends File(key string) (multipart.File, bool) to:

type File interface {
    miltipart.File
    Header() *multipart.FileHeader
}

File(key string) (File, bool)

Then users can define their APIs like:

Function: func CreateFile(name string, data service.File) (SomeObject, error)

But once you uses this method, your logics will have dependencies with nirvana.

i think it would be better define type CustomFile interface in user own project instead of nirvana project. Can we set a custom definition? Such as definition.CustomFile

Actually, users can implement a new interface which is a subset of service.File. For example:

type MyFile interface {
    io.ReadCloser
    Header() *multipart.FileHeader
}

func CreateFile(name string, data MyFile) (SomeObject, error)

Nirvana supports it.