Zhen Liu
Zhen Liu

Reputation: 7932

Check that a byte array is an image

I have a server endpoint in gin that receives an byte array.

func UploadImageHandler(c *gin.Context) {
    body, err := c.GetRawData()

    // how do I make sure this body byte array is image?
}

I need to ensure that the byte array is an image.

How would I be able to do this check in Go?

Upvotes: 1

Views: 1020

Answers (1)

Matias Barrios
Matias Barrios

Reputation: 5056

I think you can do this : https://golang.org/pkg/image/#Decode

You will end up with :

func UploadImageHandler(c *gin.Context) {
    body, err := c.GetRawData()

    img, _, err := image.Decode(bytes.NewReader(body))
    if err != nil {
    ....
}

I have not tested though.

Upvotes: 2

Related Questions