检查上传的图片尺寸是否符合要求
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package datares
|
package datares
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
"image/png"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||||
@@ -18,6 +21,9 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
qiniuTokenExpires = 300 // 七牛TOKEN有效时间,5分钟
|
qiniuTokenExpires = 300 // 七牛TOKEN有效时间,5分钟
|
||||||
|
|
||||||
|
MainImgWidth = 800
|
||||||
|
MainImgHeight = 800
|
||||||
)
|
)
|
||||||
|
|
||||||
type UploadResTokenInfo struct {
|
type UploadResTokenInfo struct {
|
||||||
@@ -28,6 +34,19 @@ type UploadResTokenInfo struct {
|
|||||||
Img string `json:"img,omitempty"`
|
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) {
|
func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTokenInfo *UploadResTokenInfo, err error) {
|
||||||
imgURL := ""
|
imgURL := ""
|
||||||
if hashCode != "" {
|
if hashCode != "" {
|
||||||
@@ -48,32 +67,20 @@ func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTok
|
|||||||
return upTokenInfo, err
|
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必须是正确的
|
// 此函数要求resBinary不能空,mimeType与hashCode必须是正确的
|
||||||
func RegisterDataResource(ctx *jxcontext.Context, name, resourceURL, mimeType, hashCode string, resBinary []byte, imgType int, isAsyncUpload2Vendor bool) (dataRes *model.DataResource, err error) {
|
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)
|
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 {
|
if model.ValidMimeTypes[mimeType] == 0 {
|
||||||
return nil, fmt.Errorf("MIME type:%s非法", mimeType)
|
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{
|
dataRes = &model.DataResource{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ImgTypeLocal = 0 // 京西自己用的,不需要上传至平台
|
ImgTypeLocal = 0 // 京西自己用的,不需要上传至平台
|
||||||
ImgTypeMain = 1 // 商品主图
|
ImgTypeMain = 1 // 商品主图
|
||||||
ImgTypeDesc = 2 // 商品描述详情
|
ImgTypeDesc = 2 // 商品描述详情
|
||||||
|
|
||||||
|
MimeTypeJpeg = "image/jpeg"
|
||||||
|
MimeTypePng = "image/png"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ValidMimeTypes = map[string][]string{
|
ValidMimeTypes = map[string]int{
|
||||||
"image/jpeg": []string{"jpeg", "jpg"},
|
MimeTypeJpeg: 1,
|
||||||
"image/png": []string{"png"},
|
MimeTypePng: 1,
|
||||||
// "image/gif": []string{"gif"}, // 美团不支持GIF
|
|
||||||
|
|
||||||
"video/mpeg": []string{"mpeg", "mpg"},
|
|
||||||
"video/mp4": []string{"mp4", "m4v"},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,3 +34,11 @@ type DataResource struct {
|
|||||||
|
|
||||||
Remark string `orm:"size(1024)" json:"remark"`
|
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