parent
b1c9743755
commit
b07dfad184
@ -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
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
Loading…
Reference in new issue