@@ -1,17 +1,9 @@
package logic
import (
"encoding/json "
"errors"
"fusenapi/model/gmodel"
"fusenapi/service/repositories "
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/curl"
"fusenapi/utils/file"
"fusenapi/utils/hash"
"io"
"strings"
"time"
"context"
@@ -19,7 +11,6 @@ import (
"fusenapi/server/resource/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type LogoCombineLogic struct {
@@ -65,125 +56,22 @@ func (l *LogoCombineLogic) LogoCombine(req *types.LogoCombineReq, userinfo *auth
// 否则, 使用用户ID和用户键名格式
userId = userinfo . UserId
}
// 根据hash 查询数据资源
var resourceId string = hash . JsonHashKey ( req . ResourceKey )
resourceModel := gmodel . NewFsResourceModel ( l . svcCtx . MysqlConn )
resourceInfo , err := resourceModel . FindOneById ( l . ctx , resourceId )
if err == nil && resourceInfo . ResourceId != "" {
return resp . SetStatus ( basic . CodeOK , map [ string ] interface { } {
"resource_id" : resourceId ,
"resource_url" : resourceInfo . ResourceUrl ,
"resource_metadata" : resourceInfo . Metadata ,
} )
} else {
if err != nil {
if ! errors . Is ( err , gorm . ErrRecordNotFound ) {
logx . Error ( err )
return resp . SetStatus ( basic . CodeDbSqlErr , "LogoCombine error" )
}
}
}
// 没有查到, 先根据模版id 查询模版数据 请求算法数据
productTemplateV2Model := gmodel . NewFsProductTemplateV2Model ( l . svcCtx . MysqlConn )
productTemplateV2Info , err := productTemplateV2Model . FindOne ( l . ctx , req . TemplateId )
if err != nil {
logx . Error ( err )
return resp . SetStatus ( basic . CodeDbSqlErr , "LogoCombine error" )
}
var groupOptions map [ string ] interface { }
if productTemplateV2Info . GroupOptions != nil {
err = json . Unmarshal ( [ ] byte ( * productTemplateV2Info . GroupOptions ) , & groupOptions )
if err != nil {
logx . Error ( err )
return resp . SetStatus ( basic . CodeDbSqlErr , "LogoCombine error Unmarshal groupOptions" )
}
}
var materialList [ ] interface { }
if productTemplateV2Info . TemplateInfo != nil {
var templateInfo map [ string ] interface { }
err = json . Unmarshal ( [ ] byte ( * productTemplateV2Info . TemplateInfo ) , & templateInfo )
if err != nil {
logx . Error ( err )
return resp . SetStatus ( basic . CodeDbSqlErr , "LogoCombine error Unmarshal templateInfo" )
}
materialList = templateInfo [ "materialList" ] . ( [ ] interface { } )
}
var moduleDataMap = make ( map [ string ] interface { } , 4 )
moduleDataMap [ "id" ] = productTemplateV2Info . Id
moduleDataMap [ "material" ] = productTemplateV2Info . MaterialImg
moduleDataMap [ "groupOptions" ] = groupOptions
moduleDataMap [ "materialList" ] = materialList
var combineParam map [ string ] interface { }
json . Unmarshal ( [ ] byte ( req . CombineParam ) , & combineParam )
var postMap = make ( map [ string ] interface { } , 2 )
postMap [ "module_data" ] = moduleDataMap
postMap [ "param_data" ] = combineParam
postMapB , _ := json . Marshal ( postMap )
//result, err := http.Post(l.svcCtx.Config.BLMService.LogoCombine.Url, "application/json", strings.NewReader(string(postMapB)))
var headerData = make ( map [ string ] string , 1 )
headerData [ "Content-Type" ] = "application/json"
result , err := curl . ApiCall ( l . svcCtx . Config . BLMService . LogoCombine . Url , "POST" , headerData , strings . NewReader ( string ( postMapB ) ) , time . Second * 20 )
if err != nil {
logx . Error ( err )
return resp . SetStatus ( basic . CodeFileLogoCombineErr , "service post fail" )
}
defer result . Body . Close ( )
b , err := io . ReadAll ( result . Body )
if err != nil {
logx . Error ( err )
return resp . SetStatus ( basic . CodeFileLogoCombineErr , "service read fail" )
}
ress := string ( b )
if ress == "Internal Server Error" {
return resp . SetStatus ( basic . CodeFileLogoCombineErr , "service read fail" )
}
var resultData map [ string ] interface { }
err = json . Unmarshal ( b , & resultData )
if err != nil {
logx . Error ( err )
return resp . SetStatus ( basic . CodeFileLogoCombineErr , "service read fail" )
}
// {
// id: "",
// logo_url:"https://s3.amazon.com/xxxx",
// result: "$saa541afaldjaldjasldjsadjsapsaasda"
// }
var fileBase = resultData [ "result" ]
// 上传文件
var upload = file . Upload {
Ctx : l . ctx ,
MysqlConn : l . svcCtx . MysqlConn ,
AwsSession : l . svcCtx . AwsSession ,
}
uploadRes , err := upload . UploadFileByBase64 ( & file . UploadBaseReq {
FileHash : resourceId ,
FileData : fileBase . ( string ) ,
UploadBucket : 1 ,
ApiType : 2 ,
res , err := l . svcCtx . Repositories . ImageHandle . LogoCombine ( l . ctx , & repositories . LogoCombineReq {
ResourceKey : req . ResourceKey ,
TemplateId : req . TemplateId ,
CombineParam : req . CombineParam ,
UserId : userId ,
GuestId : guestId ,
} )
if err != nil {
logx . Error ( e rr)
return resp . SetStatus ( basic . CodeFileLogoCombineErr , "LogoCombine error upload file failed" )
return resp . SetStatus ( basic . CodeServiceE rr)
}
// 返回成功的响应和上传URL
return resp . SetStatus ( basic . CodeOK , map [ string ] interface { } {
"resource_id" : resourceId ,
"resource_url" : uploadR es. ResourceUrl ,
"resource_id" : res. Res ourceId ,
"resource_url" : r es. ResourceUrl ,
"resource_metadata" : "" ,
} )
}