From b11e2e5d1e3f7705ce9668f8dbdd88063be3228b Mon Sep 17 00:00:00 2001 From: gazebo Date: Mon, 30 Sep 2019 17:06:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E4=B8=8A=E4=BC=A0=E7=9A=84?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=B0=BA=E5=AF=B8=E6=98=AF=E5=90=A6=E7=AC=A6?= =?UTF-8?q?=E5=90=88=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxutils/datares/datares.go | 55 ++++++++++++++++------------- business/model/common.go | 23 ++++++++---- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/business/jxutils/datares/datares.go b/business/jxutils/datares/datares.go index e1c36bfd5..7949b79cf 100644 --- a/business/jxutils/datares/datares.go +++ b/business/jxutils/datares/datares.go @@ -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, diff --git a/business/model/common.go b/business/model/common.go index 661f24350..c539770f3 100644 --- a/business/model/common.go +++ b/business/model/common.go @@ -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, ",") +}