123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package api
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "path"
- "sku3dweb/db/model"
- "sku3dweb/db/repo"
- "strings"
- "time"
- "github.com/gin-gonic/gin"
- )
- //ServiceTestHttp JWT授权的http处理函数
- func ServiceConvHdr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- body := struct {
- Hdr string
- DocId string
- }{}
- err := c.ShouldBindJSON(&body)
- if err != nil {
- return nil, err
- }
- if len(body.Hdr) < 1 {
- return nil, NewError("模型链接不能为空")
- }
- if len(body.DocId) < 1 {
- return nil, NewError("docId不能为空")
- }
- sDir, modelName := path.Split(body.Hdr)
- seg := strings.Split(sDir, "myhuaweicloud.com/")
- obsTargetPath := seg[len(seg)-1]
- // sDir = "assets/env3d/db"
- // obsTargetPath := fmt.Sprintf("%s/%s", sDir, body.DocId)
- wkspace := fmt.Sprintf("%d", time.Now().UnixNano())
- dir := fmt.Sprintf("uploads/%s", wkspace)
- err = os.MkdirAll(dir, 0777)
- if err != nil {
- return nil, NewError("创建目录失败!")
- }
- //1下载文件到本地
- localHdrpath := fmt.Sprintf("%s/%s", dir, modelName)
- out, err := os.Create(localHdrpath)
- if err != nil {
- os.RemoveAll(dir)
- return nil, err
- }
- hdrUrl := body.Hdr
- if !strings.HasPrefix(hdrUrl, "http") {
- hdrUrl = fmt.Sprintf("https:%s", body.Hdr)
- }
- resp, err := http.Get(hdrUrl)
- if err != nil {
- os.RemoveAll(dir)
- return nil, err
- }
- defer resp.Body.Close()
- pix, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- os.RemoveAll(dir)
- return nil, err
- }
- _, err = io.Copy(out, bytes.NewReader(pix))
- if err != nil {
- os.RemoveAll(dir)
- return nil, err
- }
- out.Close()
- // //调用转换器进行转化
- // //2 文件转.osg.gles
- relativePath := fmt.Sprintf("%s/%s", wkspace, modelName)
- resultPath := fmt.Sprintf("/data/%s/result/", wkspace)
- //let cmd = `docker run -i -v $(pwd)/uploads:/data dongjianran/envtools process_environment.py --approximateDirectionalLights --write-by-channel --fixedge /data/${hdrName} /data/result/`
- err = CmdExe("docker", []string{"exec", "hdr", "process_environment.py", "--approximateDirectionalLights", "--write-by-channel", "--fixedge", fmt.Sprintf(`/data/%s`, relativePath), resultPath})
- if err != nil { //获取输出对象,可以从该对象中读取输出结果
- os.RemoveAll(dir)
- return nil, err
- }
- configPath := fmt.Sprintf(`%s/result/config.json`, dir)
- ext, _ := PathExists(configPath)
- if !ext {
- os.RemoveAll(dir)
- return nil, NewError("文件转化失败!")
- }
- //上传文件到oss的同级目录 ${dir}/result/*.gz
- client, err := CreateObsClient()
- if err != nil {
- return nil, NewError("创建ObsClient 失败!")
- }
- defer func() {
- client.Close()
- }()
- bucketName := apictx.Svc.Conf.Obs.Bucket
- imageMap := map[string]string{}
- //上传产生的所有文件
- files, _ := ioutil.ReadDir(fmt.Sprintf(`%s/result`, dir))
- for _, f := range files {
- if !f.IsDir() {
- fmt.Println(f.Name())
- fpath := fmt.Sprintf(`%s/result/%s`, dir, f.Name())
- obs := UploadFile(client, bucketName, fpath, obsTargetPath)
- if obs.Size > 0 {
- imageMap[f.Name()] = obs.Url
- }
- }
- }
- bytes, _ := ioutil.ReadFile(configPath)
- //上传hdr文件
- // hdr := UploadFile(client, bucketName, localHdrpath, obsTargetPath)
- env3dConf := &model.Evn3dHdrConf{}
- err = json.Unmarshal(bytes, env3dConf)
- if err != nil {
- return nil, err
- }
- thumbnail := model.OssType{}
- for _, v := range env3dConf.Textures {
- for _, img := range v.Images {
- img.File = imageMap[img.File]
- }
- if v.Type == "thumbnail" && len(thumbnail.Url) < 1 {
- thumbnail.Url = v.Images[0].File
- thumbnail.Size = int64(v.Images[0].SizeUncompressed)
- }
- }
- env3d := &model.Env3d{
- // HDR: hdr,
- Config: env3dConf,
- Thumbnail: &thumbnail,
- }
- ret, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionEvn3d, body.DocId, env3d)
- if err != nil {
- os.RemoveAll(dir)
- return nil, err
- }
- os.RemoveAll(dir)
- return ret, nil
- }
|