ThreadService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 ossPath = String.format(FilePath.MODEL_OSS_PATH,environment, model.getModelId());
  68. String name = file1.getName();
  69. if(name.contains("obj") || name.contains("OBJ")){
  70. model.setModelDateType("b3dm");
  71. model.setModelType("b3dm");
  72. String localPath = file1.getParentFile().getPath()+"/b3dm";
  73. OBJToGLBUtil.objToB3dm2(file1.getPath(),localPath);
  74. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  75. ossPath = localPath.replace("/mnt/","");
  76. ShellUtil.yunUpload(localPath,ossPath);
  77. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath+"/tileset.json")));
  78. }
  79. if(name.contains(".ply")){
  80. model.setModelDateType("ply");
  81. model.setModelType("ply");
  82. }
  83. if(name.contains(".las")){
  84. model.setModelDateType("las");
  85. model.setModelType("las");
  86. }
  87. if("las".equals(model.getModelType()) || "ply".equals(model.getModelType()) ){
  88. mntFile = OBJToGLBUtil.lasOrPlyToBin(file1);
  89. ossPath = mntFile.getPath().replace("/mnt/","")+"/webcloud";
  90. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  91. uploadToOssUtil.uploadFileOss(mntFile );
  92. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath)));
  93. if(!uploadToOssUtil.existKey(ossPath+"/cloud.js")){
  94. throw new BusinessException(-1,"缺少cloud.js文件");
  95. }
  96. }
  97. String b3dmPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/");
  98. if(name.contains(".osgb")){
  99. model.setModelDateType("osgb");
  100. model.setModelType("b3dm");
  101. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
  102. String localPath = OBJToGLBUtil.OsgbToB3dm(objPathFile);
  103. osgbFile = new File(localPath.replace("mnt/fusion/b3dm","/mnt/fusion/osgb"));
  104. b3dmFile = new File(localPath);
  105. b3dmJsonPath = FileWriterUtil.checkB3dmTileset(b3dmFile);
  106. if(b3dmJsonPath == null){
  107. throw new BusinessException(-1,"缺少tileset.json文件");
  108. }
  109. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"80");
  110. String replace = b3dmJsonPath.replace(FilePath.MNT_BASE_PATH, "fusion/"+environment+"/");
  111. File file2 = new File(replace);
  112. ShellUtil.yunUpload(localPath,file2.getParent());
  113. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + replace))));
  114. }
  115. if(name.contains(".b3dm") && b3dmJsonPath != null){
  116. model.setModelDateType("b3dm");
  117. model.setModelType("b3dm");
  118. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60");
  119. ShellUtil.yunUpload(objPathFile.getPath(),b3dmPath);
  120. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + b3dmJsonPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/")))));
  121. }
  122. if(name.contains(".laz")){
  123. model.setModelDateType("laz");
  124. model.setModelType("laz");
  125. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  126. ShellUtil.yunUpload(objPathFile.getPath(),objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/"));
  127. model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") ))));
  128. }
  129. model.setFileNewName(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") +"/" + fileName);
  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. }