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

102 lines
3.4 KiB
Go
Raw Normal View History

2023-07-25 09:10:50 +00:00
package logic
2023-08-23 07:08:45 +00:00
//云渲染回调
2023-07-25 09:10:50 +00:00
import (
2023-08-21 05:19:12 +00:00
"context"
2023-08-21 08:19:30 +00:00
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
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-09-04 08:54:55 +00:00
"strings"
2023-08-21 08:19:30 +00:00
"time"
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-09-04 09:04:36 +00:00
logx.Info("=====收到unity返回的渲染结果消息======")
2023-09-04 08:54:55 +00:00
req.TaskId = strings.Trim(req.TaskId, " ")
2023-08-15 09:10:43 +00:00
if req.TaskId == "" {
2023-08-23 06:28:35 +00:00
logx.Error("渲染回调参数错误:任务标识")
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-23 06:28:35 +00:00
logx.Error("渲染回调参数错误:渲染结果图片数据")
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-25 03:06:32 +00:00
unityRenderEndTime := time.Now().UTC().UnixMilli()
2023-09-04 08:54:55 +00:00
//解析出真的taskId跟wid
splitSlice := strings.Split(req.TaskId, " ")
if len(splitSlice) != 2 {
logx.Error("渲染回调参数错误task_id拆分得不到合理结果")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
}
//重新赋值(很重要)
req.TaskId = splitSlice[0]
wid := splitSlice[1]
2023-08-21 08:19:30 +00:00
//存base64打印测试
/* f, _ := os.Create("b.txt")
defer f.Close()
f.WriteString(req.Image)*/
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 {
2023-08-17 08:16:27 +00:00
logx.Error("渲染回调上传文件失败:", err)
2023-08-09 06:49:15 +00:00
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image")
}
2023-08-25 03:06:32 +00:00
uploadUnityRenderImageTakesTime := time.Now().UTC().UnixMilli() - unityRenderEndTime
2023-09-04 08:54:55 +00:00
if value, ok := mapConnPool.Load(wid); ok {
2023-08-16 04:16:23 +00:00
//断言连接
ws, ok := value.(wsConnectItem)
if !ok {
2023-08-17 08:16:27 +00:00
logx.Error("渲染回调断言websocket连接失败")
2023-09-04 08:54:55 +00:00
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "渲染回调断言websocket连接失败")
2023-08-16 04:16:23 +00:00
}
2023-08-25 03:06:32 +00:00
//记录收到unity渲染结果时间以及上传渲染图耗时时间
2023-09-05 02:20:39 +00:00
ws.modifyRenderTaskProperty(req.TaskId, renderTask{unityRenderEndTime: unityRenderEndTime, uploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime})
2023-08-21 05:19:12 +00:00
//发送处理并删除任务
2023-09-05 02:20:39 +00:00
ws.deleteRenderTask(req.TaskId, uploadRes.ResourceUrl)
2023-09-04 08:54:55 +00:00
}
2023-08-21 06:46:39 +00:00
logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl)
2023-08-15 09:10:43 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success")
2023-07-25 09:10:50 +00:00
}