检查上传的图片尺寸是否符合要求
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package datares
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
@@ -18,6 +21,9 @@ import (
|
||||
|
||||
const (
|
||||
qiniuTokenExpires = 300 // 七牛TOKEN有效时间,5分钟
|
||||
|
||||
MainImgWidth = 800
|
||||
MainImgHeight = 800
|
||||
)
|
||||
|
||||
type UploadResTokenInfo struct {
|
||||
@@ -28,6 +34,19 @@ type UploadResTokenInfo struct {
|
||||
Img string `json:"img,omitempty"`
|
||||
}
|
||||
|
||||
func Binary2Image(binaryData []byte, mimeType string) (img image.Image, outMimeType string, err error) {
|
||||
if mimeType == "" {
|
||||
mimeType = http.DetectContentType(binaryData)
|
||||
}
|
||||
switch mimeType {
|
||||
case model.MimeTypeJpeg:
|
||||
img, err = jpeg.Decode(bytes.NewReader(binaryData))
|
||||
case model.MimeTypePng:
|
||||
img, err = png.Decode(bytes.NewReader(binaryData))
|
||||
}
|
||||
return img, mimeType, err
|
||||
}
|
||||
|
||||
func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTokenInfo *UploadResTokenInfo, err error) {
|
||||
imgURL := ""
|
||||
if hashCode != "" {
|
||||
@@ -48,32 +67,20 @@ func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTok
|
||||
return upTokenInfo, err
|
||||
}
|
||||
|
||||
func suffix2MimeType(suffix string) (mimeType string) {
|
||||
suffix = strings.Trim(suffix, ". ")
|
||||
for k, v := range model.ValidMimeTypes {
|
||||
for _, v2 := range v {
|
||||
if v2 == suffix {
|
||||
mimeType = k
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return mimeType
|
||||
}
|
||||
|
||||
func getMimeTypeFromURL(resourceURL string) (mimeType string) {
|
||||
index := strings.LastIndex(resourceURL, ".")
|
||||
if index >= 0 {
|
||||
mimeType = suffix2MimeType(resourceURL[index:])
|
||||
}
|
||||
return mimeType
|
||||
}
|
||||
|
||||
// 此函数要求resBinary不能空,mimeType与hashCode必须是正确的
|
||||
func RegisterDataResource(ctx *jxcontext.Context, name, resourceURL, mimeType, hashCode string, resBinary []byte, imgType int, isAsyncUpload2Vendor bool) (dataRes *model.DataResource, err error) {
|
||||
globals.SugarLogger.Debugf("RegisterDataResource, name:%s, resourceURL:%s, mimeType:%s, hashCode:%s, imgType:%d, isAsyncUpload2Vendor;%t", name, resourceURL, mimeType, hashCode, imgType, isAsyncUpload2Vendor)
|
||||
if model.ValidMimeTypes[mimeType] == nil {
|
||||
return nil, fmt.Errorf("MIME type:%s非法", mimeType)
|
||||
if model.ValidMimeTypes[mimeType] == 0 {
|
||||
return nil, fmt.Errorf("MIME type:%s非法,当前只支持:%s", mimeType, model.GetValidMimeTypeDesc())
|
||||
}
|
||||
img, _, err := Binary2Image(resBinary, mimeType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if imgType == model.ImgTypeMain {
|
||||
if img.Bounds().Dx() != MainImgWidth || img.Bounds().Dy() != MainImgHeight {
|
||||
return nil, fmt.Errorf("图片大小:%dx%d非法,要求必须:%dx%d", img.Bounds().Dx(), img.Bounds().Dy(), MainImgWidth, MainImgHeight)
|
||||
}
|
||||
}
|
||||
dataRes = &model.DataResource{
|
||||
Name: name,
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
package model
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
ImgTypeLocal = 0 // 京西自己用的,不需要上传至平台
|
||||
ImgTypeMain = 1 // 商品主图
|
||||
ImgTypeDesc = 2 // 商品描述详情
|
||||
|
||||
MimeTypeJpeg = "image/jpeg"
|
||||
MimeTypePng = "image/png"
|
||||
)
|
||||
|
||||
var (
|
||||
ValidMimeTypes = map[string][]string{
|
||||
"image/jpeg": []string{"jpeg", "jpg"},
|
||||
"image/png": []string{"png"},
|
||||
// "image/gif": []string{"gif"}, // 美团不支持GIF
|
||||
|
||||
"video/mpeg": []string{"mpeg", "mpg"},
|
||||
"video/mp4": []string{"mp4", "m4v"},
|
||||
ValidMimeTypes = map[string]int{
|
||||
MimeTypeJpeg: 1,
|
||||
MimeTypePng: 1,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -33,3 +34,11 @@ type DataResource struct {
|
||||
|
||||
Remark string `orm:"size(1024)" json:"remark"`
|
||||
}
|
||||
|
||||
func GetValidMimeTypeDesc() (desc string) {
|
||||
strList := []string{}
|
||||
for k := range ValidMimeTypes {
|
||||
strList = append(strList, k)
|
||||
}
|
||||
return strings.Join(strList, ",")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user