From de6a633248f925d544205c7593a6d1d7e8bc2225 Mon Sep 17 00:00:00 2001 From: buttercannfly <36593447+buttercannfly@users.noreply.github.com> Date: Tue, 19 Sep 2023 18:05:46 +0800 Subject: [PATCH] Update file_type.go --- pkg/checker/file_type.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/checker/file_type.go b/pkg/checker/file_type.go index be4ad38d..71f9f2a5 100644 --- a/pkg/checker/file_type.go +++ b/pkg/checker/file_type.go @@ -1,26 +1,25 @@ package checker import ( - "image/jpeg" - "image/png" + "image" + _ "image/gif" // use init to support decode jpeg,jpg,png,gif + _ "image/jpeg" + _ "image/png" "io" "strings" ) -// IsSupportedImageFile currently answers support image type is `image/jpeg,image/jpg,image/png` +// IsSupportedImageFile currently answers support image type is `image/jpeg,image/jpg,image/png, image/gif` func IsSupportedImageFile(file io.Reader, ext string) bool { + ext = strings.TrimPrefix(ext, ".") var err error switch strings.ToUpper(ext) { - case "JPEG": - _, err = jpeg.Decode(file) - case "PNG": - _, err = png.Decode(file) + case "JPG", "JPEG", "PNG", "GIF": // only allow for `image/jpeg,image/jpg,image/png, image/gif` + _, _, 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 "JPG": - return true default: return false }