service-hdr.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "path"
  11. "sku3dweb/db/model"
  12. "sku3dweb/db/repo"
  13. "strings"
  14. "time"
  15. "github.com/gin-gonic/gin"
  16. )
  17. //ServiceTestHttp JWT授权的http处理函数
  18. func ServiceConvHdr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  19. body := struct {
  20. Hdr string
  21. DocId string
  22. }{}
  23. err := c.ShouldBindJSON(&body)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if len(body.Hdr) < 1 {
  28. return nil, NewError("模型链接不能为空")
  29. }
  30. if len(body.DocId) < 1 {
  31. return nil, NewError("docId不能为空")
  32. }
  33. sDir, modelName := path.Split(body.Hdr)
  34. seg := strings.Split(sDir, "myhuaweicloud.com/")
  35. obsTargetPath := seg[len(seg)-1]
  36. // sDir = "assets/env3d/db"
  37. // obsTargetPath := fmt.Sprintf("%s/%s", sDir, body.DocId)
  38. wkspace := fmt.Sprintf("%d", time.Now().UnixNano())
  39. dir := fmt.Sprintf("uploads/%s", wkspace)
  40. err = os.MkdirAll(dir, 0777)
  41. if err != nil {
  42. return nil, NewError("创建目录失败!")
  43. }
  44. //1下载文件到本地
  45. localHdrpath := fmt.Sprintf("%s/%s", dir, modelName)
  46. out, err := os.Create(localHdrpath)
  47. if err != nil {
  48. os.RemoveAll(dir)
  49. return nil, err
  50. }
  51. hdrUrl := body.Hdr
  52. if !strings.HasPrefix(hdrUrl, "http") {
  53. hdrUrl = fmt.Sprintf("https:%s", body.Hdr)
  54. }
  55. resp, err := http.Get(hdrUrl)
  56. if err != nil {
  57. os.RemoveAll(dir)
  58. return nil, err
  59. }
  60. defer resp.Body.Close()
  61. pix, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. os.RemoveAll(dir)
  64. return nil, err
  65. }
  66. _, err = io.Copy(out, bytes.NewReader(pix))
  67. if err != nil {
  68. os.RemoveAll(dir)
  69. return nil, err
  70. }
  71. out.Close()
  72. // //调用转换器进行转化
  73. // //2 文件转.osg.gles
  74. relativePath := fmt.Sprintf("%s/%s", wkspace, modelName)
  75. resultPath := fmt.Sprintf("/data/%s/result/", wkspace)
  76. //let cmd = `docker run -i -v $(pwd)/uploads:/data dongjianran/envtools process_environment.py --approximateDirectionalLights --write-by-channel --fixedge /data/${hdrName} /data/result/`
  77. err = CmdExe("docker", []string{"exec", "hdr", "process_environment.py", "--approximateDirectionalLights", "--write-by-channel", "--fixedge", fmt.Sprintf(`/data/%s`, relativePath), resultPath})
  78. if err != nil { //获取输出对象,可以从该对象中读取输出结果
  79. os.RemoveAll(dir)
  80. return nil, err
  81. }
  82. configPath := fmt.Sprintf(`%s/result/config.json`, dir)
  83. ext, _ := PathExists(configPath)
  84. if !ext {
  85. os.RemoveAll(dir)
  86. return nil, NewError("文件转化失败!")
  87. }
  88. //上传文件到oss的同级目录 ${dir}/result/*.gz
  89. client, err := CreateObsClient()
  90. if err != nil {
  91. return nil, NewError("创建ObsClient 失败!")
  92. }
  93. defer func() {
  94. client.Close()
  95. }()
  96. bucketName := apictx.Svc.Conf.Obs.Bucket
  97. imageMap := map[string]string{}
  98. //上传产生的所有文件
  99. files, _ := ioutil.ReadDir(fmt.Sprintf(`%s/result`, dir))
  100. for _, f := range files {
  101. if !f.IsDir() {
  102. fmt.Println(f.Name())
  103. fpath := fmt.Sprintf(`%s/result/%s`, dir, f.Name())
  104. obs := UploadFile(client, bucketName, fpath, obsTargetPath)
  105. if obs.Size > 0 {
  106. imageMap[f.Name()] = obs.Url
  107. }
  108. }
  109. }
  110. bytes, _ := ioutil.ReadFile(configPath)
  111. //上传hdr文件
  112. // hdr := UploadFile(client, bucketName, localHdrpath, obsTargetPath)
  113. env3dConf := &model.Evn3dHdrConf{}
  114. err = json.Unmarshal(bytes, env3dConf)
  115. if err != nil {
  116. return nil, err
  117. }
  118. thumbnail := model.OssType{}
  119. for _, v := range env3dConf.Textures {
  120. for _, img := range v.Images {
  121. img.File = imageMap[img.File]
  122. }
  123. if v.Type == "thumbnail" && len(thumbnail.Url) < 1 {
  124. thumbnail.Url = v.Images[0].File
  125. thumbnail.Size = int64(v.Images[0].SizeUncompressed)
  126. }
  127. }
  128. env3d := &model.Env3d{
  129. // HDR: hdr,
  130. Config: env3dConf,
  131. Thumbnail: &thumbnail,
  132. }
  133. ret, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionEvn3d, body.DocId, env3d)
  134. if err != nil {
  135. os.RemoveAll(dir)
  136. return nil, err
  137. }
  138. os.RemoveAll(dir)
  139. return ret, nil
  140. }