ThreadService.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.fdkankan.fusion.common.FilePath;
  5. import com.fdkankan.fusion.common.util.*;
  6. import com.fdkankan.fusion.entity.Model;
  7. import com.fdkankan.fusion.exception.BusinessException;
  8. import com.fdkankan.fusion.service.IModelService;
  9. import com.fdkankan.redis.util.RedisUtil;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.io.FileUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.scheduling.annotation.Async;
  15. import org.springframework.stereotype.Service;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. @Service
  21. @Slf4j
  22. public class ThreadService {
  23. @Autowired
  24. RedisUtil redisUtil;
  25. @Autowired
  26. IModelService modelService;
  27. @Autowired
  28. UploadToOssUtil uploadToOssUtil;
  29. @Value("${spring.profiles.active}")
  30. private String environment;
  31. @Value("${upload.query-path}")
  32. private String queryPath;
  33. @Async
  34. public void uploadModelObj(String fileName, String objPath,File newObjFile,Model model){
  35. File objPathFile = null;
  36. File mntFile = null;
  37. File b3dmFile = null;
  38. File osgbFile = null;
  39. try {
  40. if(fileName.toLowerCase().endsWith(".zip")){
  41. ShellUtil.unZip(newObjFile.getPath(),objPath);
  42. }
  43. objPathFile = new File(objPath );
  44. if(!objPathFile.isDirectory()){
  45. throw new BusinessException(-1,"解压错误");
  46. }
  47. List<File> fileList = new ArrayList<>();
  48. FileWriterUtil.getCanRunList(fileList,objPathFile);
  49. if(fileList.size() <=0){
  50. throw new BusinessException(-1,"可上传文件不存在");
  51. }
  52. File file1 = fileList.get(0);
  53. if(file1 == null){
  54. throw new BusinessException(-1,"可上传文件不存在");
  55. }
  56. if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){
  57. throw new BusinessException(-1,"压缩包中文");
  58. }
  59. String b3dmJsonPath = null;
  60. if(file1.getName().endsWith(".b3dm") ){
  61. b3dmJsonPath = FileWriterUtil.checkB3dmTileset(objPathFile);
  62. if(b3dmJsonPath == null){
  63. throw new BusinessException(-1,"缺少tileset.json文件");
  64. }
  65. }
  66. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20");
  67. String glbOssPath = String.format(FilePath.GLB_OSS_PATH,environment, model.getModelId());
  68. String name = file1.getName();
  69. if(name.contains("obj") || name.contains("OBJ")){
  70. glbOssPath = glbOssPath.replace("mesh.glb",file1.getName().replace(".obj",".glb"));
  71. model.setModelDateType("obj");
  72. model.setModelType("glb");
  73. OBJToGLBUtil.objToGlb2(file1.getPath(), file1.getPath().replace(".obj",".glb"));
  74. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  75. uploadToOssUtil.uploadOss(file1.getPath().replace(".obj",".glb"),glbOssPath);
  76. if(!uploadToOssUtil.existKey(glbOssPath)){
  77. throw new BusinessException(-1,"缺少.glb文件");
  78. }
  79. }
  80. if(name.contains(".ply")){
  81. model.setModelDateType("ply");
  82. model.setModelType("ply");
  83. }
  84. if(name.contains(".las")){
  85. model.setModelDateType("las");
  86. model.setModelType("las");
  87. }
  88. if("las".equals(model.getModelType()) || "ply".equals(model.getModelType()) ){
  89. mntFile = OBJToGLBUtil.lasOrPlyToBin(file1);
  90. glbOssPath = mntFile.getPath().replace("/mnt/","")+"/webcloud";
  91. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  92. uploadToOssUtil.uploadFileOss(mntFile );
  93. if(!uploadToOssUtil.existKey(glbOssPath+"/cloud.js")){
  94. throw new BusinessException(-1,"缺少cloud.js文件");
  95. }
  96. }
  97. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + glbOssPath)));
  98. String b3dmPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/");
  99. if(name.contains(".osgb")){
  100. model.setModelDateType("osgb");
  101. model.setModelType("osgb");
  102. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
  103. String localPath = OBJToGLBUtil.OsgbToB3dm(objPathFile);
  104. osgbFile = new File(localPath.replace("mnt/fusion/b3dm","/mnt/fusion/osgb"));
  105. b3dmFile = new File(localPath);
  106. b3dmJsonPath = FileWriterUtil.checkB3dmTileset(b3dmFile);
  107. if(b3dmJsonPath == null){
  108. throw new BusinessException(-1,"缺少tileset.json文件");
  109. }
  110. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"80");
  111. String replace = b3dmJsonPath.replace(FilePath.MNT_BASE_PATH, "fusion/"+environment+"/");
  112. File file2 = new File(replace);
  113. ShellUtil.yunUpload(localPath,file2.getParent());
  114. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + replace))));
  115. }
  116. if(name.contains(".b3dm") && b3dmJsonPath != null){
  117. model.setModelDateType("b3dm");
  118. model.setModelType("b3dm");
  119. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
  120. ShellUtil.yunUpload(objPathFile.getPath(),b3dmPath);
  121. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + b3dmJsonPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/")))));
  122. }
  123. if(name.contains(".laz")){
  124. model.setModelDateType("laz");
  125. model.setModelType("laz");
  126. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  127. ShellUtil.yunUpload(objPathFile.getPath(),objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/"));
  128. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") + "/cloud.js"))));
  129. }
  130. setCreateStatus(model,1);
  131. modelService.saveOrUpdate(model);
  132. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"100");
  133. }catch (Exception e){
  134. setCreateStatus(model,-1);
  135. log.error("uploadObj--ThreadError-modeId:{},error:{}",model.getModelId(),e);
  136. }finally {
  137. // if(newObjFile!=null){
  138. // FileUtil.del(newObjFile);
  139. // }
  140. // if(objPathFile!=null){
  141. // FileUtil.del(objPathFile);
  142. // }
  143. // if(mntFile!=null){
  144. // FileUtil.del(mntFile.getParentFile());
  145. // }
  146. // if(b3dmFile != null){
  147. // FileUtil.del(b3dmFile.getParentFile());
  148. // }
  149. // if(osgbFile != null){
  150. // FileUtil.del(osgbFile.getParentFile());
  151. // }
  152. }
  153. }
  154. private void setCreateStatus(Model model,Integer status){
  155. String redisKey = RedisKeyUtil.modelCancelUpload+model.getModelId();
  156. if(redisUtil.hasKey(redisKey)){
  157. if(redisUtil.get(redisKey).equals("-2")){
  158. return;
  159. }
  160. }
  161. model.setCreateStatus(status);
  162. modelService.saveOrUpdate(model);
  163. if(status != 1){
  164. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),status.toString());
  165. }
  166. }
  167. }