fusenapi/server/websocket/internal/logic/rendernotifylogic.go

113 lines
3.0 KiB
Go
Raw Normal View History

2023-07-25 09:10:50 +00:00
package logic
import (
2023-07-26 03:53:06 +00:00
"fusenapi/constants"
2023-08-07 04:31:31 +00:00
"fusenapi/utils/auth"
2023-07-25 09:10:50 +00:00
"fusenapi/utils/basic"
2023-08-09 06:49:15 +00:00
"fusenapi/utils/file"
2023-08-07 03:25:06 +00:00
"fusenapi/utils/websocket_data"
2023-08-17 04:14:38 +00:00
"time"
2023-07-25 09:10:50 +00:00
2023-08-16 04:16:23 +00:00
"context"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
2023-07-25 09:10:50 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type RenderNotifyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
return &RenderNotifyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
2023-08-07 04:31:31 +00:00
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-08-17 08:11:09 +00:00
logx.Info("收到渲染回调数据:", *req)
2023-08-15 09:10:43 +00:00
if req.TaskId == "" {
2023-08-07 06:16:09 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
}
2023-08-15 09:10:43 +00:00
if req.Image == "" {
2023-08-07 06:16:09 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
2023-07-26 03:53:06 +00:00
}
2023-08-15 09:10:43 +00:00
if req.UserId == 0 && req.GuestId == 0 {
2023-08-09 06:12:58 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid user_id or guest_id")
}
2023-08-09 06:49:15 +00:00
// 上传文件
var upload = file.Upload{
Ctx: l.ctx,
MysqlConn: l.svcCtx.MysqlConn,
AwsSession: l.svcCtx.AwsSession,
}
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
2023-08-17 03:36:41 +00:00
Source: "unity cloud render",
2023-08-15 09:10:43 +00:00
FileHash: req.TaskId,
FileData: req.Image,
2023-08-17 03:36:41 +00:00
Metadata: "",
2023-08-09 06:49:15 +00:00
UploadBucket: 1,
ApiType: 2,
2023-08-15 09:10:43 +00:00
UserId: req.UserId,
GuestId: req.GuestId,
2023-08-17 03:36:41 +00:00
FileByte: nil,
2023-08-09 06:49:15 +00:00
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image")
}
2023-08-16 04:16:23 +00:00
//遍历websocket链接把数据传进去
mapConnPool.Range(func(key, value any) bool {
//断言连接
ws, ok := value.(wsConnectItem)
if !ok {
return true
}
//关闭标识
if ws.isClose {
return true
}
//查询有无该渲染任务
renderId, ok := ws.renderProperty.renderImageTask[req.TaskId]
if !ok {
return true
}
b := ws.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{
RenderId: renderId,
Image: uploadRes.ResourceUrl,
})
2023-08-17 04:14:38 +00:00
deleteTask := renderImageControlChanItem{
2023-08-16 04:16:23 +00:00
Option: 0, //0删除 1添加
TaskId: req.TaskId,
RenderId: renderId,
}
2023-08-17 04:14:38 +00:00
select {
case <-ws.closeChan: //关闭了
return true
case ws.renderProperty.renderImageTaskCtlChan <- deleteTask: //删除对应的需要渲染的图片map
//发送数据到out chan
ws.sendToOutChan(b)
case <-time.After(time.Second * 3): //超时丢弃
return true
}
2023-08-16 04:16:23 +00:00
return true
})
2023-08-16 09:36:25 +00:00
logx.Info("渲染回调成功######################")
2023-08-15 09:10:43 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success")
2023-07-25 09:10:50 +00:00
}