ThreadService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.List;
  21. @Service
  22. @Slf4j
  23. public class ThreadService {
  24. @Autowired
  25. RedisUtil redisUtil;
  26. @Autowired
  27. IModelService modelService;
  28. @Autowired
  29. UploadToOssUtil uploadToOssUtil;
  30. @Value("${spring.profiles.active}")
  31. private String environment;
  32. @Value("${upload.query-path}")
  33. private String queryPath;
  34. @Async
  35. public void uploadModelObj(String fileName, String objPath,File newObjFile,Model model){
  36. File objPathFile = null;
  37. File mntFile = null;
  38. File b3dmFile = null;
  39. File osgbFile = null;
  40. try {
  41. if(fileName.toLowerCase().endsWith(".zip")){
  42. ShellUtil.unZip(newObjFile.getPath(),objPath);
  43. }
  44. objPathFile = new File(objPath );
  45. if(!objPathFile.isDirectory()){
  46. throw new BusinessException(-1,"解压错误");
  47. }
  48. List<File> fileList = new ArrayList<>();
  49. FileWriterUtil.getCanRunList(fileList,objPathFile);
  50. if(fileList.size() <=0){
  51. throw new BusinessException(-1,"可上传文件不存在");
  52. }
  53. File file1 = fileList.get(0);
  54. if(file1 == null){
  55. throw new BusinessException(-1,"可上传文件不存在");
  56. }
  57. if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){
  58. throw new BusinessException(-1,"压缩包中文");
  59. }
  60. String b3dmJsonPath = null;
  61. if(file1.getName().endsWith(".b3dm") ){
  62. b3dmJsonPath = FileWriterUtil.checkB3dmTileset(objPathFile);
  63. if(b3dmJsonPath == null){
  64. throw new BusinessException(-1,"缺少tileset.json文件");
  65. }
  66. }
  67. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20");
  68. String ossPath = String.format(FilePath.MODEL_OSS_PATH,environment, model.getModelId());
  69. String name = file1.getName();
  70. if(name.contains("obj") || name.contains("OBJ")){
  71. model.setModelDateType("b3dm");
  72. model.setModelType("b3dm");
  73. String localPath = file1.getParentFile().getPath()+"/b3dm";
  74. OBJToGLBUtil.objToB3dm(file1.getPath(),localPath);
  75. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  76. ossPath = localPath.replace("/mnt/","");
  77. ShellUtil.yunUpload(localPath,ossPath);
  78. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath+"/tileset.json")));
  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. ossPath = mntFile.getPath().replace("/mnt/","")+"/webcloud";
  91. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  92. uploadToOssUtil.uploadFileOss(mntFile );
  93. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath)));
  94. if(!uploadToOssUtil.existKey(ossPath+"/cloud.js")){
  95. throw new BusinessException(-1,"缺少cloud.js文件");
  96. }
  97. }
  98. String b3dmPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/");
  99. if(name.contains(".osgb")){
  100. model.setModelDateType("osgb");
  101. model.setModelType("b3dm");
  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/") ))));
  129. }
  130. model.setFileNewName(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") +"/" + fileName);
  131. setCreateStatus(model,1);
  132. modelService.saveOrUpdate(model);
  133. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"100");
  134. }catch (Exception e){
  135. setCreateStatus(model,-1);
  136. log.error("uploadObj--ThreadError-modeId:{},error:{}",model.getModelId(),e);
  137. }finally {
  138. if(newObjFile!=null){
  139. FileUtil.del(newObjFile);
  140. }
  141. if(objPathFile!=null){
  142. FileUtil.del(objPathFile);
  143. }
  144. if(mntFile!=null){
  145. FileUtil.del(mntFile.getParentFile());
  146. }
  147. if(b3dmFile != null){
  148. FileUtil.del(b3dmFile.getParentFile());
  149. }
  150. if(osgbFile != null){
  151. FileUtil.del(osgbFile.getParentFile());
  152. }
  153. }
  154. }
  155. private void setCreateStatus(Model model,Integer status){
  156. String redisKey = RedisKeyUtil.modelCancelUpload+model.getModelId();
  157. if(redisUtil.hasKey(redisKey)){
  158. if(redisUtil.get(redisKey).equals("-2")){
  159. return;
  160. }
  161. }
  162. model.setCreateStatus(status);
  163. modelService.saveOrUpdate(model);
  164. if(status != 1){
  165. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),status.toString());
  166. }
  167. }
  168. @Async
  169. public void objToB3dm(String num,Integer type) {
  170. String objPath = String.format(FilePath.OBJ_LOCAL_PATH ,environment,num);
  171. ShellUtil.yunDownload(String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh" ,objPath);
  172. List<String> localGlbPaths = new ArrayList<>();
  173. List<String> ossGlbPaths = new ArrayList<>();
  174. File localFile = new File(objPath);
  175. Long size = 0L;
  176. this.toGlB(localFile, localGlbPaths,size);
  177. String sizeStr = FileWriterUtil.setFileSize(size);
  178. if(!localGlbPaths.isEmpty()){
  179. for (String localGlbPath : localGlbPaths) {
  180. String ossPath = localGlbPath.replace("/mnt/","");
  181. ShellUtil.yunUpload(localGlbPath,ossPath);
  182. ossGlbPaths.add(queryPath +ossPath+"/tileset.json");
  183. }
  184. log.info("转换完成修改数据库:{}:{}:{}",ossGlbPaths,num,type);
  185. String ossGlbPath = JSONArray.toJSONString(ossGlbPaths);
  186. LambdaUpdateWrapper<Model> wrapper = new LambdaUpdateWrapper<>();
  187. wrapper.eq(Model::getType,type);
  188. wrapper.eq(Model::getNum,num);
  189. wrapper.set(Model::getModelGlbUrl,ossGlbPath);
  190. wrapper.set(Model::getModelDateType,"b3dm");
  191. wrapper.set(Model::getModelType,"b3dm");
  192. wrapper.set(Model::getCreateStatus,1);
  193. wrapper.set(Model::getModelSize,sizeStr);
  194. modelService.update(wrapper);
  195. }
  196. }
  197. private void toGlB(File localFile, List<String> localGlbPath,Long size) {
  198. File[] files = localFile.listFiles();
  199. for (File file : files) {
  200. if(file.isDirectory()){
  201. toGlB(file,localGlbPath,size);
  202. }
  203. if(file.getPath().contains("lod_") ){
  204. if(file.getPath().contains("lod_0") && file.getName().contains(".obj")){
  205. String glbPath = OBJToGLBUtil.objToB3dm(file.getPath(),file.getParentFile().getPath()+"/b3dm");
  206. size += file.length();
  207. localGlbPath.add(glbPath);
  208. }
  209. continue;
  210. }
  211. if(file.getName().contains(".obj")){
  212. String glbPath = OBJToGLBUtil.objToB3dm(file.getPath(),file.getParentFile().getPath()+"/b3dm");
  213. size += file.length();
  214. localGlbPath.add(glbPath);
  215. }
  216. }
  217. }
  218. }