From f92bf8bfeeea75c4bb792d84c3291af06d9d649f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 14:49:15 +0800 Subject: [PATCH 01/15] fix --- goctl_template/api/context.tpl | 1 - server/render/etc/render.yaml | 8 +++++- server/render/internal/config/config.go | 9 +++++++ .../internal/logic/rendernotifylogic.go | 25 +++++++++++++++--- server/render/internal/svc/servicecontext.go | 26 ++++++++++++------- .../resource/internal/svc/servicecontext.go | 1 - .../websocket/internal/svc/servicecontext.go | 1 - 7 files changed, 53 insertions(+), 18 deletions(-) diff --git a/goctl_template/api/context.tpl b/goctl_template/api/context.tpl index e5884b34..fdf46361 100644 --- a/goctl_template/api/context.tpl +++ b/goctl_template/api/context.tpl @@ -22,7 +22,6 @@ type ServiceContext struct { } func NewServiceContext(c {{.config}}) *ServiceContext { - initalize.InitRabbitMq(c.SourceRabbitMq, nil) return &ServiceContext{ Config: c, MysqlConn: initalize.InitMysql(c.SourceMysql), diff --git a/server/render/etc/render.yaml b/server/render/etc/render.yaml index d0f93153..86b2a6a1 100644 --- a/server/render/etc/render.yaml +++ b/server/render/etc/render.yaml @@ -6,4 +6,10 @@ Auth: AccessSecret: fusen2023 AccessExpire: 2592000 RefreshAfter: 1592000 -SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672 \ No newline at end of file +SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672 +AWS: + S3: + Credentials: + AccessKeyID: AKIAZB2JKUXDPNRP4YT2 + Secret: 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 + Token: \ No newline at end of file diff --git a/server/render/internal/config/config.go b/server/render/internal/config/config.go index fa950a01..71834c5d 100644 --- a/server/render/internal/config/config.go +++ b/server/render/internal/config/config.go @@ -10,4 +10,13 @@ type Config struct { SourceMysql string Auth types.Auth SourceRabbitMq string + AWS struct { + S3 struct { + Credentials struct { + AccessKeyID string + Secret string + Token string + } + } + } } diff --git a/server/render/internal/logic/rendernotifylogic.go b/server/render/internal/logic/rendernotifylogic.go index b3466aaa..6a11f4bf 100644 --- a/server/render/internal/logic/rendernotifylogic.go +++ b/server/render/internal/logic/rendernotifylogic.go @@ -6,6 +6,7 @@ import ( "fusenapi/constants" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/file" "fusenapi/utils/websocket_data" "fusenapi/server/render/internal/svc" @@ -62,15 +63,31 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") }*/ - //创建/更新资源 - + // 上传文件 + var upload = file.Upload{ + Ctx: l.ctx, + MysqlConn: l.svcCtx.MysqlConn, + AwsSession: l.svcCtx.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + FileHash: req.Info.TaskId, + FileData: req.Info.Image, + UploadBucket: 1, + ApiType: 2, + UserId: req.Info.UserId, + GuestId: req.Info.GuestId, + }) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image") + } //发送消息到对应的rabbitmq data := websocket_data.RenderImageNotify{ TaskId: req.Info.TaskId, - Image: req.Info.Image, + Image: uploadRes.ResourceUrl, } d, _ := json.Marshal(data) - if err := l.svcCtx.RabbitMq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil { + if err = l.svcCtx.RabbitMq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil { logx.Error(err) return resp.SetStatus(basic.CodeServiceErr, "failed to send data") } diff --git a/server/render/internal/svc/servicecontext.go b/server/render/internal/svc/servicecontext.go index 79e04e0d..08a78653 100644 --- a/server/render/internal/svc/servicecontext.go +++ b/server/render/internal/svc/servicecontext.go @@ -4,6 +4,9 @@ import ( "errors" "fmt" "fusenapi/server/render/internal/config" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" "net/http" "fusenapi/initalize" @@ -14,20 +17,23 @@ import ( ) type ServiceContext struct { - Config config.Config - - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - RabbitMq *initalize.RabbitMqHandle + Config config.Config + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle + AwsSession *session.Session } func NewServiceContext(c config.Config) *ServiceContext { - initalize.InitRabbitMq(c.SourceRabbitMq, nil) + config := aws.Config{ + Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token), + } return &ServiceContext{ - Config: c, - MysqlConn: initalize.InitMysql(c.SourceMysql), - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), - RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + Config: c, + MysqlConn: initalize.InitMysql(c.SourceMysql), + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + AwsSession: session.Must(session.NewSession(&config)), } } diff --git a/server/resource/internal/svc/servicecontext.go b/server/resource/internal/svc/servicecontext.go index f4b7039e..a81a0767 100644 --- a/server/resource/internal/svc/servicecontext.go +++ b/server/resource/internal/svc/servicecontext.go @@ -29,7 +29,6 @@ func NewServiceContext(c config.Config) *ServiceContext { config := aws.Config{ Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token), } - initalize.InitRabbitMq(c.SourceRabbitMq, nil) return &ServiceContext{ Config: c, MysqlConn: initalize.InitMysql(c.SourceMysql), diff --git a/server/websocket/internal/svc/servicecontext.go b/server/websocket/internal/svc/servicecontext.go index 3ca743ec..df41e0b3 100644 --- a/server/websocket/internal/svc/servicecontext.go +++ b/server/websocket/internal/svc/servicecontext.go @@ -22,7 +22,6 @@ type ServiceContext struct { } func NewServiceContext(c config.Config) *ServiceContext { - initalize.InitRabbitMq(c.SourceRabbitMq, nil) return &ServiceContext{ Config: c, MysqlConn: initalize.InitMysql(c.SourceMysql), From 7b32eb91547c0f946f33cb1ca9e5ef1b8cb73d88 Mon Sep 17 00:00:00 2001 From: Hiven Date: Wed, 9 Aug 2023 14:58:20 +0800 Subject: [PATCH 02/15] =?UTF-8?q?feat:=E8=8E=B7=E5=8F=96=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/uploadfilebaselogic.go | 107 ++++------------- utils/file/upload.go | 112 ++++++++++-------- 2 files changed, 83 insertions(+), 136 deletions(-) diff --git a/server/upload/internal/logic/uploadfilebaselogic.go b/server/upload/internal/logic/uploadfilebaselogic.go index 1383730a..0e079884 100644 --- a/server/upload/internal/logic/uploadfilebaselogic.go +++ b/server/upload/internal/logic/uploadfilebaselogic.go @@ -1,21 +1,16 @@ package logic import ( - "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/file" "fusenapi/utils/hash" - "time" "context" "fusenapi/server/upload/internal/svc" "fusenapi/server/upload/internal/types" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/request" - "github.com/aws/aws-sdk-go/service/s3" "github.com/zeromicro/go-zero/core/logx" ) @@ -66,91 +61,35 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri } } - // 定义存储桶名称 - var bucketName *string - var apiType int64 = req.ApiType - - // 根据类别选择存储桶 - switch req.UploadBucket { - case 2: - bucketName = basic.TempfileBucketName - default: - bucketName = basic.StorageBucketName - } - - // 设置AWS会话的区域 - l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1") - - // 创建新的S3服务实例 - svc := s3.New(l.svcCtx.AwsSession) - - // 定义S3请求和当前时间 - var s3req *request.Request - + // 根据hash 查询数据资源 var resourceId string = hash.JsonHashKey(req.FileKey) - var uploadUrl = UploadUrl{} - resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) - resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId) - if err == nil && resourceInfo.ResourceId != "" { - uploadUrl.Status = 1 - uploadUrl.ResourceId = resourceId - uploadUrl.ResourceUrl = *resourceInfo.ResourceUrl - } else { - dist, contentType, err := file.FileBase64ToByte(req.FileData) + // 上传文件 + var upload = file.Upload{ + Ctx: l.ctx, + MysqlConn: l.svcCtx.MysqlConn, + AwsSession: l.svcCtx.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + FileHash: resourceId, + FileData: req.FileData, + UploadBucket: req.UploadBucket, + ApiType: req.ApiType, + UserId: userId, + GuestId: guestId, + }) - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,base64tobyte error") - } - - // 创建S3对象存储请求 - s3req, _ = svc.PutObjectRequest( - &s3.PutObjectInput{ - Bucket: bucketName, - Key: &resourceId, - }, - ) - - // 设置请求体为文件数据 - s3req.SetBufferBody(dist) - - // 发送请求 - err = s3req.Send() - - // 检查是否有错误 - if err != nil { - logx.Error(err) - uploadUrl.Status = 0 - } else { - var url = s3req.HTTPRequest.URL.String() - // 打印请求URL - logx.Info(url) - uploadUrl.Status = 1 - uploadUrl.ResourceId = resourceId - uploadUrl.ResourceUrl = url - var version string = "0.0.1" - var nowTime = time.Now() - _, err = resourceModel.Create(l.ctx, &gmodel.FsResource{ - ResourceId: resourceId, - UserId: &userId, - GuestId: &guestId, - ResourceType: &contentType, - ResourceUrl: &url, - Version: &version, - UploadedAt: &nowTime, - Metadata: &req.Metadata, - ApiType: &apiType, - BucketName: bucketName, - }) - if err != nil { - logx.Error(err) - } - } + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeFileUploadErr, "upload file failed") } // 返回成功的响应和上传URL return resp.SetStatus(basic.CodeOK, map[string]interface{}{ - "upload_data": uploadUrl, + "upload_data": UploadUrl{ + Status: 1, + ResourceId: uploadRes.ResourceId, + ResourceUrl: uploadRes.ResourceUrl, + }, }) } diff --git a/utils/file/upload.go b/utils/file/upload.go index 38b0e826..2596b14e 100644 --- a/utils/file/upload.go +++ b/utils/file/upload.go @@ -65,63 +65,71 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er var uploadBaseRes = UploadBaseRes{} resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn) - resourceInfo, err := resourceModel.FindOneById(upload.Ctx, resourceId) - if err == nil && resourceInfo.ResourceId != "" { - uploadBaseRes.Status = 1 - uploadBaseRes.ResourceId = resourceId - uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl - } else { - dist, contentType, err := FileBase64ToByte(req.FileData) - - if err != nil { - logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoGet.mysql") - return nil, err - } - - // 创建S3对象存储请求 - s3req, _ = svc.PutObjectRequest( - &s3.PutObjectInput{ - Bucket: bucketName, - Key: &resourceId, - }, - ) - - // 设置请求体为文件数据 - s3req.SetBufferBody(dist) - - // 发送请求 - err = s3req.Send() - - // 检查是否有错误 - if err != nil { - logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.s3req") - return nil, err - } else { - var url = s3req.HTTPRequest.URL.String() - // 打印请求URL - logx.Info(url) + err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error { + resourceModelTS := gmodel.NewFsResourceModel(connGorm) + resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId) + if err == nil && resourceInfo.ResourceId != "" { uploadBaseRes.Status = 1 uploadBaseRes.ResourceId = resourceId - uploadBaseRes.ResourceUrl = url - var version string = "0.0.1" - var nowTime = time.Now() - _, err = resourceModel.Create(upload.Ctx, &gmodel.FsResource{ - ResourceId: resourceId, - UserId: &req.UserId, - GuestId: &req.GuestId, - ResourceType: &contentType, - ResourceUrl: &url, - Version: &version, - UploadedAt: &nowTime, - Metadata: &req.Metadata, - ApiType: &apiType, - BucketName: bucketName, - }) + uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl + } else { + dist, contentType, err := FileBase64ToByte(req.FileData) + if err != nil { - logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoAdd.mysql") - return nil, err + logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoGet.mysql") + return err + } + + // 创建S3对象存储请求 + s3req, _ = svc.PutObjectRequest( + &s3.PutObjectInput{ + Bucket: bucketName, + Key: &resourceId, + }, + ) + + // 设置请求体为文件数据 + s3req.SetBufferBody(dist) + + // 发送请求 + err = s3req.Send() + + // 检查是否有错误 + if err != nil { + logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.s3req") + return err + } else { + var url = s3req.HTTPRequest.URL.String() + // 打印请求URL + logx.Info(url) + uploadBaseRes.Status = 1 + uploadBaseRes.ResourceId = resourceId + uploadBaseRes.ResourceUrl = url + var version string = "0.0.1" + var nowTime = time.Now() + _, err = resourceModelTS.Create(upload.Ctx, &gmodel.FsResource{ + ResourceId: resourceId, + UserId: &req.UserId, + GuestId: &req.GuestId, + ResourceType: &contentType, + ResourceUrl: &url, + Version: &version, + UploadedAt: &nowTime, + Metadata: &req.Metadata, + ApiType: &apiType, + BucketName: bucketName, + }) + if err != nil { + logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoAdd.mysql") + return err + } } } + return nil + }) + if err != nil { + return nil, err } + return &uploadBaseRes, err } From 29e42011a9a0d825c1392c7caaaea5831a1470ef Mon Sep 17 00:00:00 2001 From: Hiven Date: Wed, 9 Aug 2023 15:15:12 +0800 Subject: [PATCH 03/15] =?UTF-8?q?feat:=E8=8E=B7=E5=8F=96=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/uploadfilesbackendlogic.go | 89 +++++------------- utils/file/upload.go | 91 +++++++++++++++++++ 2 files changed, 112 insertions(+), 68 deletions(-) diff --git a/server/upload/internal/logic/uploadfilesbackendlogic.go b/server/upload/internal/logic/uploadfilesbackendlogic.go index a0fba113..eb1a546a 100644 --- a/server/upload/internal/logic/uploadfilesbackendlogic.go +++ b/server/upload/internal/logic/uploadfilesbackendlogic.go @@ -2,22 +2,18 @@ package logic import ( "encoding/json" - "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/file" "fusenapi/utils/hash" "io" "net/http" - "time" "context" "fusenapi/server/upload/internal/svc" "fusenapi/server/upload/internal/types" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/request" - "github.com/aws/aws-sdk-go/service/s3" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/mr" ) @@ -101,16 +97,6 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, //获取上传的文件组 files := l.r.MultipartForm.File["file"] - // 设置AWS会话的区域 - l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1") - - // 创建新的S3服务实例 - svc := s3.New(l.svcCtx.AwsSession) - - // 定义S3请求和当前时间 - var s3req *request.Request - - resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) result, err := mr.MapReduce(func(source chan<- interface{}) { for i, info := range uploadInfoList { fileType := files[i].Header.Get("Content-Type") @@ -148,60 +134,28 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, uploadUrl.ResourceType = uploadDataInfo.FileType var resourceId string = uploadDataInfo.HashKey - // 查询数据库 - resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId) - if err == nil && resourceInfo.ResourceId != "" { - uploadUrl.Status = 1 - uploadUrl.ResourceId = resourceId - uploadUrl.ResourceUrl = *resourceInfo.ResourceUrl - } else { - // 创建S3对象存储请求 - s3req, _ = svc.PutObjectRequest( - &s3.PutObjectInput{ - Bucket: uploadDataInfo.Bucket, - Key: &uploadDataInfo.HashKey, - }, - ) - // 设置请求体为文件数据 - s3req.SetBufferBody(uploadDataInfo.FileData) - - // 发送请求 - err = s3req.Send() - // 检查是否有错误 - if err != nil { - logx.Error(err) - uploadUrl.Status = 0 - } else { - contentType := http.DetectContentType(uploadDataInfo.FileData) - var url = s3req.HTTPRequest.URL.String() - // 打印请求URL - logx.Info(url) - uploadUrl.Status = 1 - uploadUrl.ResourceId = resourceId - uploadUrl.ResourceUrl = url - var version string = "0.0.1" - var nowTime = time.Now() - _, err = resourceModel.Create(l.ctx, &gmodel.FsResource{ - ResourceId: resourceId, - UserId: &userId, - GuestId: &guestId, - ResourceType: &contentType, - ResourceUrl: &url, - Version: &version, - UploadedAt: &nowTime, - Metadata: &uploadDataInfo.Metadata, - ApiType: &uploadDataInfo.ApiType, - BucketName: bucketName, - }) - if err != nil { - logx.Error(err) - } - } + // 上传文件 + var upload = file.Upload{ + Ctx: l.ctx, + MysqlConn: l.svcCtx.MysqlConn, + AwsSession: l.svcCtx.AwsSession, + } + uploadRes, err := upload.UploadFileByByte(&file.UploadBaseReq{ + FileHash: resourceId, + FileByte: uploadDataInfo.FileData, + UploadBucket: 1, + ApiType: 2, + UserId: userId, + GuestId: guestId, + }) + if err == nil { + uploadUrl.ResourceId = uploadRes.ResourceId + uploadUrl.ResourceType = uploadRes.ResourceType + uploadUrl.ResourceUrl = uploadRes.ResourceUrl + // Notice 这个必须加! + writer.Write(uploadUrl) } - - // Notice 这个必须加! - writer.Write(uploadUrl) }, func(pipe <-chan interface{}, writer mr.Writer[interface{}], cancel func(error)) { var uploadUrlList = make(map[string][]*UploadUrl) var uploadUrlListFail []*UploadUrl @@ -235,7 +189,6 @@ type UploadInfo struct { FileKeys string `json:"file_keys"` // 上传文件唯一标识 FileData *string `json:"file_data"` // 上传文件Base64 Metadata string `json:"meta_data"` // 上传文件额外信息 - } type UploadData struct { diff --git a/utils/file/upload.go b/utils/file/upload.go index 2596b14e..0038ed1c 100644 --- a/utils/file/upload.go +++ b/utils/file/upload.go @@ -4,6 +4,7 @@ import ( "context" "fusenapi/model/gmodel" "fusenapi/utils/basic" + "net/http" "time" "github.com/aws/aws-sdk-go/aws" @@ -28,6 +29,7 @@ type UploadBaseReq struct { ApiType int64 UserId int64 GuestId int64 + FileByte []byte } type UploadBaseRes struct { @@ -133,3 +135,92 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er return &uploadBaseRes, err } + +func (upload *Upload) UploadFileByByte(req *UploadBaseReq) (*UploadBaseRes, error) { + // 定义存储桶名称 + var bucketName *string + var apiType int64 = req.ApiType + + // 根据类别选择存储桶 + switch req.UploadBucket { + case 2: + bucketName = basic.TempfileBucketName + default: + bucketName = basic.StorageBucketName + } + + // 设置AWS会话的区域 + upload.AwsSession.Config.Region = aws.String("us-west-1") + + // 创建新的S3服务实例 + svc := s3.New(upload.AwsSession) + + // 定义S3请求和当前时间 + var s3req *request.Request + + var resourceId string = req.FileHash + + var uploadBaseRes = UploadBaseRes{} + resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn) + err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error { + resourceModelTS := gmodel.NewFsResourceModel(connGorm) + resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId) + if err == nil && resourceInfo.ResourceId != "" { + uploadBaseRes.Status = 1 + uploadBaseRes.ResourceId = resourceId + uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl + } else { + contentType := http.DetectContentType(req.FileByte) + // 创建S3对象存储请求 + s3req, _ = svc.PutObjectRequest( + &s3.PutObjectInput{ + Bucket: bucketName, + Key: &resourceId, + }, + ) + + // 设置请求体为文件数据 + s3req.SetBufferBody(req.FileByte) + + // 发送请求 + err = s3req.Send() + + // 检查是否有错误 + if err != nil { + logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.s3req") + return err + } else { + var url = s3req.HTTPRequest.URL.String() + // 打印请求URL + logx.Info(url) + uploadBaseRes.Status = 1 + uploadBaseRes.ResourceId = resourceId + uploadBaseRes.ResourceUrl = url + var version string = "0.0.1" + var nowTime = time.Now() + _, err = resourceModelTS.Create(upload.Ctx, &gmodel.FsResource{ + ResourceId: resourceId, + UserId: &req.UserId, + GuestId: &req.GuestId, + ResourceType: &contentType, + ResourceUrl: &url, + Version: &version, + UploadedAt: &nowTime, + Metadata: &req.Metadata, + ApiType: &apiType, + BucketName: bucketName, + }) + if err != nil { + logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoAdd.mysql") + return err + } + } + } + return nil + }) + if err != nil { + return nil, err + } + + return &uploadBaseRes, err +} From 2d0d410c404194a76a16a73365fd66fc94dd6c7e Mon Sep 17 00:00:00 2001 From: Hiven Date: Wed, 9 Aug 2023 15:17:02 +0800 Subject: [PATCH 04/15] =?UTF-8?q?feat:=E8=8E=B7=E5=8F=96=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/upload/internal/logic/uploadfilesbackendlogic.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/upload/internal/logic/uploadfilesbackendlogic.go b/server/upload/internal/logic/uploadfilesbackendlogic.go index eb1a546a..24a5aeaa 100644 --- a/server/upload/internal/logic/uploadfilesbackendlogic.go +++ b/server/upload/internal/logic/uploadfilesbackendlogic.go @@ -150,6 +150,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, GuestId: guestId, }) if err == nil { + uploadUrl.Status = 1 uploadUrl.ResourceId = uploadRes.ResourceId uploadUrl.ResourceType = uploadRes.ResourceType uploadUrl.ResourceUrl = uploadRes.ResourceUrl From fcf7c909a368918d991e312019c7de24948b5006 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 15:29:27 +0800 Subject: [PATCH 05/15] fix --- model/gmodel/fs_resource_logic.go | 14 +++--------- .../render/consumer/assemble_render_data.go | 22 ++++++++++++++++++- .../internal/logic/datatransferlogic.go | 8 +++---- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/model/gmodel/fs_resource_logic.go b/model/gmodel/fs_resource_logic.go index 6dab100f..19db19ad 100644 --- a/model/gmodel/fs_resource_logic.go +++ b/model/gmodel/fs_resource_logic.go @@ -2,7 +2,6 @@ package gmodel import ( "context" - "errors" "fusenapi/utils/handlers" "gorm.io/gorm" @@ -10,16 +9,9 @@ import ( // TODO: 使用model的属性做你想做的 -func (p *FsResourceModel) FindOneById(ctx context.Context, resourceId string) (*FsResource, error) { - var resp FsResource - result := p.db.Table(p.name).WithContext(ctx).Where("resource_id =?", resourceId).Take(&resp) - if result.Error != nil { - // 检查 ErrRecordNotFound 错误 - if !errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, result.Error - } - } - return &resp, nil +func (p *FsResourceModel) FindOneById(ctx context.Context, resourceId string) (resp *FsResource, err error) { + err = p.db.Table(p.name).WithContext(ctx).Where("resource_id =?", resourceId).Take(&resp).Error + return resp, err } func (p *FsResourceModel) Create(ctx context.Context, req *FsResource) (resp *FsResource, err error) { diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index d5f79cbf..b00cc014 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -48,6 +48,27 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { if !ok { return errors.New("allmodels is nil!!") } + rabbitmq := initalize.RabbitMqHandle{} + //查询有没有缓存的资源 + resource, err := allmodels.FsResource.FindOneById(ctx, parseInfo.TaskId) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error("failed to find render resource:", err) + return err + } + } else { + //有数据则直接返回 + cacheData := websocket_data.RenderImageNotify{ + TaskId: parseInfo.TaskId, + Image: *resource.ResourceUrl, + } + d, _ := json.Marshal(cacheData) + if err = rabbitmq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil { + logx.Error("failed to send cache render resource to queue:RABBIT_MQ_RENDER_RESULT_DATA") + return err + } + return nil + } timeSearchBegin := time.Now().UnixMilli() //获取模板 templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) @@ -250,7 +271,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { "folder": "", //todo 千人千面需要使用 } b, _ := json.Marshal(sendData) - rabbitmq := initalize.RabbitMqHandle{} if err = rabbitmq.SendMsg(constants.RABBIT_MQ_TO_UNITY, b); err != nil { logx.Error("发送渲染组装数据到rabbitmq失败:", err) return err diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 52f25a61..c2d5fae0 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -85,7 +85,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp } defer conn.Close() //鉴权不成功10秒后断开 - var ( + /*var ( userInfo *auth.UserInfo isAuth bool ) @@ -102,10 +102,10 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //发送关闭信息 _ = conn.WriteMessage(websocket.CloseMessage, nil) return - } + }*/ //测试的目前写死 39 - /*var userInfo auth.UserInfo - userInfo.UserId = 39*/ + var userInfo auth.UserInfo + userInfo.UserId = 39 //生成连接唯一标识 uniqueId := websocketIdGenerator.Get() ws := wsConnectItem{ From 84dc60be493961260135e6bc09ad3216f4252aeb Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 15:46:57 +0800 Subject: [PATCH 06/15] fix --- utils/websocket_data/render_data.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index 18d272ed..1fd5fb35 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -14,9 +14,10 @@ type RenderImageReqMsg struct { type RenderData struct { TemplateTagId int64 `json:"template_tag_id"` //模板标签id ProductId int64 `json:"product_id"` //产品id - Data interface{} `json:"data"` //面片数据 - UserId int64 `json:"user_id"` //用户id - GuestId int64 `json:"guest_id"` //游客id + LogoId int64 `json:"logo_id"` + Data interface{} `json:"data"` //面片数据 + UserId int64 `json:"user_id"` //用户id + GuestId int64 `json:"guest_id"` //游客id } // websocket发送渲染完的数据 From 64536beae6b3466a5c71ad5b9cb3c74ae3eff714 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 16:06:40 +0800 Subject: [PATCH 07/15] fix --- .../render/consumer/assemble_render_data.go | 20 ---------------- .../internal/logic/datatransferlogic.go | 5 ++++ .../internal/logic/ws_render_image_logic.go | 23 ++++++++++++++++++- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index b00cc014..4cb1abe2 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -49,26 +49,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { return errors.New("allmodels is nil!!") } rabbitmq := initalize.RabbitMqHandle{} - //查询有没有缓存的资源 - resource, err := allmodels.FsResource.FindOneById(ctx, parseInfo.TaskId) - if err != nil { - if !errors.Is(err, gorm.ErrRecordNotFound) { - logx.Error("failed to find render resource:", err) - return err - } - } else { - //有数据则直接返回 - cacheData := websocket_data.RenderImageNotify{ - TaskId: parseInfo.TaskId, - Image: *resource.ResourceUrl, - } - d, _ := json.Marshal(cacheData) - if err = rabbitmq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil { - logx.Error("failed to send cache render resource to queue:RABBIT_MQ_RENDER_RESULT_DATA") - return err - } - return nil - } timeSearchBegin := time.Now().UnixMilli() //获取模板 templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index c2d5fae0..b386c83c 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fusenapi/constants" "fusenapi/initalize" + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/id_generator" "fusenapi/utils/websocket_data" @@ -64,7 +65,9 @@ var ( // 每个连接的连接基本属性 type wsConnectItem struct { conn *websocket.Conn //websocket的连接 + ctx context.Context rabbitMq *initalize.RabbitMqHandle + allModels *gmodel.AllModelsGen closeChan chan struct{} //ws连接关闭chan isClose bool //是否已经关闭 uniqueId uint64 //ws连接唯一标识 @@ -110,7 +113,9 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp uniqueId := websocketIdGenerator.Get() ws := wsConnectItem{ conn: conn, + ctx: l.ctx, rabbitMq: l.svcCtx.RabbitMq, + allModels: l.svcCtx.AllModels, uniqueId: uniqueId, closeChan: make(chan struct{}, 1), inChan: make(chan []byte, 1000), diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 225c63dd..d210c924 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -2,10 +2,12 @@ package logic import ( "encoding/json" + "errors" "fusenapi/constants" "fusenapi/utils/hash" "fusenapi/utils/websocket_data" "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" ) // 云渲染属性 @@ -33,8 +35,27 @@ func (w *wsConnectItem) renderImage(data []byte) { //用户id赋值 renderImageData.RenderData.UserId = w.userId renderImageData.RenderData.GuestId = w.guestId - //把需要渲染的图片任务加进去 + //生成任务id taskId := hash.JsonHashKey(renderImageData.RenderData) + //查询有没有缓存的资源,有就返回###################### + resource, err := w.allModels.FsResource.FindOneById(w.ctx, taskId) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error("failed to find render resource:", err) + return + } + } else { + //返回给客户端 + b := w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ + RenderId: renderImageData.RenderId, + Image: *resource.ResourceUrl, + }) + //发送数据到out chan + w.sendToOutChan(b) + return + } + //########################################### + //把需要渲染的图片任务加进去 w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 1, //0删除 1添加 TaskId: taskId, From c51c9f228cd55ef0eb17e71e03b9933e5943035c Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 16:08:58 +0800 Subject: [PATCH 08/15] fix --- utils/websocket_data/render_data.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index 1fd5fb35..dd6ab83c 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -16,8 +16,8 @@ type RenderData struct { ProductId int64 `json:"product_id"` //产品id LogoId int64 `json:"logo_id"` Data interface{} `json:"data"` //面片数据 - UserId int64 `json:"user_id"` //用户id - GuestId int64 `json:"guest_id"` //游客id + UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) + GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) } // websocket发送渲染完的数据 From 28b9d1ac1ff16f682815f390e9557ced7b1789ce Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 16:58:08 +0800 Subject: [PATCH 09/15] fix --- .../render/consumer/assemble_render_data.go | 122 +----------------- utils/websocket_data/render_data.go | 11 +- 2 files changed, 8 insertions(+), 125 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 4cb1abe2..d1c21d30 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -11,11 +11,7 @@ import ( "fusenapi/utils/websocket_data" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" - "io/ioutil" - "net/http" "strconv" - "strings" - "time" ) // 这里请求的py接口返回数据 @@ -49,7 +45,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { return errors.New("allmodels is nil!!") } rabbitmq := initalize.RabbitMqHandle{} - timeSearchBegin := time.Now().UnixMilli() //获取模板 templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) if err != nil { @@ -60,100 +55,8 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("failed to get template info:", err) return err } - renderLogTime := time.Now().UnixMilli() - timeSearchBegin - now := time.Now().Unix() - title := "1-组装模板数据" - //云渲染日志 - err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{ - UserId: &parseInfo.RenderData.UserId, - GuestId: &parseInfo.RenderData.GuestId, - Title: &title, - Time: &renderLogTime, - Tag: &parseInfo.RenderId, - Ctime: &now, - }) - if err != nil { - logx.Error(err) - } - pyapiBeginTime := time.Now().UnixMilli() - //这里curl post请求数据。获取处理好的贴图数据,用于贴model的贴图 - pythonPostData := map[string]interface{}{ - "tids": []int64{templateInfo.Id}, - "data": parseInfo.RenderData.Data, - } - pyPostBytes, _ := json.Marshal(pythonPostData) - //post 数据结构 - a := `{"tids":[128,431],"data":[{"id":"9d35ac5a-81a0-3caf-3246-cdbea9f2ddfe","tag":"MainColor","title":"\u8d34\u56fe2","type":"color","text":"","fill":"#c028b9","fontSize":20,"fontFamily":"Aqum2SmallCaps3","ifBr":false,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":1024,"height":1024,"x":0,"y":0,"opacity":1,"optionalColor":[{"color":"#000000","name":"Black","default":true}],"zIndex":2,"svgPath":"","follow":{"fill":"","ifShow":"","content":""},"group":[],"cameraStand":{"x":0,"y":0,"z":0},"proportion":60,"materialName":"","materialTime":""},{"id":"c9be653f-dfc1-5659-1eb8-7ab128abe3d5","tag":"Logo","title":"\u8d34\u56fe4","type":"image","text":"","fill":"#c028b9","fontSize":65,"fontFamily":"MontserratBold3","ifBr":true,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":312,"height":144.8044172010362,"x":99,"y":406.49999999999875,"opacity":1,"optionalColor":[{"color":"#000000","name":"Black","default":false},{"color":"#FFFFFF","name":"White","default":false},{"name":"MainColor","color":"#c028b9","default":true}],"zIndex":3,"svgPath":"","follow":{"fill":"","ifShow":"","content":""},"group":[],"cameraStand":{"x":0,"y":0,"z":0},"proportion":60,"materialTime":"","materialName":""},{"id":"e3269e77-b8c2-baec-bb9b-8399915a711c","tag":"Slogan","title":"\u8d34\u56fe5","type":"text","text":"","fill":"","fontSize":16,"fontFamily":"MontserratBold3","ifBr":false,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":312,"height":18.999939381668568,"x":99,"y":605.0000538829611,"opacity":1,"optionalColor":[{"color":"#000000","name":"Black","default":true}],"zIndex":4,"svgPath":"","follow":{"fill":"bfc2b5a3-10af-c95b-fbf1-3016540fffad","ifShow":"","content":""},"group":[],"cameraStand":{"x":0,"y":0,"z":36},"proportion":60,"materialName":"","materialTime":""},{"id":"bfc2b5a3-10af-c95b-fbf1-3016540fffad","tag":"SecondaryColor","title":"\u8d34\u56fe9","type":"color","text":"","fill":"#FFFFFF","fontSize":20,"fontFamily":"Aqum2SmallCaps3","ifBr":false,"ifShow":true,"ifGroup":false,"maxNum":50,"rotation":0,"align":"center","verticalAlign":"middle","material":"","width":1024,"height":1024,"x":0,"y":0,"opacity":1,"optionalColor":[{"color":"#000000","name":"Black","default":true}],"zIndex":1,"svgPath":"","follow":{"fill":"","ifShow":"","content":""},"group":[],"cameraStand":{"x":0,"y":0,"z":0},"proportion":60}]}` - url := "http://110.41.19.98:8867/imgRender" - pyRsp, err := http.Post(url, "application/json;charset=UTF-8", strings.NewReader(a)) - if err != nil { - logx.Error("request python render api err:", err) - return err - } - defer pyRsp.Body.Close() - pyRspBytes, err := ioutil.ReadAll(pyRsp.Body) - if err != nil { - logx.Error("failed to read python api rsp body,err=", err) - return err - } - var rspInfo pythonApiRsp - if err = json.Unmarshal(pyRspBytes, &rspInfo); err != nil { - logx.Error("failed to unmarshal python api rsp:", err) - return err - } - if rspInfo.Code != 200 { - logx.Error("python api 接口请求错误:", rspInfo.Msg) - return err - } - if len(rspInfo.Data) == 0 { - logx.Error("python api 接口没有数据:") - return err - } - mapImageData := make(map[int64]int) - for k, v := range rspInfo.Data { - mapImageData[v.Tid] = k - } - //云渲染日志 - title = "2-请求->接收python合成刀版图接口" - now = time.Now().Unix() - pyRequestTime := time.Now().UnixMilli() - pyapiBeginTime - err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{ - UserId: &parseInfo.RenderData.UserId, - GuestId: &parseInfo.RenderData.GuestId, - Title: &title, - Time: &pyRequestTime, - Tag: &parseInfo.RenderId, - Ctime: &now, - }) - if err != nil { - logx.Error(err) - } - incTime := int64(0) - mapCurlData := make(map[int64]int) - for k, v := range rspInfo.Data { - mapCurlData[v.Tid] = k - incTime += v.Costtime - } - //云渲染日志 - title = "3-python合成刀版图" - now = time.Now().Unix() - postData := string(pyPostBytes) - pyRspStr := string(pyRspBytes) - err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{ - UserId: &parseInfo.RenderData.UserId, - GuestId: &parseInfo.RenderData.GuestId, - PostUrl: &url, - PostData: &postData, - Result: &pyRspStr, - Title: &title, - Time: &incTime, - Tag: &parseInfo.RenderId, - Ctime: &now, - }) - if err != nil { - logx.Error(err) - } - timePinjieBegin := time.Now().UnixMilli() + // todo curl请求python获取刀版图 baseImage###### + baseImage := "" //获取渲染设置信息 element, err := allmodels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId) if err != nil { @@ -177,10 +80,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { return err } } - baseImage := "" - if index, ok := mapImageData[templateInfo.Id]; ok { - baseImage = constants.H5_URL + "/storage" + rspInfo.Data[index].Imgurl - } tempData := make([]map[string]interface{}, 0, 3) if element.Base != nil && *element.Base != "" { tempData = append(tempData, map[string]interface{}{ @@ -224,21 +123,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { "data": tempData, }, } - timePinjie := time.Now().UnixMilli() - timePinjieBegin - //云渲染日志 - title = "接收到python刀版图 -> 3-组装MQ渲染任务队列" - now = time.Now().Unix() - err = allmodels.FsCloudRenderLog.Create(ctx, &gmodel.FsCloudRenderLog{ - UserId: &parseInfo.RenderData.UserId, - GuestId: &parseInfo.RenderData.GuestId, - Title: &title, - Time: &timePinjie, - Tag: &parseInfo.RenderId, - Ctime: &now, - }) - if err != nil { - logx.Error(err) - } sendData := map[string]interface{}{ "id": parseInfo.TaskId, "order_id": 0, @@ -255,6 +139,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("发送渲染组装数据到rabbitmq失败:", err) return err } - logx.Info("发送渲染组装数据到rabbitmq 成功") + logx.Info("发送渲染组装数据到unity成功") return nil } diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index dd6ab83c..282b8c75 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -12,12 +12,11 @@ type RenderImageReqMsg struct { RenderData RenderData `json:"render_data"` } type RenderData struct { - TemplateTagId int64 `json:"template_tag_id"` //模板标签id - ProductId int64 `json:"product_id"` //产品id - LogoId int64 `json:"logo_id"` - Data interface{} `json:"data"` //面片数据 - UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) - GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + ProductId int64 `json:"product_id"` //产品id + LogoId int64 `json:"logo_id"` + UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) + GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) } // websocket发送渲染完的数据 From 731cf006a6994c31af9e9edaca94d0e91560f14f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 17:08:50 +0800 Subject: [PATCH 10/15] fix --- utils/websocket_data/render_data.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index 282b8c75..25fc9dfc 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -12,11 +12,11 @@ type RenderImageReqMsg struct { RenderData RenderData `json:"render_data"` } type RenderData struct { - TemplateTagId int64 `json:"template_tag_id"` //模板标签id - ProductId int64 `json:"product_id"` //产品id - LogoId int64 `json:"logo_id"` - UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) - GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + ProductId int64 `json:"product_id"` //产品id + Logo string `json:"logo"` + UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) + GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) } // websocket发送渲染完的数据 From 3f73edae00b106b9bfb47f2f414ff17d47e9a58b Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 17:11:45 +0800 Subject: [PATCH 11/15] fix --- server/render/consumer/assemble_render_data.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index d1c21d30..5463ef4a 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -45,7 +45,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { return errors.New("allmodels is nil!!") } rabbitmq := initalize.RabbitMqHandle{} - //获取模板 + //获取模板(产品第一个sku的模板) templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -55,7 +55,10 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("failed to get template info:", err) return err } + // todo 获取sku对应用来合成刀版图的json数据 + // todo curl请求python获取刀版图 baseImage###### + baseImage := "" //获取渲染设置信息 element, err := allmodels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId) From c004b06602dc4b186817b912aa555849886e267a Mon Sep 17 00:00:00 2001 From: Hiven Date: Wed, 9 Aug 2023 18:05:39 +0800 Subject: [PATCH 12/15] =?UTF-8?q?fix:=E4=B8=8A=E4=BC=A0logo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/upload/etc/upload.yaml | 2 +- server/upload/internal/logic/uploadlogologic.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index 3c4a9efd..5a8a5cb6 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -15,4 +15,4 @@ AWS: Token: BLMService: ImageProcess: - Url: "http://192.168.1.8:45678/FeatureExtraction" + Url: "http://192.168.1.7:45678/FeatureExtraction" diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go index 28551b04..579ef53c 100644 --- a/server/upload/internal/logic/uploadlogologic.go +++ b/server/upload/internal/logic/uploadlogologic.go @@ -100,6 +100,10 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us } resultStr = string(b) + if resultStr == "Internal Server Error" { + return resp.SetStatus(basic.CodeFileUploadLogoErr, resultStr) + } + var module = "logo" var nowTime = time.Now().Unix() // 新增记录 From ea0804ad63e2e6f0c5f944d92ec09113e2b6a624 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 18:09:16 +0800 Subject: [PATCH 13/15] fix --- .../internal/logic/ws_render_image_logic.go | 31 ++++++++++++++++- utils/curl/http_curl.go | 34 +++++++++++++++++++ utils/websocket_data/render_data.go | 6 ++-- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 utils/curl/http_curl.go diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index d210c924..65eee011 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -25,16 +25,45 @@ type renderImageControlChanItem struct { // 渲染发送到组装数据组装数据 func (w *wsConnectItem) renderImage(data []byte) { - var renderImageData websocket_data.AssembleRenderData + var renderImageData websocket_data.RenderImageReqMsg if err := json.Unmarshal(data, &renderImageData); err != nil { w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)) logx.Error("invalid format of websocket render image message", err) return } logx.Info("收到请求云渲染图片数据:", renderImageData) + if renderImageData.RenderId == "" { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:render_id is empty") + logx.Error("invalid format of websocket render image message:render_id is empty") + return + } + if renderImageData.RenderData.ProductId <= 0 { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:product_id ") + logx.Error("invalid format of websocket render image message:product_id") + return + } + if renderImageData.RenderData.TemplateTagId <= 0 { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:template_tag_id ") + logx.Error("invalid format of websocket render image message:template_tag_id") + return + } + //获取上传最近的logo + userMaterial, err := w.allModels.FsUserMaterial.FindLatestOne(w.ctx, w.userId, w.guestId) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "failed to get user logo") + logx.Error("failed to get user logo") + return + } + //使用默认logo(写死一个默认) + renderImageData.RenderData.Logo = "https://s3.us-west-1.amazonaws.com/storage.fusenpack.com/f5ccd11365099fa47a6316b1cd639f6dd6064dcd2d37c8d2fcd0a322160b33cc" + } else { + renderImageData.RenderData.Logo = *userMaterial.ResourceUrl + } //用户id赋值 renderImageData.RenderData.UserId = w.userId renderImageData.RenderData.GuestId = w.guestId + //生成任务id taskId := hash.JsonHashKey(renderImageData.RenderData) //查询有没有缓存的资源,有就返回###################### diff --git a/utils/curl/http_curl.go b/utils/curl/http_curl.go new file mode 100644 index 00000000..8ec5a05b --- /dev/null +++ b/utils/curl/http_curl.go @@ -0,0 +1,34 @@ +package curl + +import ( + "bytes" + "errors" + "net/http" + "strings" + "time" +) + +// 接口请求 +func ApiCall(url, method string, header map[string]string, postData []byte, timeOut time.Duration) (rsp *http.Response, err error) { + method = strings.ToUpper(method) + if method != "GET" && method != "POST" && method != "PUT" && method != "DELETE" { + return nil, errors.New("invalid http method") + } + if url == "" { + return nil, errors.New("request url can`t be empty") + } + client := &http.Client{} + if timeOut <= 0 { + client.Timeout = time.Second * 15 + } else { + client.Timeout = timeOut + } + requestHandle, err := http.NewRequest(method, url, bytes.NewReader(postData)) + if err != nil { + return nil, err + } + for k, v := range header { + requestHandle.Header.Set(k, v) + } + return client.Do(requestHandle) +} diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index 25fc9dfc..928d1005 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -14,9 +14,9 @@ type RenderImageReqMsg struct { type RenderData struct { TemplateTagId int64 `json:"template_tag_id"` //模板标签id ProductId int64 `json:"product_id"` //产品id - Logo string `json:"logo"` - UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) - GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) + Logo string `json:"logo"` //log资源地址(websocket连接建立再赋值) + UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值) + GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值) } // websocket发送渲染完的数据 From 5470f22e77c59372bcbb5fb840975dbb3f2ee6a5 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 9 Aug 2023 19:34:11 +0800 Subject: [PATCH 14/15] fix --- .../render/consumer/assemble_render_data.go | 98 +++++++++++++++---- server/render/render.go | 2 +- utils/curl/http_curl.go | 6 +- 3 files changed, 82 insertions(+), 24 deletions(-) diff --git a/server/render/consumer/assemble_render_data.go b/server/render/consumer/assemble_render_data.go index 5463ef4a..a3fc6ef0 100644 --- a/server/render/consumer/assemble_render_data.go +++ b/server/render/consumer/assemble_render_data.go @@ -7,22 +7,23 @@ import ( "fmt" "fusenapi/constants" "fusenapi/initalize" - "fusenapi/model/gmodel" + "fusenapi/server/render/internal/svc" + "fusenapi/utils/curl" + "fusenapi/utils/file" + "fusenapi/utils/hash" "fusenapi/utils/websocket_data" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" + "io/ioutil" "strconv" + "strings" ) // 这里请求的py接口返回数据 type pythonApiRsp struct { - Code int `json:"code"` - Msg string `json:"msg"` - Data []struct { - Tid int64 `json:"tid"` - Imgurl string `json:"imgurl"` - Costtime int64 `json:"costtime"` - } `json:"data"` + Id string `json:"id"` //物料模板的id + LogoUrl string `json:"logo_url"` //logo地址 + result string `json:"result"` //图片base64 } // 消费渲染需要组装的数据 @@ -36,17 +37,17 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("MqConsumerRenderAssemble数据格式错误:", err) return nil //不返回错误就删除消息 } - val := ctx.Value("allmodels") + val := ctx.Value("svcctx") if val == nil { - return errors.New("allmodels is nil") + return errors.New("svcctx is nil") } - allmodels, ok := val.(*gmodel.AllModelsGen) + svcCtx, ok := val.(*svc.ServiceContext) if !ok { - return errors.New("allmodels is nil!!") + return errors.New("svcctx is nil!!") } rabbitmq := initalize.RabbitMqHandle{} //获取模板(产品第一个sku的模板) - templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) + templateInfo, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logx.Error("template info is not found") @@ -55,13 +56,25 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Error("failed to get template info:", err) return err } - // todo 获取sku对应用来合成刀版图的json数据 - - // todo curl请求python获取刀版图 baseImage###### - - baseImage := "" + combineHash := hash.JsonHashKey(parseInfo) //区别于云渲染的taskid,这个用获取刀版图缓存 + //获取该hash值下有没有对应的资源 + resource, err := svcCtx.AllModels.FsResource.FindOneById(ctx, combineHash) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error("failed to get resource :", err) + return err + } + combineImage := "" //刀版图 + //如果不存在,则请求生成刀版图 + if errors.Is(err, gorm.ErrRecordNotFound) { + combineImage, err = getCombineImage(ctx, svcCtx, parseInfo, combineHash) + if err != nil { + return err + } + } else { + combineImage = *resource.ResourceUrl + } //获取渲染设置信息 - element, err := allmodels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId) + element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *templateInfo.ModelId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logx.Error("element info is not found,model_id = ?", *templateInfo.ModelId) @@ -87,7 +100,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { if element.Base != nil && *element.Base != "" { tempData = append(tempData, map[string]interface{}{ "name": "model", - "data": "0," + baseImage + "," + *element.Base, + "data": "0," + combineImage + "," + *element.Base, "type": "other", "layer": "0", "is_update": 1, @@ -145,3 +158,48 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error { logx.Info("发送渲染组装数据到unity成功") return nil } + +// 获取刀版图 +func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo websocket_data.AssembleRenderData, combineHash string) (image string, err error) { + // todo 获取sku对应用来合成刀版图的json数据 + + url := "http://192.168.1.7:45678/LogoCombine" + header := make(map[string]string) + header["content-type"] = "application/json" + postData := "" // todo 请求数据要查出来 + httpRsp, err := curl.ApiCall(url, "POST", header, strings.NewReader(postData), 20) + if err != nil { + logx.Error("failed to combine logo:", err) + return "", err + } + defer httpRsp.Body.Close() + bytes, err := ioutil.ReadAll(httpRsp.Body) + if err != nil { + logx.Error("failed to read python api rsp body:", err) + return "", err + } + var pythonApiInfo pythonApiRsp + if err = json.Unmarshal(bytes, &pythonApiInfo); err != nil { + logx.Error("failed to parse python api rsp:", err) + return "", err + } + //上传刀版图 + var upload = file.Upload{ + Ctx: ctx, + MysqlConn: svcCtx.MysqlConn, + AwsSession: svcCtx.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + FileHash: combineHash, + FileData: pythonApiInfo.result, + UploadBucket: 1, + ApiType: 2, + UserId: parseInfo.RenderData.UserId, + GuestId: parseInfo.RenderData.GuestId, + }) + if err != nil { + logx.Error("上传刀版图到s3失败:", err) + return "", err + } + return uploadRes.ResourceUrl, nil +} diff --git a/server/render/render.go b/server/render/render.go index acb9d019..f3cdafef 100644 --- a/server/render/render.go +++ b/server/render/render.go @@ -35,7 +35,7 @@ func main() { //消费渲染前组装数据队列 ctx1 := context.Background() ctx2, cancel := context.WithCancel(ctx1) - ctx2 = context.WithValue(ctx2, "allmodels", ctx.AllModels) + ctx2 = context.WithValue(ctx2, "svcctx", ctx) defer cancel() go ctx.RabbitMq.Consume(ctx2, constants.RABBIT_MQ_ASSEMBLE_RENDER_DATA, &consumer.MqConsumerRenderAssemble{}) handler.RegisterHandlers(server, ctx) diff --git a/utils/curl/http_curl.go b/utils/curl/http_curl.go index 8ec5a05b..0fbef756 100644 --- a/utils/curl/http_curl.go +++ b/utils/curl/http_curl.go @@ -1,15 +1,15 @@ package curl import ( - "bytes" "errors" + "io" "net/http" "strings" "time" ) // 接口请求 -func ApiCall(url, method string, header map[string]string, postData []byte, timeOut time.Duration) (rsp *http.Response, err error) { +func ApiCall(url, method string, header map[string]string, body io.Reader, timeOut time.Duration) (rsp *http.Response, err error) { method = strings.ToUpper(method) if method != "GET" && method != "POST" && method != "PUT" && method != "DELETE" { return nil, errors.New("invalid http method") @@ -23,7 +23,7 @@ func ApiCall(url, method string, header map[string]string, postData []byte, time } else { client.Timeout = timeOut } - requestHandle, err := http.NewRequest(method, url, bytes.NewReader(postData)) + requestHandle, err := http.NewRequest(method, url, body) if err != nil { return nil, err } From 104a9db7eadc42394f3028e9f0b86f928af5156d Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 10 Aug 2023 10:19:37 +0800 Subject: [PATCH 15/15] fix --- server/product/internal/logic/getpricebypidlogic.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/product/internal/logic/getpricebypidlogic.go b/server/product/internal/logic/getpricebypidlogic.go index 44bfe67a..ea18202f 100644 --- a/server/product/internal/logic/getpricebypidlogic.go +++ b/server/product/internal/logic/getpricebypidlogic.go @@ -116,7 +116,12 @@ func (l *GetPriceByPidLogic) dealWithStepRange(stepNumSlice, stepPriceSlice []in end := int64(0) if numKey == 0 { //第一个 begin = *priceInfo.MinBuyNum * (*priceInfo.EachBoxNum) - end = num - 1 + //只有一阶价格 + if lenStepPrice == 1 { + end = -1 + } else { + end = num - 1 + } } else if numKey < lenStepNum-1 { //中间的 nextNum := int64(stepNumSlice[numKey+1]) * (*priceInfo.EachBoxNum) begin = num