diff --git a/constants/blm_service.go b/constants/blm_service.go index 2e6406e5..255e77de 100644 --- a/constants/blm_service.go +++ b/constants/blm_service.go @@ -1,6 +1,13 @@ package constants const ( - BLMServiceUrlLogoCombine string = "/LogoCombine" - BLMServiceUrlLogoRemovebg string = "/removebg" + BLMServiceUrlLogoCombine string = "/LogoCombine" + BLMServiceUrlLogoRemovebg string = "/removebg" + BLMServiceUrlLogoFeatureExtraction string = "/FeatureExtraction" ) + +type BLMServiceUrlResult struct { + Code string `json:"code"` + Msg string `json:"msg"` + Data interface{} `json:"data"` +} diff --git a/go.mod b/go.mod index 1f76d5aa..9e65decb 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0 github.com/aws/aws-sdk-go v1.44.295 github.com/bwmarrin/snowflake v0.3.0 + github.com/go-resty/resty/v2 v2.7.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/uuid v1.3.0 github.com/gorilla/websocket v1.5.0 diff --git a/go.sum b/go.sum index e5035777..e1de021a 100644 --- a/go.sum +++ b/go.sum @@ -183,6 +183,8 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= +github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= @@ -687,6 +689,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211008194852-3b03d305991f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= diff --git a/server/resource/internal/types/types.go b/server/resource/internal/types/types.go index ccf8d112..b13c8691 100644 --- a/server/resource/internal/types/types.go +++ b/server/resource/internal/types/types.go @@ -10,7 +10,7 @@ type LogoRemovebgReq struct { LogoFile string `form:"logo_file"` Width string `form:"width"` Height string `form:"height"` - Proportion int64 `form:"proportion"` + Proportion string `form:"proportion"` } type ResourceInfoReq struct { diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index c5ed9c47..27a5b2c5 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -17,6 +17,7 @@ AWS: Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm Token: BLMService: + Url: "http://18.119.109.254:8999" ImageProcess: # Url: "http://192.168.1.7:8999/FeatureExtraction" Url: "http://18.119.109.254:8999/FeatureExtraction" diff --git a/server/upload/internal/config/config.go b/server/upload/internal/config/config.go index 2735b3c2..b474e0e4 100644 --- a/server/upload/internal/config/config.go +++ b/server/upload/internal/config/config.go @@ -22,6 +22,7 @@ type Config struct { } } BLMService struct { + Url string ImageProcess struct { Url string } diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go index 3d441f07..8cdf3f53 100644 --- a/server/upload/internal/logic/uploadlogologic.go +++ b/server/upload/internal/logic/uploadlogologic.go @@ -126,6 +126,15 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us Source: "upload-logo", }) + // 是否去背景 + if req.IsRemoveBg == 1 { + // res, err := l.svcCtx.Repositories.ImageHandle.LogoInfoSet(l.ctx, &repositories.LogoInfoSetReq{}) + + // if err != nil { + // return resp.SetStatus(basic.CodeServiceErr) + // } + } + if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeFileUploadErr, "upload file failed") diff --git a/server/upload/internal/svc/servicecontext.go b/server/upload/internal/svc/servicecontext.go index be5f2f25..6f9c644a 100644 --- a/server/upload/internal/svc/servicecontext.go +++ b/server/upload/internal/svc/servicecontext.go @@ -21,9 +21,10 @@ type ServiceContext struct { Config config.Config SharedState *shared.SharedState - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - AwsSession *session.Session + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + AwsSession *session.Session + Repositories *initalize.Repositories } func NewServiceContext(c config.Config) *ServiceContext { @@ -39,6 +40,11 @@ func NewServiceContext(c config.Config) *ServiceContext { MysqlConn: initalize.InitMysql(c.SourceMysql), AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), AwsSession: session.Must(session.NewSession(&config)), + Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{ + GormDB: initalize.InitMysql(c.SourceMysql), + BLMServiceUrl: &c.BLMService.Url, + AwsSession: session.Must(session.NewSession(&config)), + }), } } diff --git a/server_api/resource.api b/server_api/resource.api index f78dae89..da716189 100644 --- a/server_api/resource.api +++ b/server_api/resource.api @@ -26,7 +26,7 @@ type ( LogoFile string `form:"logo_file"` Width string `form:"width"` Height string `form:"height"` - Proportion int64 `form:"proportion"` + Proportion string `form:"proportion"` } ) diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go index c1899458..75fbbf53 100644 --- a/service/repositories/image_handle.go +++ b/service/repositories/image_handle.go @@ -35,6 +35,8 @@ type ( } ImageHandle = interface { + // logo信息 + LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error) // logo合图 LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) @@ -44,6 +46,32 @@ type ( } ) +/* logo信息 */ +type ( + LogoInfoSetReq struct { + LogoUrl string `json:"logo_url"` + } + LogoInfoSetRes struct{} +) + +func (l *defaultImageHandle) LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error) { + var resultBLM constants.BLMServiceUrlResult + postMap := make(map[string]string, 1) + postMap["logo_url"] = in.LogoUrl + + _, err := curl.NewClient(ctx, &curl.Config{ + BaseUrl: *l.BLMServiceUrl, + Url: constants.BLMServiceUrlLogoCombine, + }).PostForm(postMap, &resultBLM) + if err != nil { + logx.Error(err) + return nil, err + } + return nil, nil +} + +/* logo信息 */ + /* logo合图 */ type ( LogoCombineReq struct { @@ -249,7 +277,7 @@ type ( LogoFile string `json:"logo_file"` Width string `json:"width"` Height string `json:"height"` - Proportion int64 `json:"proportion"` + Proportion string `json:"proportion"` } LogoStandardRes struct { ResourceId string @@ -299,53 +327,24 @@ func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardR } } + var resultBLM constants.BLMServiceUrlResult var postMap = make(map[string]interface{}, 5) postMap["is_remove_bg"] = in.IsRemoveBg postMap["logo_file"] = in.LogoFile postMap["width"] = in.Width postMap["height"] = in.Height postMap["proportion"] = in.Proportion - postMapB, _ := json.Marshal(postMap) - var headerData = make(map[string]string, 1) - headerData["Content-Type"] = "application/json" - result, err := curl.ApiCall(*l.BLMServiceUrl+constants.BLMServiceUrlLogoRemovebg, "POST", headerData, strings.NewReader(string(postMapB)), time.Minute*5) + err = curl.NewClient(ctx, &curl.Config{ + BaseUrl: *l.BLMServiceUrl, + Url: constants.BLMServiceUrlLogoRemovebg, + }).PostJson(postMap, &resultBLM) + if err != nil { logx.Error(err) return nil, err } - defer result.Body.Close() - b, err := io.ReadAll(result.Body) - if err != nil { - logx.Error(err) - return nil, err - } - var resultStr string - if string(b) == "Internal Server Error" { - err = errors.New("BLMService fail Internal Server Error") - logx.Error(err) - return nil, err - } else { - var resData map[string]interface{} - err = json.Unmarshal(b, &resData) - if err != nil || resData == nil { - logx.Error(err) - return nil, err - } - - if resData != nil { - if resData["code"].(string) == "200" { - resultStr = resData["data"].(string) - } else { - logx.Error(err) - return nil, err - } - } else { - logx.Error(err) - return nil, err - } - } - + var resultStr string = resultBLM.Data.(string) var resultData map[string]interface{} err = json.Unmarshal([]byte(resultStr), &resultData) if err != nil || resultData == nil { @@ -353,8 +352,6 @@ func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardR return nil, err } - //$removeBg ='{"nobg_url": "/test/dIE10gGfXM_scale.png", "thumbnail_url": "/test/dIE10gGfXM_thumbnail.png", "ismax_proportion": true, "img_color": ["#000000", "#EEF5FB", "#6AAFE6", "#9ECDF1", "#298EDC", "#0C7BD1"]}' - var fileBase = resultData["nobg_url"].(string) ismaxProportion = resultData["ismax_proportion"].(bool) diff --git a/utils/curl/client_resty.go b/utils/curl/client_resty.go new file mode 100644 index 00000000..a131a253 --- /dev/null +++ b/utils/curl/client_resty.go @@ -0,0 +1,144 @@ +package curl + +import ( + "bytes" + "context" + "errors" + "time" + + "github.com/go-resty/resty/v2" + "github.com/zeromicro/go-zero/core/logc" + "github.com/zeromicro/go-zero/core/logx" +) + +func NewClient(ctx context.Context, c *Config) Client { + // 创建一个restry客户端 + client := resty.New().SetBaseURL(c.BaseUrl) + + // 设置超时时间为 5 分钟 + client.SetTimeout(5 * time.Minute) + + if c.HeaderData != nil { + for k, v := range c.HeaderData { + client = client.SetHeader(k, v) + } + } + + if c.RetryCount > 0 { + client = client.SetRetryCount(int(c.RetryCount)) + } + + if c.RetryWaitTime > 0 { + client = client.SetRetryWaitTime(time.Duration(c.RetryWaitTime) * time.Second) + } + + return &defaultClient{ + client: client, + c: c, + ctx: ctx, + } +} + +type ( + Config struct { + BaseUrl string `json:"base_url"` + Url string `json:"url"` + HeaderData map[string]string `json:"header_data"` + RetryCount int64 `json:"retry_count"` + RetryWaitTime int64 `json:"retry_wait_time"` + } + defaultClient struct { + c *Config + client *resty.Client + ctx context.Context + } + + Client = interface { + // PostJson请求 + PostJson(jsonData interface{}, res interface{}) error + // PostForm请求 + PostForm(formData map[string]string, res interface{}) (interface{}, error) + // 上传文件 + PostFile(fileParam string, fileName string, res interface{}, profileBytes []byte, formData map[string]string) (interface{}, error) + } +) + +// PostJson请求 +func (c *defaultClient) PostJson(jsonData interface{}, res interface{}) error { + logc.Infof(c.ctx, "客户端名称 Client PostJson jsonData:%+v", jsonData) + + logc.Infof(c.ctx, "客户端名称 请求开始时间:%+v", time.Now().UTC()) + resp, err := c.client. + SetHeader("Accept", "application/json"). + SetHeader("Content-Type", "application/json"). + R(). + SetContext(c.ctx). + SetResult(res). + SetBody(jsonData). + Post(c.c.Url) + + if err != nil { + logc.Errorf(c.ctx, "客户端 请求失败 Client PostForm error:%+v", err) + return err + } + logc.Infof(c.ctx, "客户端名称 请求结束时间:%+v", time.Now().UTC()) + + logc.Infof(c.ctx, "客户端 请求返回结果 Client PostForm RawResponse result:%+v,%+V", resp.RawResponse.Status, resp.RawResponse.StatusCode) + + //logc.Infof(c.ctx, "客户端 请求返回结果 Client PostForm res result:%+v", res) + if resp.StatusCode() != 200 { + err = errors.New("服务端失败") + logx.Errorf("客户端 请求失败 Client PostForm Server error:%+v", err) + } + return err +} + +// PostForm请求 +func (c *defaultClient) PostForm(formData map[string]string, res interface{}) (interface{}, error) { + resp, err := c.client. + SetHeader("Accept", "application/json"). + SetHeader("Content-Type", "multipart/form-data"). + R(). + SetContext(c.ctx). + SetResult(res). + SetFormData(formData). + Post(c.c.Url) + + if err != nil { + logc.Errorf(c.ctx, "客户端 请求失败 Client PostForm error:%+v", err) + return res, err + } + logc.Infof(c.ctx, "客户端 请求返回结果 Client PostForm RawResponse result:%+v,%+V", resp.RawResponse.Status, resp.RawResponse.StatusCode) + logc.Infof(c.ctx, "客户端 请求返回结果 Client PostForm res result:%+v", res) + if resp.StatusCode() != 200 { + err = errors.New("服务端失败") + logx.Errorf("客户端 请求失败 Client PostForm Server error:%+v", err) + } + return res, err +} + +// 上传文件 +func (c *defaultClient) PostFile(fileParam string, fileName string, res interface{}, profileBytes []byte, formData map[string]string) (interface{}, error) { + + resp, err := c.client. + SetHeader("Accept", "application/json"). + SetHeader("Content-Type", "multipart/form-data"). + R(). + SetContext(c.ctx). + SetResult(res). + SetFileReader(fileParam, fileName, bytes.NewReader(profileBytes)). + SetFormData(formData). + Post(c.c.Url) + + if err != nil { + logc.Errorf(c.ctx, "客户端 请求失败 Client PostFile error:%+v", err) + return res, err + } + logc.Infof(c.ctx, "客户端 请求返回结果 Client PostFile RawResponse result:%+v,%+V", resp.RawResponse.Status, resp.RawResponse.StatusCode) + logc.Infof(c.ctx, "客户端 请求返回结果 Client PostFile res result:%+v", res) + if resp.StatusCode() != 200 { + err = errors.New("服务端失败") + logx.Errorf("客户端 请求失败 Client PostFile Server error:%+v", err) + } + return res, err +}