fusenapi/service/repositories/image_handle.go

478 lines
14 KiB
Go
Raw Normal View History

2023-08-14 09:56:06 +00:00
package repositories
import (
"context"
"encoding/json"
"errors"
2023-10-10 06:58:36 +00:00
"fmt"
2023-08-14 09:56:06 +00:00
"fusenapi/constants"
"fusenapi/model/gmodel"
2023-10-18 03:01:40 +00:00
"fusenapi/utils/auth"
2023-08-14 09:56:06 +00:00
"fusenapi/utils/curl"
"fusenapi/utils/file"
"fusenapi/utils/hash"
2023-10-08 02:22:00 +00:00
"fusenapi/utils/s3url_to_s3id"
2023-10-11 07:10:38 +00:00
"strconv"
2023-08-24 07:35:31 +00:00
"time"
2023-08-14 09:56:06 +00:00
"github.com/aws/aws-sdk-go/aws/session"
2023-08-24 07:35:31 +00:00
"github.com/zeromicro/go-zero/core/logc"
2023-08-14 09:56:06 +00:00
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
2023-10-10 06:58:36 +00:00
var globalBLMServiceIndex int
2023-10-10 09:17:28 +00:00
func NewImageHandle(gormDB *gorm.DB, bLMServiceUrls []string, awsSession *session.Session) ImageHandle {
2023-08-14 09:56:06 +00:00
return &defaultImageHandle{
2023-10-10 09:17:28 +00:00
MysqlConn: gormDB,
BLMServiceUrls: bLMServiceUrls,
AwsSession: awsSession,
2023-08-14 09:56:06 +00:00
}
}
type (
defaultImageHandle struct {
2023-10-10 09:17:28 +00:00
MysqlConn *gorm.DB
BLMServiceUrls []string
AwsSession *session.Session
2023-08-14 09:56:06 +00:00
}
ImageHandle = interface {
2023-08-23 03:09:14 +00:00
// logo信息
LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error)
2023-08-14 09:56:06 +00:00
// logo合图
LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error)
2023-08-18 08:47:22 +00:00
// logo裁剪
LogoStandard(ctx context.Context, in *LogoStandardReq) (*LogoStandardRes, error)
2023-08-14 09:56:06 +00:00
}
)
2023-08-29 10:00:37 +00:00
/* 获取logo最新信息 */
type (
LogoInfoReq struct {
UserId int64 `json:"user_id"`
GuestId int64 `json:"guest_id"`
}
LogoInfoRes struct {
2023-09-18 09:53:05 +00:00
Metadata *string `json:"metadata"`
LogoUrl *string `json:"logo_url"`
UserInfoMetadata *string `json:"user_info_metadata"`
2023-08-29 10:00:37 +00:00
}
)
/* 获取logo最新信息 */
2023-08-23 03:09:14 +00:00
/* logo信息 */
type (
LogoInfoSetReq struct {
2023-10-18 06:01:46 +00:00
LogoUrl string `json:"logo_url"`
Version string `json:"version"`
Debug *auth.Debug `json:"debug"`
2023-08-23 03:09:14 +00:00
}
2023-08-23 06:22:36 +00:00
LogoInfoSetRes struct {
Res string `json:"res"`
}
2023-08-23 03:09:14 +00:00
)
func (l *defaultImageHandle) LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error) {
2023-10-10 06:58:36 +00:00
fmt.Println("算法请求轮训下标:", globalBLMServiceIndex)
2023-10-10 09:17:28 +00:00
var bLMServicePort = l.BLMServiceUrls[globalBLMServiceIndex]
if len(l.BLMServiceUrls) == (globalBLMServiceIndex + 1) {
2023-10-10 06:58:36 +00:00
globalBLMServiceIndex = 0
} else {
globalBLMServiceIndex = globalBLMServiceIndex + 1
}
2023-08-23 03:09:14 +00:00
var resultBLM constants.BLMServiceUrlResult
2023-10-30 09:28:05 +00:00
postMap := make(map[string]string, 3)
2023-08-23 03:09:14 +00:00
postMap["logo_url"] = in.LogoUrl
2023-10-07 03:32:11 +00:00
postMap["version"] = in.Version
2023-08-23 03:09:14 +00:00
2023-10-30 09:28:05 +00:00
if in.Debug != nil && in.Debug.IsAllTemplateTag == 1 {
postMap["is_all_template"] = "1"
} else {
postMap["is_all_template"] = "0"
}
2023-10-18 06:01:46 +00:00
2023-08-24 07:35:31 +00:00
logc.Infof(ctx, "算法请求--LOGO基础信息--开始时间:%v", time.Now().UTC())
2023-08-23 06:22:36 +00:00
err := curl.NewClient(ctx, &curl.Config{
2023-10-10 07:58:37 +00:00
BaseUrl: bLMServicePort,
2023-08-23 07:10:22 +00:00
Url: constants.BLMServiceUrlLogoFeatureExtraction,
2023-08-23 06:22:36 +00:00
}).PostJson(postMap, &resultBLM)
2023-08-24 07:35:31 +00:00
logc.Infof(ctx, "算法请求--LOGO基础信息--结束时间:%v", time.Now().UTC())
2023-08-23 03:09:14 +00:00
if err != nil {
logx.Error(err)
return nil, err
}
2023-08-23 06:22:36 +00:00
if resultBLM.Code != "200" {
err = errors.New(resultBLM.Msg)
logx.Error(err)
return nil, err
}
2023-08-23 07:10:22 +00:00
return &LogoInfoSetRes{
Res: resultBLM.Data.(string),
}, nil
2023-08-23 03:09:14 +00:00
}
/* logo信息 */
2023-08-14 09:56:06 +00:00
/* logo合图 */
type (
LogoCombineReq struct {
2023-09-19 07:59:44 +00:00
UserId int64 `json:"user_id"`
GuestId int64 `json:"guest_id"`
ProductTemplateV2Info *gmodel.FsProductTemplateV2 `json:"product_template_v2_info"`
ProductTemplateTagGroups interface{} `json:"product_template_tag_groups"`
TemplateTag string `json:"template_tag"`
Website string `json:"website"` // 合图参数
Slogan string `json:"slogan"` // 合图参数
Address string `json:"address"` // 合图参数
Phone string `json:"phone"` // 合图参数
Qrcode string `json:"qrcode"` // 合图参数
LogoUrl string `json:"logo_url"` // 合图参数
2023-10-08 07:48:22 +00:00
Resolution string `json:"resolution"` // 合图参数
2023-10-18 03:01:40 +00:00
TemplateTagColor TemplateTagColor `json:"template_tag_color"` // 合图颜色
Debug *auth.Debug `json:"debug"`
2023-08-14 09:56:06 +00:00
}
LogoCombineRes struct {
2023-10-20 06:36:16 +00:00
ResourceId string `json:"resource_id"`
ResourceUrl *string `json:"resource_url"`
Metadata *string `json:"metadata"`
DebugData *auth.DebugData `json:"debug_data"`
2023-08-14 09:56:06 +00:00
}
)
2023-09-19 06:39:33 +00:00
type TemplateTagColor struct {
Color [][]string `json:"color"`
Index int `json:"index"`
}
2023-08-14 09:56:06 +00:00
func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) {
2023-10-18 03:48:32 +00:00
var resp = &LogoCombineRes{}
2023-10-08 02:22:00 +00:00
logoResourceId := s3url_to_s3id.GetS3ResourceIdFormUrl(in.LogoUrl)
if logoResourceId == "" {
return nil, errors.New("invalid logo url")
2023-09-21 03:35:00 +00:00
}
2023-10-30 07:30:31 +00:00
userMaterialModel := gmodel.NewFsUserMaterialModel(l.MysqlConn.Debug())
resLogoInfo, err := userMaterialModel.FindOneByLogoResourceId(ctx, logoResourceId)
2023-08-18 09:16:49 +00:00
if err != nil {
2023-08-29 10:00:37 +00:00
logx.Error(err)
2023-10-30 07:11:36 +00:00
return nil, fmt.Errorf("logo find fial logoResourceId: %v, err: %v", logoResourceId, err)
2023-08-18 09:16:49 +00:00
}
2023-08-14 09:56:06 +00:00
// 根据hash 查询数据资源
2023-08-16 08:33:25 +00:00
var hashKeyData = *in
hashKeyData.GuestId = 0
hashKeyData.UserId = 0
2023-09-21 03:35:00 +00:00
hashKeyData.LogoUrl = in.LogoUrl
2023-08-18 11:37:11 +00:00
var hashKeyDataMap map[string]interface{}
hashKeyDataB, _ := json.Marshal(hashKeyData)
json.Unmarshal(hashKeyDataB, &hashKeyDataMap)
var resourceId string = hash.JsonHashKey(hashKeyDataMap)
2023-08-15 02:58:24 +00:00
2023-08-14 09:56:06 +00:00
resourceModel := gmodel.NewFsResourceModel(l.MysqlConn)
resourceInfo, err := resourceModel.FindOneById(ctx, resourceId)
if err == nil && resourceInfo.ResourceId != "" {
2023-10-18 03:48:32 +00:00
if in.Debug == nil || (in.Debug != nil && in.Debug.IsCache == 1) {
return &LogoCombineRes{
2023-10-20 06:36:16 +00:00
ResourceId: resourceId,
ResourceUrl: resourceInfo.ResourceUrl,
2023-10-18 03:48:32 +00:00
}, nil
}
2023-08-14 09:56:06 +00:00
} else {
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return nil, err
}
}
}
var groupOptions map[string]interface{}
var materialList []interface{}
2023-09-19 07:09:48 +00:00
if in.ProductTemplateV2Info.TemplateInfo != nil {
2023-08-14 09:56:06 +00:00
var templateInfo map[string]interface{}
2023-09-19 07:09:48 +00:00
err = json.Unmarshal([]byte(*in.ProductTemplateV2Info.TemplateInfo), &templateInfo)
2023-08-14 09:56:06 +00:00
if err != nil {
logx.Error(err)
return nil, err
}
2023-09-12 09:54:31 +00:00
mapMaterialList, existMaterialList := templateInfo["materialList"]
if !existMaterialList {
2023-09-12 09:49:40 +00:00
err = errors.New("materialList is null")
logc.Errorf(ctx, "materialList err%v", err)
return nil, err
}
2023-09-12 09:54:31 +00:00
materialList = mapMaterialList.([]interface{})
mapGroupOptions, existGroupOptions := templateInfo["groupOptions"]
if !existGroupOptions {
2023-09-12 09:49:40 +00:00
err = errors.New("groupOptions is null")
logc.Errorf(ctx, "groupOptions err%v", err)
return nil, err
}
2023-09-12 09:54:31 +00:00
groupOptions = mapGroupOptions.(map[string]interface{})
2023-08-14 09:56:06 +00:00
}
var moduleDataMap = make(map[string]interface{}, 4)
2023-09-19 07:09:48 +00:00
moduleDataMap["id"] = in.ProductTemplateV2Info.Id
moduleDataMap["material"] = in.ProductTemplateV2Info.MaterialImg
2023-08-14 09:56:06 +00:00
moduleDataMap["groupOptions"] = groupOptions
moduleDataMap["materialList"] = materialList
2023-10-31 04:26:44 +00:00
var combineParam = make(map[string]interface{}, 8)
2023-10-25 06:43:49 +00:00
json.Unmarshal(*resLogoInfo.Metadata, &combineParam)
2023-10-08 07:48:22 +00:00
combineParam["resolution"] = in.Resolution
2023-08-15 02:58:24 +00:00
combineParam["template_tagid"] = in.TemplateTag
combineParam["website"] = in.Website
combineParam["slogan"] = in.Slogan
combineParam["phone"] = in.Phone
combineParam["address"] = in.Address
2023-08-15 03:20:09 +00:00
combineParam["qrcode"] = in.Qrcode
2023-09-19 06:39:33 +00:00
combineParam["template_tag_selected"] = map[string]interface{}{
"template_tag": in.TemplateTag,
"color": in.TemplateTagColor.Color,
"index": in.TemplateTagColor.Index,
}
2023-10-07 03:32:11 +00:00
var postMap = make(map[string]interface{}, 3)
2023-08-14 09:56:06 +00:00
postMap["module_data"] = moduleDataMap
2023-09-19 07:59:44 +00:00
postMap["tag_data"] = in.ProductTemplateTagGroups
2023-08-14 09:56:06 +00:00
postMap["param_data"] = combineParam
2023-10-10 07:10:15 +00:00
fmt.Println("算法请求轮训下标:", globalBLMServiceIndex)
2023-10-10 09:17:28 +00:00
var bLMServicePort = l.BLMServiceUrls[globalBLMServiceIndex]
if len(l.BLMServiceUrls) == (globalBLMServiceIndex + 1) {
2023-10-10 07:10:15 +00:00
globalBLMServiceIndex = 0
} else {
globalBLMServiceIndex = globalBLMServiceIndex + 1
}
2023-10-25 06:49:08 +00:00
2023-08-25 02:37:07 +00:00
logc.Infof(ctx, "合图--算法请求--合图--开始时间:%v", time.Now().UTC())
2023-08-25 02:48:54 +00:00
var startTimeLogoCombine = time.Now().UnixMilli() //合图--处理--开始时间
2023-08-25 02:37:07 +00:00
2023-08-23 06:22:36 +00:00
var resultBLM constants.BLMServiceUrlResult
err = curl.NewClient(ctx, &curl.Config{
2023-10-10 07:58:37 +00:00
BaseUrl: bLMServicePort,
2023-09-22 04:05:05 +00:00
Url: constants.BLMServiceUrlLogoCombine,
2023-09-22 07:12:44 +00:00
RequireTimeout: time.Second * 15,
2023-08-23 06:22:36 +00:00
}).PostJson(postMap, &resultBLM)
2023-08-25 02:37:07 +00:00
2023-10-10 06:19:11 +00:00
//logc.Infof(ctx, "合图--算法请求--合图--结束时间:%v", time.Now().UTC())
2023-08-25 02:48:54 +00:00
endTimeLogoCombine := time.Now().UnixMilli() //合图--处理--开始时间
diffTimeLogoCombine := endTimeLogoCombine - startTimeLogoCombine //合图--处理--中间差
logc.Infof(ctx, "合图--算法请求--合图--业务耗时:%d", diffTimeLogoCombine)
2023-08-14 09:56:06 +00:00
if err != nil {
logx.Error(err)
return nil, err
}
2023-08-22 02:02:50 +00:00
2023-08-23 06:22:36 +00:00
if resultBLM.Code != "200" {
err = errors.New(resultBLM.Msg)
2023-08-15 03:59:35 +00:00
logx.Error(err)
2023-08-14 09:56:06 +00:00
return nil, err
}
2023-08-23 06:22:36 +00:00
var resultStr string = resultBLM.Data.(string)
2023-08-14 09:56:06 +00:00
var resultData map[string]interface{}
2023-08-15 10:35:16 +00:00
err = json.Unmarshal([]byte(resultStr), &resultData)
if err != nil || resultData == nil {
2023-08-14 09:56:06 +00:00
logx.Error(err)
return nil, err
}
2023-08-18 10:41:51 +00:00
2023-08-17 09:20:43 +00:00
var fileBase = resultData["result"].(string)
2023-10-11 07:10:38 +00:00
var spendTime = resultData["spend_time"].(string)
spendTimeInt, _ := strconv.Atoi(spendTime)
diffTimeLogoCombine = int64(spendTimeInt)
logc.Infof(ctx, "合图--算法请求--合图--业务耗时(自身):%d", diffTimeLogoCombine)
2023-08-14 09:56:06 +00:00
// 上传文件
var upload = file.Upload{
Ctx: ctx,
MysqlConn: l.MysqlConn,
AwsSession: l.AwsSession,
}
2023-08-25 02:37:07 +00:00
logc.Infof(ctx, "合图--上传文件--开始时间:%v", time.Now().UTC())
2023-08-25 02:48:54 +00:00
var startTimeUploadFile = time.Now().UnixMilli() //合图--上传--开始时间
2023-08-25 02:37:07 +00:00
2023-08-14 09:56:06 +00:00
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
2023-08-16 09:33:34 +00:00
Source: "combine-image",
2023-08-14 09:56:06 +00:00
FileHash: resourceId,
2023-08-17 09:20:43 +00:00
FileData: fileBase,
2023-08-14 09:56:06 +00:00
UploadBucket: 1,
ApiType: 2,
UserId: in.UserId,
GuestId: in.GuestId,
})
2023-08-25 02:49:52 +00:00
logc.Infof(ctx, "合图--上传文件--结束时间:%v", time.Now().UTC())
2023-08-25 02:48:54 +00:00
endTimeUploadFile := time.Now().UnixMilli() //合图--处理--开始时间
diffTimeUploadFile := endTimeUploadFile - startTimeUploadFile //合图--处理--中间差
2023-08-25 02:37:07 +00:00
2023-08-25 02:48:54 +00:00
logc.Infof(ctx, "合图--上传文件--业务耗时:%d", diffTimeUploadFile)
2023-08-14 09:56:06 +00:00
if err != nil {
logx.Error(err)
return nil, err
}
2023-08-25 02:37:07 +00:00
2023-10-18 03:48:32 +00:00
resp.ResourceId = uploadRes.ResourceId
resp.ResourceUrl = &uploadRes.ResourceUrl
if in.Debug != nil {
resp.DebugData = &auth.DebugData{
DiffTimeLogoCombine: diffTimeLogoCombine,
DiffTimeUploadFile: diffTimeUploadFile,
}
}
return resp, nil
2023-08-14 09:56:06 +00:00
}
/* logo合图 */
2023-08-18 08:47:22 +00:00
type (
LogoStandardReq struct {
IsRemoveBg string `json:"is_remove_bg"`
LogoFile string `json:"logo_file"`
Width string `json:"width"`
Height string `json:"height"`
2023-08-23 03:09:14 +00:00
Proportion string `json:"proportion"`
2023-08-18 08:47:22 +00:00
}
2023-08-18 09:16:49 +00:00
LogoStandardRes struct {
ResourceId string
ResourceUrl string
IsmaxProportion bool
ImgColor []string
}
2023-08-21 06:39:03 +00:00
LogoStandardMetaData struct {
Param LogoStandardReq `json:"param"`
Result LogoStandardRes `json:"result"`
}
2023-08-18 08:47:22 +00:00
)
/* 图片裁剪 */
func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardReq) (*LogoStandardRes, error) {
2023-08-21 06:39:03 +00:00
var ismaxProportion bool
var imgColor []string
var logoStandardMetaData LogoStandardMetaData
2023-08-21 02:09:26 +00:00
var hashKeyDataMap map[string]interface{}
hashKeyDataB, _ := json.Marshal(in)
json.Unmarshal(hashKeyDataB, &hashKeyDataMap)
var resourceId string = hash.JsonHashKey(hashKeyDataMap)
2023-08-21 06:39:03 +00:00
resourceModel := gmodel.NewFsResourceModel(l.MysqlConn)
resourceInfo, err := resourceModel.FindOneById(ctx, resourceId)
if err == nil && resourceInfo.ResourceId != "" {
if resourceInfo.Metadata != nil {
json.Unmarshal([]byte(*resourceInfo.Metadata), &logoStandardMetaData)
}
// 取出参数
2023-08-21 06:59:21 +00:00
ismaxProportion = logoStandardMetaData.Result.IsmaxProportion
imgColor = logoStandardMetaData.Result.ImgColor
2023-08-21 06:39:03 +00:00
return &LogoStandardRes{
ResourceId: resourceInfo.ResourceId,
ResourceUrl: *resourceInfo.ResourceUrl,
IsmaxProportion: ismaxProportion,
ImgColor: imgColor,
}, nil
} else {
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return nil, err
}
}
}
2023-08-23 03:09:14 +00:00
var resultBLM constants.BLMServiceUrlResult
2023-08-18 09:51:51 +00:00
var postMap = make(map[string]interface{}, 5)
2023-08-18 09:16:49 +00:00
postMap["is_remove_bg"] = in.IsRemoveBg
postMap["logo_file"] = in.LogoFile
postMap["width"] = in.Width
postMap["height"] = in.Height
postMap["proportion"] = in.Proportion
2023-10-10 07:10:15 +00:00
fmt.Println("算法请求轮训下标:", globalBLMServiceIndex)
2023-10-10 09:17:28 +00:00
var bLMServicePort = l.BLMServiceUrls[globalBLMServiceIndex]
if len(l.BLMServiceUrls) == (globalBLMServiceIndex + 1) {
2023-10-10 07:10:15 +00:00
globalBLMServiceIndex = 0
} else {
globalBLMServiceIndex = globalBLMServiceIndex + 1
}
2023-08-24 07:35:31 +00:00
logc.Infof(ctx, "算法请求--去背景--开始时间:%v", time.Now().UTC())
2023-08-23 03:09:14 +00:00
err = curl.NewClient(ctx, &curl.Config{
2023-10-10 07:58:37 +00:00
BaseUrl: bLMServicePort,
2023-08-23 03:09:14 +00:00
Url: constants.BLMServiceUrlLogoRemovebg,
}).PostJson(postMap, &resultBLM)
2023-08-24 07:35:31 +00:00
logc.Infof(ctx, "算法请求--去背景--结束时间:%v", time.Now().UTC())
2023-08-18 09:16:49 +00:00
if err != nil {
logx.Error(err)
return nil, err
}
2023-08-23 06:22:36 +00:00
if resultBLM.Code != "200" {
err = errors.New(resultBLM.Msg)
logx.Error(err)
return nil, err
}
2023-08-23 03:09:14 +00:00
var resultStr string = resultBLM.Data.(string)
2023-08-18 09:16:49 +00:00
var resultData map[string]interface{}
err = json.Unmarshal([]byte(resultStr), &resultData)
if err != nil || resultData == nil {
logx.Error(err)
return nil, err
}
2023-08-21 06:39:03 +00:00
2023-08-18 09:38:12 +00:00
var fileBase = resultData["nobg_url"].(string)
2023-08-21 06:39:03 +00:00
ismaxProportion = resultData["ismax_proportion"].(bool)
2023-08-18 09:38:12 +00:00
for _, v := range resultData["img_color"].([]interface{}) {
imgColor = append(imgColor, v.(string))
}
2023-08-21 06:59:21 +00:00
var logoStandardRes LogoStandardRes
logoStandardRes.IsmaxProportion = ismaxProportion
logoStandardRes.ImgColor = imgColor
logoStandardMetaData.Param = *in
logoStandardMetaData.Result = logoStandardRes
metadataB, err := json.Marshal(logoStandardMetaData)
if err != nil {
logx.Error(err)
return nil, err
}
var metadata = string(metadataB)
2023-08-18 09:38:12 +00:00
// 上传文件
var upload = file.Upload{
Ctx: ctx,
MysqlConn: l.MysqlConn,
AwsSession: l.AwsSession,
}
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
Source: "combine-removebg",
FileHash: resourceId,
FileData: fileBase,
UploadBucket: 1,
ApiType: 2,
2023-08-21 06:59:21 +00:00
Metadata: metadata,
2023-08-18 09:38:12 +00:00
})
if err != nil {
logx.Error(err)
return nil, err
}
return &LogoStandardRes{
ResourceId: uploadRes.ResourceId,
ResourceUrl: uploadRes.ResourceUrl,
IsmaxProportion: ismaxProportion,
ImgColor: imgColor,
}, nil
2023-08-18 08:47:22 +00:00
}
/* 图片裁剪 */