我正在使用Goa v3来设计一个端点,该端点允许我使用multipart/form-data
POST请求上传文件(更准确地说,图像(。 我声明如下Service
:
var _ = Service("images", func() {
HTTP(func() {
Path("/images")
})
Method("upload", func() {
HTTP(func() {
POST("/")
MultipartRequest()
})
Payload(func() {
Description("Multipart request Payload")
Attribute("File", Bytes, "File")
})
Result(ImageList)
})
})
我运行goa gen
和goa example
命令来生成样板代码。除了cmd
目录之外,example
代码还生成images.go
主文件和multipart.go
文件来声明编码器和解码器逻辑,例如:
func ImagesUploadDecoderFunc(mr *multipart.Reader, p **images.UploadPayload) error {
// Add multipart request decoder logic here
return nil
}
我可以使用mr.NextPart()
显然获得对图像文件的引用,但我仍然不确定如何将其映射到images.UploadPayload
类型的Bytes
字段(或者也许我应该声明另一种类型的字段来处理文件??
我在果阿文档中找不到任何示例。
好的,我终于明白了multipart.Reader
的工作原理,我想出了一个解决方案。
首先让我们澄清一下,与 Goa 通常的工作方式不同(将请求参数与Payload
字段"自动"映射(,使用MultipartRequest()
,我必须自己进行映射,因此Payload
实际上可以具有任何结构。
就我而言,我重新定义了我的Payload
结构,如下所示:
// ImageUpload single image upload element
var ImageUpload = Type("ImageUpload", func() {
Description("A single Image Upload type")
Attribute("type", String)
Attribute("bytes", Bytes)
Attribute("name", String)
})
// ImageUploadPayload is a list of files
var ImageUploadPayload = Type("ImageUploadPayload", func() {
Description("Image Upload Payload")
Attribute("Files", ArrayOf(ImageUpload), "Collection of uploaded files")
})
简而言之,我想支持上传多个文件,每个文件都有其 mime 类型、文件名和数据。
为了实现这一点,我实现了这样的multipart.go
解码器函数:
func ImagesUploadDecoderFunc(mr *multipart.Reader, p **images.ImageUploadPayload) error {
res := images.ImageUploadPayload{}
for {
p, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
_, params, err := mime.ParseMediaType(p.Header.Get("Content-Disposition"))
if err != nil {
// can't process this entry, it probably isn't an image
continue
}
disposition, _, err := mime.ParseMediaType(p.Header.Get("Content-Type"))
// the disposition can be, for example 'image/jpeg' or 'video/mp4'
// I want to support only image files!
if err != nil || !strings.HasPrefix(disposition, "image/") {
// can't process this entry, it probably isn't an image
continue
}
if params["name"] == "file" {
bytes, err := ioutil.ReadAll(p)
if err != nil {
// can't process this entry, for some reason
fmt.Fprintln(os.Stderr, err)
continue
}
filename := params["filename"]
imageUpload := images.ImageUpload{
Type: &disposition,
Bytes: bytes,
Name: &filename,
}
res.Files = append(res.Files, &imageUpload)
}
}
*p = &res
return nil
}