2

我正在使用 Goa v3 设计一个端点,允许我通过multipart/form-dataPOST 请求上传文件(更准确地说是图像)。我已宣布以下内容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 gengoa 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()并获得对图像文件的引用,但我仍然不确定我应该如何将它映射到类型中的Bytes字段images.UploadPayload(或者我应该声明另一种类型的字段来处理文件??)。

我在 Goa 文档中找不到任何示例。

4

1 回答 1

2

好的,我终于明白了它是如何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
}
于 2019-10-13T00:42:52.807 回答