From b07dfad184675a406a82d36dbfdcee44691526bb Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Sun, 21 Aug 2022 23:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/image.go | 10 ++++++++++ util/imageCheck.go | 42 +++++++++++++++++++++++++++++++++++++++++ util/imageCheck_test.go | 30 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 util/imageCheck.go create mode 100644 util/imageCheck_test.go diff --git a/controller/image.go b/controller/image.go index 491d290..d70ce82 100644 --- a/controller/image.go +++ b/controller/image.go @@ -41,6 +41,16 @@ func (a *ImageController) UploadNiupic(c *gin.Context) { fileName := util.Md5Encode(fmt.Sprintf("%s%s", f.Filename, time.Now().String())) fildDir := fmt.Sprintf("%s%d%s_", "upload_tmp_", time.Now().Year(), time.Now().Month().String()) subfilepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt) + if strings.Contains(strings.ToUpper(fileExt), "JPEG") || strings.Contains(strings.ToUpper(fileExt), "JPG") || strings.Contains(strings.ToUpper(fileExt), "PNG") || strings.Contains(strings.ToUpper(fileExt), "GIF") { + _, err := util.CheckImageFile(subfilepath) + if err != nil { + c.JSON(200, gin.H{ + "code": 400, + "msg": "上传失败 文件格式异常!", + }) + return + } + } err := c.SaveUploadedFile(f, subfilepath) if err != nil { diff --git a/util/imageCheck.go b/util/imageCheck.go new file mode 100644 index 0000000..c7263f2 --- /dev/null +++ b/util/imageCheck.go @@ -0,0 +1,42 @@ +package util + +import ( + "image/gif" + "image/jpeg" + "image/png" + "log" + "os" + "strings" +) + +/** +@author: sre +@date: 2022/8/21 0021 +@desc: https://mp.weixin.qq.com/s/5oz0F2Fbn7Fh5edD8_zD5w +**/ + +// CheckImageFile 同时支持三种图片格式的真实性校验。 +func CheckImageFile(path string) (string, error) { + f, err := os.Open(path) + if err != nil { + log.Println("打开文件失败:", err.Error()) + } + strings.LastIndex(path, ".") + lastIndex := strings.LastIndex(path, ".") + extend := path[lastIndex+1:] + extendUpper := strings.ToUpper(extend) + switch { + case strings.Contains(extendUpper, "JPEG") || strings.Contains(extendUpper, "JPG"): + _, err = jpeg.Decode(f) + case strings.Contains(extendUpper, "JPEG"): + _, err = png.Decode(f) + case strings.Contains(extendUpper, "GIF"): + _, err = gif.Decode(f) + } + + if err != nil { + log.Println("校验文件类型失败:", err.Error()) + return "", err + } + return "", nil +} diff --git a/util/imageCheck_test.go b/util/imageCheck_test.go new file mode 100644 index 0000000..0391f60 --- /dev/null +++ b/util/imageCheck_test.go @@ -0,0 +1,30 @@ +package util + +import ( + "fmt" + "strings" + "testing" +) + +/* +* +@author: sre +@date: 2022/8/21 0021 +@desc: todo +* +*/ +func TestCheckImageFile(t *testing.T) { + path := "D:\\Desktop\\icon\\avatar.png" + _, err := CheckImageFile(path) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("success") +} +func TestIndex(t *testing.T) { + path := "343455.sdsdsdsd.jpg" + lastIndex := strings.LastIndex(path, ".") + extend := path[lastIndex+1:] + fmt.Println(extend) +}