113 lines
3.6 KiB
Go
113 lines
3.6 KiB
Go
package logic
|
||
|
||
//云渲染回调
|
||
import (
|
||
"context"
|
||
"fusenapi/server/websocket/internal/svc"
|
||
"fusenapi/server/websocket/internal/types"
|
||
"fusenapi/utils/auth"
|
||
"fusenapi/utils/basic"
|
||
"fusenapi/utils/file"
|
||
"strings"
|
||
"time"
|
||
|
||
"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)
|
||
// }
|
||
|
||
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||
logx.Info("=====收到unity返回的渲染结果消息======")
|
||
req.TaskId = strings.Trim(req.TaskId, " ")
|
||
if req.TaskId == "" {
|
||
logx.Error("渲染回调参数错误:任务标识")
|
||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
|
||
}
|
||
if req.Image == "" {
|
||
logx.Error("渲染回调参数错误:渲染结果图片数据")
|
||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
|
||
}
|
||
unityRenderEndTime := time.Now().UTC().UnixMilli()
|
||
//解析出真的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]
|
||
//存base64打印测试
|
||
/* f, _ := os.Create("b.txt")
|
||
defer f.Close()
|
||
f.WriteString(req.Image)*/
|
||
// 上传文件
|
||
var upload = file.Upload{
|
||
Ctx: l.ctx,
|
||
MysqlConn: l.svcCtx.MysqlConn,
|
||
AwsSession: l.svcCtx.AwsSession,
|
||
}
|
||
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||
Source: "unity cloud render",
|
||
FileHash: req.TaskId,
|
||
FileData: req.Image,
|
||
Metadata: "",
|
||
UploadBucket: 1,
|
||
ApiType: 2,
|
||
UserId: req.UserId,
|
||
GuestId: req.GuestId,
|
||
FileByte: nil,
|
||
})
|
||
if err != nil {
|
||
logx.Error("渲染回调上传文件失败:", err)
|
||
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image")
|
||
}
|
||
uploadUnityRenderImageTakesTime := time.Now().UTC().UnixMilli() - unityRenderEndTime
|
||
if value, ok := mapConnPool.Load(wid); ok {
|
||
//断言连接
|
||
ws, ok := value.(wsConnectItem)
|
||
if !ok {
|
||
logx.Error("渲染回调断言websocket连接失败")
|
||
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "渲染回调断言websocket连接失败")
|
||
}
|
||
//记录收到unity渲染结果时间以及上传渲染图耗时时间
|
||
ws.modifyRenderTaskProperty(renderImageControlChanItem{
|
||
option: 2,
|
||
taskId: req.TaskId,
|
||
taskProperty: renderTask{
|
||
unityRenderEndTime: unityRenderEndTime,
|
||
uploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime,
|
||
},
|
||
})
|
||
//发送处理并删除任务
|
||
ws.deleteRenderTask(renderImageControlChanItem{
|
||
option: 0, //0删除 1添加 2修改耗时属性
|
||
taskId: req.TaskId,
|
||
renderNotifyImageUrl: uploadRes.ResourceUrl,
|
||
})
|
||
}
|
||
logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl)
|
||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||
}
|