不需要客户端再主动调用RegisterDataResource了,RegisterDataResource改为空操作
This commit is contained in:
@@ -2,8 +2,8 @@ package datares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
@@ -17,12 +17,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
qiniuTokenExpires = 300 // 七牛TOKEN有效时间,5分钟
|
||||
dataResCacheTimeout = 24 * time.Hour
|
||||
)
|
||||
|
||||
var (
|
||||
dataResCache jxutils.SyncMapWithTimeout
|
||||
qiniuTokenExpires = 300 // 七牛TOKEN有效时间,5分钟
|
||||
)
|
||||
|
||||
type UploadResTokenInfo struct {
|
||||
@@ -50,7 +45,6 @@ func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTok
|
||||
Hit: imgURL != "",
|
||||
Img: imgURL,
|
||||
}
|
||||
dataResCache.StoreWithTimeout(imgURL, hashCode, dataResCacheTimeout)
|
||||
return upTokenInfo, err
|
||||
}
|
||||
|
||||
@@ -75,7 +69,9 @@ func getMimeTypeFromURL(resourceURL string) (mimeType string) {
|
||||
return mimeType
|
||||
}
|
||||
|
||||
func RegisterDataResource(ctx *jxcontext.Context, name, resourceURL, mimeType, hashCode string, resBinary []byte, imgType int, isAsync bool) (dataRes *model.DataResource, err error) {
|
||||
// 此函数要求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)
|
||||
}
|
||||
@@ -106,17 +102,17 @@ func RegisterDataResource(ctx *jxcontext.Context, name, resourceURL, mimeType, h
|
||||
}
|
||||
if imgType > 0 {
|
||||
// 忽略上传错误
|
||||
UploadImage2Vendors(ctx, nil, dataRes, resBinary, isAsync)
|
||||
UploadImage2Vendors(ctx, nil, dataRes, resBinary, isAsyncUpload2Vendor)
|
||||
}
|
||||
return dataRes, err
|
||||
}
|
||||
|
||||
func TryRegisterDataResource(ctx *jxcontext.Context, name, resourceURL string, isAllowDownLoad bool, imgType int, isAsync bool) (dataRes *model.DataResource, err error) {
|
||||
func TryRegisterDataResource(ctx *jxcontext.Context, name, resourceURL string, imgType int, isAsyncUpload2Vendor bool) (dataRes *model.DataResource, err error) {
|
||||
globals.SugarLogger.Debugf("TryRegisterDataResource, name:%s, resourceURL:%s, imgType:%d, isAsyncUpload2Vendor;%t", name, resourceURL, imgType, isAsyncUpload2Vendor)
|
||||
dataRes = &model.DataResource{
|
||||
MainURL: resourceURL,
|
||||
}
|
||||
db := dao.GetDB()
|
||||
err = dao.GetEntity(db, dataRes, "MainURL")
|
||||
err = dao.GetEntity(dao.GetDB(), dataRes, "MainURL")
|
||||
if err != nil {
|
||||
if !dao.IsNoRowsError(err) {
|
||||
return nil, err
|
||||
@@ -124,20 +120,11 @@ func TryRegisterDataResource(ctx *jxcontext.Context, name, resourceURL string, i
|
||||
} else {
|
||||
return dataRes, nil
|
||||
}
|
||||
value, ok := dataResCache.Load(resourceURL)
|
||||
var hashCode string
|
||||
var resBinary []byte
|
||||
if !ok {
|
||||
if !isAllowDownLoad {
|
||||
return nil, nil
|
||||
}
|
||||
if resBinary, hashCode, err = jxutils.DownloadFileByURL(resourceURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
hashCode = value.(string)
|
||||
resBinary, hashCode, err := jxutils.DownloadFileByURL(resourceURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return RegisterDataResource(ctx, name, resourceURL, getMimeTypeFromURL(resourceURL), hashCode, resBinary, imgType, isAsync)
|
||||
return RegisterDataResource(ctx, name, resourceURL, http.DetectContentType(resBinary), hashCode, resBinary, imgType, isAsyncUpload2Vendor)
|
||||
}
|
||||
|
||||
func GetDataResource(ctx *jxcontext.Context, hashCode string) (resourceURL string, err error) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals/api2"
|
||||
"git.rosy.net.cn/jx-callback/globals/testinit"
|
||||
|
||||
@@ -36,7 +37,7 @@ func TestGetDataResource(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTryRegisterDataResource(t *testing.T) {
|
||||
dataRes, err := TryRegisterDataResource(jxcontext.AdminCtx, "test", "http://image.jxc4.com/image/8552261fb928be10f6406b13dc29570e.jpg", true, true, false)
|
||||
dataRes, err := TryRegisterDataResource(jxcontext.AdminCtx, "test", "http://image.jxc4.com/image/37db6b94925e669e0a7b6844ba7ce160.jpg", model.ImgTypeMain, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user