Skip to content

Commit

Permalink
Release/1.2.5 (#761)
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Vollrath <adam.d.vollrath@gmail.com>
Co-authored-by: shuai <lishuailing@sifou.com>
Co-authored-by: kelvinkuo <kelvinkuo224@gmail.com>
Co-authored-by: hgaol <dhangao@hotmail.com>
Co-authored-by: sy-records <52o@qq52o.cn>
Co-authored-by: Adam Vollrath <adam.d.vollrath@gmail.com>
Co-authored-by: kumfo <kumfo@sifou.com>
Co-authored-by: Yang Wong <yang wang>
Co-authored-by: hbsciw <hbsciw@gmail.com>
  • Loading branch information
8 people committed Jan 29, 2024
1 parent abecc1c commit 968f23d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/checker/file_type.go
Expand Up @@ -29,21 +29,53 @@ import (
"strings"
)

const (
maxImageSize = 8192 * 8192
)

// IsSupportedImageFile currently answers support image type is
// `image/jpeg, image/jpg, image/png, image/gif, image/webp`
func IsSupportedImageFile(file io.Reader, ext string) bool {
ext = strings.ToLower(strings.TrimPrefix(ext, "."))
var err error
switch ext {
case "jpg", "jpeg", "png", "gif": // only allow for `image/jpeg,image/jpg,image/png, image/gif`
if !checkImageSize(file) {
return false
}
_, _, err = image.Decode(file)
case "ico":
// TODO: There is currently no good Golang library to parse whether the image is in ico format.
return true
case "webp":
if !checkWebpSize(file) {
return false
}
_, err = webp.Decode(file)
default:
return false
}
return err == nil
}

func checkImageSize(file io.Reader) bool {
config, _, err := image.DecodeConfig(file)
if err != nil {
return false
}
if (config.Width * config.Height) > maxImageSize {
return false
}
return true
}

func checkWebpSize(file io.Reader) bool {
config, err := webp.DecodeConfig(file)
if err != nil {
return false
}
if (config.Width * config.Height) > maxImageSize {
return false
}
return true
}

0 comments on commit 968f23d

Please sign in to comment.