ThreadService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.fusion.common.FilePath;
  6. import com.fdkankan.fusion.common.util.*;
  7. import com.fdkankan.fusion.entity.Model;
  8. import com.fdkankan.fusion.exception.BusinessException;
  9. import com.fdkankan.fusion.service.IModelService;
  10. import com.fdkankan.redis.util.RedisUtil;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.io.FileUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.scheduling.annotation.Async;
  16. import org.springframework.stereotype.Service;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. @Service
  23. @Slf4j
  24. public class ThreadService {
  25. @Autowired
  26. RedisUtil redisUtil;
  27. @Autowired
  28. IModelService modelService;
  29. @Autowired
  30. UploadToOssUtil uploadToOssUtil;
  31. @Value("${spring.profiles.active}")
  32. private String environment;
  33. @Value("${upload.query-path}")
  34. private String queryPath;
  35. @Async
  36. public void uploadModelObj(String fileName, String objPath,File newObjFile,Model model){
  37. File objPathFile = null;
  38. File mntFile = null;
  39. File b3dmFile = null;
  40. File osgbFile = null;
  41. try {
  42. if(fileName.toLowerCase().endsWith(".zip")){
  43. ShellUtil.unZip(newObjFile.getPath(),objPath);
  44. }
  45. objPathFile = new File(objPath );
  46. if(!objPathFile.isDirectory()){
  47. throw new BusinessException(-1,"解压错误");
  48. }
  49. List<File> fileList = new ArrayList<>();
  50. FileWriterUtil.getCanRunList(fileList,objPathFile);
  51. if(fileList.size() <=0){
  52. throw new BusinessException(-1,"可上传文件不存在");
  53. }
  54. File file1 = fileList.get(0);
  55. if(file1 == null){
  56. throw new BusinessException(-1,"可上传文件不存在");
  57. }
  58. if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){
  59. throw new BusinessException(-1,"压缩包中文");
  60. }
  61. String b3dmJsonPath = null;
  62. if(file1.getName().endsWith(".b3dm") ){
  63. b3dmJsonPath = FileWriterUtil.checkB3dmTileset(objPathFile);
  64. if(b3dmJsonPath == null){
  65. throw new BusinessException(-1,"缺少tileset.json文件");
  66. }
  67. }
  68. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20");
  69. String ossPath = String.format(FilePath.MODEL_OSS_PATH,environment, model.getModelId());
  70. String name = file1.getName();
  71. if(name.contains("obj") || name.contains("OBJ")){
  72. model.setModelDateType("b3dm");
  73. model.setModelType("b3dm");
  74. String localPath = file1.getParentFile().getPath()+"/b3dm";
  75. OBJToGLBUtil.objToB3dm(file1.getPath(),localPath);
  76. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  77. ossPath = localPath.replace("/mnt/","");
  78. ShellUtil.yunUpload(localPath,ossPath);
  79. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath+"/tileset.json")));
  80. }
  81. if(name.contains(".ply")){
  82. model.setModelDateType("ply");
  83. model.setModelType("ply");
  84. }
  85. if(name.contains(".las")){
  86. model.setModelDateType("las");
  87. model.setModelType("las");
  88. }
  89. if("las".equals(model.getModelType()) || "ply".equals(model.getModelType()) ){
  90. mntFile = OBJToGLBUtil.lasOrPlyToBin(file1);
  91. ossPath = mntFile.getPath().replace("/mnt/","")+"/webcloud";
  92. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  93. uploadToOssUtil.uploadFileOss(mntFile );
  94. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath)));
  95. if(!uploadToOssUtil.existKey(ossPath+"/cloud.js")){
  96. throw new BusinessException(-1,"缺少cloud.js文件");
  97. }
  98. }
  99. String b3dmPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/");
  100. if(name.contains(".osgb")){
  101. model.setModelDateType("osgb");
  102. model.setModelType("b3dm");
  103. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
  104. String localPath = OBJToGLBUtil.OsgbToB3dm(objPathFile);
  105. osgbFile = new File(localPath.replace("mnt/fusion/b3dm","/mnt/fusion/osgb"));
  106. b3dmFile = new File(localPath);
  107. b3dmJsonPath = FileWriterUtil.checkB3dmTileset(b3dmFile);
  108. if(b3dmJsonPath == null){
  109. throw new BusinessException(-1,"缺少tileset.json文件");
  110. }
  111. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"80");
  112. String replace = b3dmJsonPath.replace(FilePath.MNT_BASE_PATH, "fusion/"+environment+"/");
  113. File file2 = new File(replace);
  114. ShellUtil.yunUpload(localPath,file2.getParent());
  115. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + replace))));
  116. }
  117. if(name.contains(".b3dm") && b3dmJsonPath != null){
  118. model.setModelDateType("b3dm");
  119. model.setModelType("b3dm");
  120. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
  121. ShellUtil.yunUpload(objPathFile.getPath(),b3dmPath);
  122. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + b3dmJsonPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/")))));
  123. }
  124. if(name.contains(".laz")){
  125. model.setModelDateType("laz");
  126. model.setModelType("laz");
  127. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  128. ShellUtil.yunUpload(objPathFile.getPath(),objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/"));
  129. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") ))));
  130. }
  131. model.setFileNewName(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") +"/" + fileName);
  132. setCreateStatus(model,1);
  133. modelService.saveOrUpdate(model);
  134. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"100");
  135. }catch (Exception e){
  136. setCreateStatus(model,-1);
  137. log.error("uploadObj--ThreadError-modeId:{},error:{}",model.getModelId(),e);
  138. }finally {
  139. if(newObjFile!=null){
  140. FileUtil.del(newObjFile);
  141. }
  142. if(objPathFile!=null){
  143. FileUtil.del(objPathFile);
  144. }
  145. if(mntFile!=null){
  146. FileUtil.del(mntFile.getParentFile());
  147. }
  148. if(b3dmFile != null){
  149. FileUtil.del(b3dmFile.getParentFile());
  150. }
  151. if(osgbFile != null){
  152. FileUtil.del(osgbFile.getParentFile());
  153. }
  154. }
  155. }
  156. private void setCreateStatus(Model model,Integer status){
  157. String redisKey = RedisKeyUtil.modelCancelUpload+model.getModelId();
  158. if(redisUtil.hasKey(redisKey)){
  159. if(redisUtil.get(redisKey).equals("-2")){
  160. return;
  161. }
  162. }
  163. model.setCreateStatus(status);
  164. modelService.saveOrUpdate(model);
  165. if(status != 1){
  166. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),status.toString());
  167. }
  168. }
  169. @Async
  170. public void objToB3dm(String num,Integer type) {
  171. String objPath = String.format(FilePath.OBJ_LOCAL_PATH ,environment,num);
  172. ShellUtil.yunDownload(String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh" ,objPath);
  173. List<String> localGlbPaths = new ArrayList<>();
  174. List<String> ossGlbPaths = new ArrayList<>();
  175. File localFile = new File(objPath);
  176. HashMap<String,Long> sizeMap = new HashMap<>();
  177. sizeMap.put("size",0L);
  178. this.toGlB(localFile, localGlbPaths,sizeMap);
  179. String sizeStr = FileWriterUtil.setFileSize(sizeMap.get("size"));
  180. if(!localGlbPaths.isEmpty()){
  181. for (String localGlbPath : localGlbPaths) {
  182. String ossPath = localGlbPath.replace("/mnt/","");
  183. ShellUtil.yunUpload(localGlbPath,ossPath);
  184. ossGlbPaths.add(queryPath +ossPath+"/tileset.json");
  185. }
  186. log.info("转换完成修改数据库:{}:{}:{}",ossGlbPaths,num,type);
  187. String ossGlbPath = JSONArray.toJSONString(ossGlbPaths);
  188. LambdaUpdateWrapper<Model> wrapper = new LambdaUpdateWrapper<>();
  189. wrapper.eq(Model::getType,type);
  190. wrapper.eq(Model::getNum,num);
  191. wrapper.set(Model::getModelGlbUrl,ossGlbPath);
  192. wrapper.set(Model::getModelDateType,"b3dm");
  193. wrapper.set(Model::getModelType,"b3dm");
  194. wrapper.set(Model::getCreateStatus,1);
  195. wrapper.set(Model::getModelSize,sizeStr);
  196. modelService.update(wrapper);
  197. }
  198. }
  199. private void toGlB(File localFile, List<String> localGlbPath, HashMap<String,Long> sizeMap ) {
  200. File[] files = localFile.listFiles();
  201. for (File file : files) {
  202. if(file.isDirectory()){
  203. toGlB(file,localGlbPath,sizeMap);
  204. }
  205. if(file.getPath().contains("lod_") ){
  206. if(file.getPath().contains("lod_0") && file.getName().contains(".obj")){
  207. String glbPath = OBJToGLBUtil.objToB3dm(file.getPath(),file.getParentFile().getPath()+"/b3dm");
  208. sizeMap.put("size",sizeMap.get("size") + file.length());
  209. localGlbPath.add(glbPath);
  210. }
  211. continue;
  212. }
  213. if(file.getName().contains(".obj")){
  214. String glbPath = OBJToGLBUtil.objToB3dm(file.getPath(),file.getParentFile().getPath()+"/b3dm");
  215. sizeMap.put("size",sizeMap.get("size") + file.length());
  216. localGlbPath.add(glbPath);
  217. }
  218. }
  219. }
  220. }