feat:合图调整

This commit is contained in:
momo 2023-08-31 11:56:15 +08:00
parent 2853d8599f
commit ad7a8858a2
5 changed files with 165 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import (
type Repositories struct {
ImageHandle repositories.ImageHandle
NewResource repositories.Resource
}
type NewAllRepositorieData struct {
@ -20,5 +21,6 @@ type NewAllRepositorieData struct {
func NewAllRepositories(newData *NewAllRepositorieData) *Repositories {
return &Repositories{
ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession),
NewResource: repositories.NewResource(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession),
}
}

View File

@ -18,8 +18,8 @@ AWS:
Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm
Token:
BLMService:
Url: "http://18.119.109.254:8999"
# Url: "http://192.168.1.9:8999"
# Url: "http://18.119.109.254:8999"
Url: "http://192.168.1.9:8999"
LogoCombine:
#Url: "http://192.168.1.7:8999/LogoCombine"
Url: "http://18.119.109.254:8999/LogoCombine"

View File

@ -3,7 +3,9 @@ package logic
import (
"bytes"
"encoding/json"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/file"
@ -96,6 +98,7 @@ func (l *LogoResizeLogic) LogoResize(req *types.LogoResizeReq, userinfo *auth.Us
hashKeyDataB, _ := json.Marshal(req)
json.Unmarshal(hashKeyDataB, &hashKeyDataMap)
var resourceId string = hash.JsonHashKey(hashKeyDataMap)
fmt.Println(resourceId)
// 上传文件
var upload = file.Upload{
@ -116,10 +119,28 @@ func (l *LogoResizeLogic) LogoResize(req *types.LogoResizeReq, userinfo *auth.Us
logx.Errorf("upload UploadFileByByte err: %v", err)
return resp.SetStatus(basic.CodeFileNoFoundErr)
}
metadataChild := make(map[string]interface{}, 1)
metadataChildData := make(map[string]interface{}, 1)
metadataChildKey := fmt.Sprintf("%d*%d", req.Width, req.Height)
metadataChildData[metadataChildKey] = repositories.Cropping{
ResourceId: uploadRes.ResourceId,
ResourceUrl: uploadRes.ResourceUrl,
Width: req.Width,
Height: req.Height,
}
metadataChild["cropping"] = metadataChildData
// 原图metadata 更新
_, err = l.svcCtx.Repositories.NewResource.UpdateMetadata(l.ctx, &repositories.UpdateMetadataReq{
ResourceId: req.ResourceId,
MetadataChild: metadataChild,
})
// 返回成功的响应和上传URL
if err != nil {
return resp.SetStatus(basic.CodeServiceErr, "原图metadata更新失败")
}
// 返回成功的响应
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"resource_id": uploadRes.ResourceId,
"resource_url": uploadRes.ResourceUrl,

View File

@ -193,9 +193,17 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq
productTemplateV2Info, err := productTemplateV2Model.FindOne(ctx, in.TemplateId)
if err != nil {
logx.Error(err)
logc.Errorf(ctx, "productTemplateV2Model.FindOne%v", err)
return nil, err
}
productTemplateTagInfo, err := gmodel.NewFsProductTemplateTagsModel(l.MysqlConn).FindOneByTagName(ctx, in.TemplateTag, "groups")
if err != nil {
logc.Errorf(ctx, "NewFsProductTemplateTagsModel.FindOneByTagName%v", err)
return nil, err
}
var groupOptions map[string]interface{}
var materialList []interface{}
if productTemplateV2Info.TemplateInfo != nil {
@ -216,6 +224,13 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq
moduleDataMap["groupOptions"] = groupOptions
moduleDataMap["materialList"] = materialList
var tagDataMap []interface{}
err = json.Unmarshal([]byte(*productTemplateTagInfo.Groups), &tagDataMap)
if err != nil {
logc.Errorf(ctx, "Unmarshal tagDataMap%v", err)
return nil, err
}
var combineParam map[string]interface{}
json.Unmarshal([]byte(*resLogoInfo.Metadata), &combineParam)
combineParam["template_tagid"] = in.TemplateTag
@ -227,6 +242,7 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq
var postMap = make(map[string]interface{}, 2)
postMap["module_data"] = moduleDataMap
postMap["tag_data"] = tagDataMap
postMap["param_data"] = combineParam
logc.Infof(ctx, "合图--算法请求--合图--开始时间:%v", time.Now().UTC())

View File

@ -0,0 +1,122 @@
package repositories
import (
"context"
"encoding/json"
"fusenapi/model/gmodel"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/zeromicro/go-zero/core/logc"
"gorm.io/gorm"
)
func NewResource(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) Resource {
return &defaultResource{
MysqlConn: gormDB,
BLMServiceUrl: bLMServiceUrl,
AwsSession: awsSession,
}
}
type (
defaultResource struct {
MysqlConn *gorm.DB
BLMServiceUrl *string
AwsSession *session.Session
}
Resource interface {
// 更新metadata
UpdateMetadata(ctx context.Context, in *UpdateMetadataReq) (*UpdateMetadataRes, error)
}
)
// 全局
type (
Cropping struct {
ResourceId string `json:"resource_id"`
ResourceUrl string `json:"resource_url"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}
)
/* 更新metadata */
type (
UpdateMetadataReq struct {
ResourceId string `json:"resource_id"`
MetadataChild map[string]interface{} `json:"metadata_child"`
}
UpdateMetadataRes struct{}
)
// 更新metadata
func (d *defaultResource) UpdateMetadata(ctx context.Context, in *UpdateMetadataReq) (*UpdateMetadataRes, error) {
// 事务处理
err := d.MysqlConn.Transaction(func(tx *gorm.DB) error {
var NewFsResourceModel = gmodel.NewFsResourceModel(tx)
oldResourceWhere := tx.Where("resource_id = ?", in.ResourceId)
oldResource, err := NewFsResourceModel.FindOneByQuery(ctx, oldResourceWhere, nil)
if err != nil || oldResource.ResourceId == "" {
logc.Errorf(ctx, "FindOneByQuery oldResource error: %v", err)
return err
}
var newResourceMetadata string
if oldResource.Metadata != nil && *oldResource.Metadata != "{}" {
var oldResourceMetadata map[string]interface{}
err = json.Unmarshal([]byte(*oldResource.Metadata), &oldResourceMetadata)
if err != nil {
logc.Errorf(ctx, "Unmarshal oldResourceMetadata error: %v", err)
return err
}
for k, v := range in.MetadataChild {
switch val := v.(type) {
case map[string]interface{}:
oldResourceMetadataData := oldResourceMetadata[k].(map[string]interface{})
for key := range val {
oldResourceMetadataData[key] = val
}
oldResourceMetadata[k] = oldResourceMetadataData
}
}
oldResourceMetadataB, err := json.Marshal(oldResourceMetadata)
if err != nil {
logc.Errorf(ctx, "Marshal oldResourceMetadata error: %v", err)
return err
}
newResourceMetadata = string(oldResourceMetadataB)
} else {
var resourceMetadata = make(map[string]interface{}, len(in.MetadataChild))
for k, v := range in.MetadataChild {
switch val := v.(type) {
case map[string]interface{}:
var resourceMetadataData = make(map[string]interface{}, len(val))
for key := range val {
resourceMetadataData[key] = val
}
resourceMetadata[k] = resourceMetadataData
}
}
newResourceMetadataB, err := json.Marshal(resourceMetadata)
if err != nil {
logc.Errorf(ctx, "Marshal oldResourceMetadata error: %v", err)
return err
}
newResourceMetadata = string(newResourceMetadataB)
}
oldResource.Metadata = &newResourceMetadata
_, err = NewFsResourceModel.BuilderUpdate(ctx, tx, oldResource)
if err != nil {
logc.Errorf(ctx, "Transaction UpdateMetadata error: %v", err)
return err
}
return nil
})
if err != nil {
logc.Errorf(ctx, "BuilderUpdate oldResource error: %v", err)
return nil, err
}
return nil, nil
}
/* 更新metadata */