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

134 lines
4.7 KiB
Go
Raw Normal View History

2023-07-26 06:35:29 +00:00
package logic
import (
"encoding/json"
2023-08-09 08:06:40 +00:00
"errors"
2023-07-27 09:33:50 +00:00
"fusenapi/constants"
2023-08-07 02:48:14 +00:00
"fusenapi/utils/hash"
2023-08-07 03:25:06 +00:00
"fusenapi/utils/websocket_data"
2023-08-08 06:20:57 +00:00
2023-07-26 06:35:29 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-08-09 08:06:40 +00:00
"gorm.io/gorm"
2023-07-26 06:35:29 +00:00
)
2023-07-26 09:06:53 +00:00
// 云渲染属性
type renderProperty struct {
2023-08-07 02:48:14 +00:00
renderImageTask map[string]string //需要渲染的图片任务 key是taskId val 是renderId
2023-07-26 09:06:53 +00:00
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道
}
// 渲染任务新增移除的控制通道的数据
type renderImageControlChanItem struct {
2023-08-07 02:48:14 +00:00
Option int // 0删除 1添加
TaskId string //map的key
RenderId string // map的val
2023-07-27 09:41:36 +00:00
}
2023-08-07 03:15:51 +00:00
// 渲染发送到组装数据组装数据
2023-08-07 05:17:23 +00:00
func (w *wsConnectItem) renderImage(data []byte) {
2023-08-09 10:09:16 +00:00
var renderImageData websocket_data.RenderImageReqMsg
2023-07-27 09:41:36 +00:00
if err := json.Unmarshal(data, &renderImageData); err != nil {
2023-08-15 07:09:53 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)))
2023-07-27 09:41:36 +00:00
logx.Error("invalid format of websocket render image message", err)
return
}
2023-07-26 06:35:29 +00:00
logx.Info("收到请求云渲染图片数据:", renderImageData)
2023-08-09 10:09:16 +00:00
if renderImageData.RenderId == "" {
2023-08-15 07:09:53 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:render_id is empty"))
2023-08-09 10:09:16 +00:00
logx.Error("invalid format of websocket render image message:render_id is empty")
return
}
if renderImageData.RenderData.ProductId <= 0 {
2023-08-15 07:09:53 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:product_id "))
2023-08-09 10:09:16 +00:00
logx.Error("invalid format of websocket render image message:product_id")
return
}
2023-08-15 03:18:22 +00:00
if renderImageData.RenderData.TemplateTag == "" {
2023-08-15 07:09:53 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:template_tag "))
2023-08-15 03:18:22 +00:00
logx.Error("invalid format of websocket render image message:template_tag")
2023-08-09 10:09:16 +00:00
return
}
//获取上传最近的logo
userMaterial, err := w.allModels.FsUserMaterial.FindLatestOne(w.ctx, w.userId, w.guestId)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-15 07:09:53 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "failed to get user logo"))
2023-08-09 10:09:16 +00:00
logx.Error("failed to get user logo")
return
}
2023-08-15 07:09:53 +00:00
//使用默认logo(id=0)
userMaterialDefault, err := w.allModels.FsUserMaterial.FindOneById(w.ctx, 0)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "default logo is not exists"))
return
}
logx.Error("default logo is not exists")
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "failed to get default logo"))
return
}
renderImageData.RenderData.Logo = *userMaterialDefault.ResourceUrl
2023-08-09 10:09:16 +00:00
} else {
renderImageData.RenderData.Logo = *userMaterial.ResourceUrl
}
2023-08-09 04:28:06 +00:00
//用户id赋值
2023-08-08 04:22:15 +00:00
renderImageData.RenderData.UserId = w.userId
2023-08-09 04:28:06 +00:00
renderImageData.RenderData.GuestId = w.guestId
2023-08-09 10:09:16 +00:00
2023-08-09 08:06:40 +00:00
//生成任务id
2023-08-07 03:15:51 +00:00
taskId := hash.JsonHashKey(renderImageData.RenderData)
2023-08-09 08:06:40 +00:00
//查询有没有缓存的资源,有就返回######################
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
}
//###########################################
//把需要渲染的图片任务加进去
2023-08-07 03:15:51 +00:00
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Option: 1, //0删除 1添加
TaskId: taskId,
RenderId: renderImageData.RenderId,
}
2023-08-07 03:49:17 +00:00
tmpData := websocket_data.AssembleRenderData{
2023-08-07 03:15:51 +00:00
TaskId: taskId,
2023-08-08 04:22:15 +00:00
RenderId: renderImageData.RenderId,
2023-08-07 03:15:51 +00:00
RenderData: renderImageData.RenderData,
}
d, _ := json.Marshal(tmpData)
//发送给对应的流水线组装数据
2023-08-15 09:36:17 +00:00
if err = w.rabbitMq.SendMsg(constants.RABBIT_MQ_ASSEMBLE_RENDER_DATA, d); err != nil {
2023-08-10 10:12:07 +00:00
logx.Error("发送渲染任务数据到MQ失败:", string(d), "err:", err)
2023-08-07 02:48:14 +00:00
return
}
2023-08-10 10:12:07 +00:00
logx.Info("发送渲染数据到rabbitmq成功:", string(d))
2023-08-07 02:48:14 +00:00
}
// 操作连接中渲染任务的增加/删除
func (w *wsConnectItem) operationRenderTask() {
for {
2023-07-27 07:46:07 +00:00
select {
2023-08-07 02:48:14 +00:00
case <-w.closeChan:
2023-07-27 07:46:07 +00:00
return
2023-08-07 02:48:14 +00:00
case data := <-w.renderProperty.renderImageTaskCtlChan:
switch data.Option {
case 0: //删除任务
delete(w.renderProperty.renderImageTask, data.TaskId)
case 1: //新增任务
w.renderProperty.renderImageTask[data.TaskId] = data.RenderId
2023-07-28 09:15:37 +00:00
}
2023-07-26 07:01:59 +00:00
}
2023-07-26 06:35:29 +00:00
}
}