asset.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package repo
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "io/ioutil"
  7. "mats/conf"
  8. "mats/db/model"
  9. "net/http"
  10. "time"
  11. "go.mongodb.org/mongo-driver/bson"
  12. )
  13. type IRepoAsset interface {
  14. Upload(ctx *RepoSession, userId string) (interface{}, error)
  15. Update(ctx *RepoSession) (interface{}, error)
  16. Detail(ctx *RepoSession, id string) (interface{}, error)
  17. GetEntity() interface{}
  18. }
  19. func RepoAssetCreateDefaultValue(asset *conf.AppAsset) IRepoAsset {
  20. switch asset.Type {
  21. case "decorate":
  22. return &AssetDecorateMeshRepo{Conf: asset, Model: &model.AssetDecorateMesh{}}
  23. case "texture":
  24. return &AssetTextureRepo{Conf: asset, Model: &model.AssetTexture{}}
  25. case "env3d":
  26. return &AssetEnv3dRepo{Conf: asset, Model: &model.Env3d{}}
  27. case "boxtpl":
  28. return &BoxtplRepo{Conf: asset, Model: &model.Boxtpl{}}
  29. case "material":
  30. return &MaterialRepo{Conf: asset, Model: &model.AssetMaterial{}}
  31. }
  32. return nil
  33. }
  34. func CreateMeshConvTask(ctx *RepoSession, file *model.OssType, meshId string, collecton string, userId string) (interface{}, error) {
  35. taskConfig := &model.TaskConvMesh2Osgjs{
  36. CreateTime: time.Now(),
  37. FbxFile: file,
  38. OsgjsFieldName: "osgjs",
  39. MeshId: meshId,
  40. Collecton: collecton,
  41. UserId: userId,
  42. }
  43. taskId, err := RepoAddDoc(ctx, CollectionTaskMeshConv, taskConfig)
  44. if err != nil {
  45. return nil, err
  46. }
  47. //生成worker请求
  48. req := &model.WorkerRequest{WorkerType: "osgconverter", MainId: meshId, TaskCollection: CollectionTaskMeshConv, UserId: userId, Name: "模型转换", TaskId: taskId, CreateTime: time.Now()}
  49. return true, ReqestTaskWorker(ctx, req, CollectionTaskMeshConv)
  50. }
  51. func CreateShadowTask(ctx *RepoSession, file *model.OssType, meshId string, collecton string, userId string) (interface{}, error) {
  52. taskConfig := &model.TaskCreateShadow{
  53. CreateTime: time.Now(),
  54. FbxFile: file,
  55. ShadowFieldName: "shadow",
  56. MeshId: meshId,
  57. Collecton: collecton,
  58. UserId: userId,
  59. }
  60. taskId, err := RepoAddDoc(ctx, CollectionTaskShadowCreator, taskConfig)
  61. if err != nil {
  62. return nil, err
  63. }
  64. //生成worker请求
  65. req := &model.WorkerRequest{WorkerType: "shadowCreator", MainId: meshId, TaskCollection: CollectionTaskShadowCreator, UserId: userId, Name: "阴影生成", TaskId: taskId, CreateTime: time.Now()}
  66. return true, ReqestTaskWorker(ctx, req, CollectionTaskShadowCreator)
  67. }
  68. func CreateHdrConverterTask(ctx *RepoSession, hdr *model.OssType, env3dId string, collecton string, userId string) (interface{}, error) {
  69. taskConfig := &model.TaskConvHdr{
  70. CreateTime: time.Now(),
  71. HdrFile: hdr,
  72. Env3dId: env3dId,
  73. Collecton: collecton,
  74. UserId: userId,
  75. }
  76. taskId, err := RepoAddDoc(ctx, CollectionTaskHdrConv, taskConfig)
  77. if err != nil {
  78. return nil, err
  79. }
  80. //生成worker请求
  81. req := &model.WorkerRequest{WorkerType: "hdrconverter", TaskCollection: CollectionTaskHdrConv, UserId: userId, Name: "环境球转换", TaskId: taskId, CreateTime: time.Now()}
  82. return true, ReqestTaskWorker(ctx, req, CollectionTaskHdrConv)
  83. }
  84. func ReqestTaskWorker(ctx *RepoSession, task *model.WorkerRequest, taskCollection string) error {
  85. taskCenterHost := conf.AppConfig.TaskCenter
  86. if len(taskCenterHost) < 1 {
  87. return errors.New("任务中心没有配置,没法执行耗时任务")
  88. }
  89. client := &http.Client{}
  90. bytesData, _ := json.Marshal(task)
  91. req, err := http.NewRequest("POST", taskCenterHost+"/execTask", bytes.NewReader(bytesData))
  92. if err != nil {
  93. return err
  94. }
  95. resp, err := client.Do(req)
  96. if err != nil {
  97. return err
  98. }
  99. if resp.StatusCode != 200 {
  100. return errors.New("taskCenter 请求出错")
  101. }
  102. body, _ := ioutil.ReadAll(resp.Body)
  103. taskRep := &model.WorkerResponse{}
  104. err = json.Unmarshal(body, taskRep)
  105. if err != nil {
  106. return errors.New("解析WorkerResponse错误!")
  107. }
  108. if taskRep.ErrorNo != 200 {
  109. return errors.New(taskRep.ErrorDesc)
  110. }
  111. _, err = RepoUpdateSetDocProps(ctx, taskCollection, task.TaskId, bson.M{"$set": bson.M{"workingId": taskRep.Result.WorkingId}})
  112. return err
  113. }