CommonServiceImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.fdkankan.manage.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.json.JSONObject;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.fdkankan.common.util.SecurityUtil;
  8. import com.fdkankan.manage.common.OssPath;
  9. import com.fdkankan.manage.common.ResultCode;
  10. import com.fdkankan.manage.common.ResultData;
  11. import com.fdkankan.common.util.DateExtUtil;
  12. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  13. import com.fdkankan.manage.constant.FileTypeEnum;
  14. import com.fdkankan.manage.entity.CommonUpload;
  15. import com.fdkankan.manage.entity.DictFile;
  16. import com.fdkankan.manage.exception.BusinessException;
  17. import com.fdkankan.manage.service.ICommonService;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.*;
  21. import com.fdkankan.manage.service.ICommonUploadService;
  22. import com.fdkankan.manage.service.IDictFileService;
  23. import com.fdkankan.manage.util.FileWriterUtil;
  24. import com.fdkankan.manage.util.OBJToGLBUtil;
  25. import com.fdkankan.manage.util.ShellUtil;
  26. import com.fdkankan.manage.vo.response.DictFileVo;
  27. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import javax.annotation.Resource;
  35. /**
  36. * <p>
  37. * TODO
  38. * </p>
  39. *
  40. * @author dengsixing
  41. * @since 2022/6/7
  42. **/
  43. @Service
  44. @Slf4j
  45. public class CommonServiceImpl implements ICommonService {
  46. @Value("${fyun.host:https://4dkk.4dage.com/}")
  47. private String ossUrlPrefix;
  48. @Autowired
  49. private FYunFileServiceInterface fYunFileServiceInterface;
  50. @Autowired
  51. RabbitMqProducer rabbitMqProducer;
  52. @Override
  53. public ResultData uploadFile(MultipartFile file) {
  54. File tempFile = null;
  55. try {
  56. String uuid = UUID.randomUUID().toString();
  57. String originalFilename = file.getOriginalFilename();
  58. String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
  59. String ossPath = String.format(OssPath.MANAGE_FILE_PATH, DateUtil.format(Calendar.getInstance()
  60. .getTime(), DateExtUtil.dateStyle6), uuid + extName);
  61. tempFile = new File(OssPath.localPath + ossPath);
  62. file.transferTo(tempFile);
  63. fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
  64. String url = this.ossUrlPrefix + ossPath;
  65. return ResultData.ok(url);
  66. }catch (Exception e){
  67. log.info("upload-file-error:{}",e);
  68. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  69. }finally {
  70. if(tempFile != null){
  71. tempFile.delete();
  72. }
  73. }
  74. }
  75. @Autowired
  76. ICommonUploadService commonUploadService;
  77. @Autowired
  78. IDictFileService dictFileService;
  79. @Override
  80. public ResultData uploadFileNew(MultipartFile file,Integer dictId) {
  81. if(file.isEmpty() ){
  82. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  83. }
  84. File tempFile = null;
  85. try {
  86. String uuid = UUID.randomUUID().toString().replace("-","");
  87. String originalFilename = file.getOriginalFilename();
  88. String extName = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
  89. String ossPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, uuid + extName);
  90. tempFile = new File(OssPath.localPath + ossPath);
  91. if(!tempFile.getParentFile().exists()){
  92. tempFile.getParentFile().mkdirs();
  93. }
  94. file.transferTo(tempFile);
  95. if(extName.equals(".zip")){
  96. return uploadModelZip(originalFilename.replace(extName, ""),tempFile,dictId);
  97. }
  98. fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
  99. String url = this.ossUrlPrefix + ossPath;
  100. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(extName.replace(".", ""));
  101. if(fileTypeEnum == null){
  102. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  103. }
  104. String format = extName.replace(".", "");
  105. CommonUpload commonUpload = commonUploadService.add(originalFilename.replace(extName, ""), url, String.valueOf(file.getSize()), uuid, fileTypeEnum, format,format,1,null,dictId);
  106. tempFile.delete();
  107. return ResultData.ok(commonUpload);
  108. }catch (Exception e){
  109. log.info("upload-file-error:{}",e);
  110. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  111. }
  112. }
  113. private ResultData uploadModelZip(String oldName,File tempFile,Integer dictId) {
  114. String unzipPath = tempFile.getParentFile().getPath() +"/result/"+UUID.randomUUID().toString().replace("-","");
  115. ShellUtil.unZip(tempFile.getPath(),unzipPath);
  116. File unZipFile = new File(unzipPath);
  117. if(!unZipFile.exists() || !unZipFile.isDirectory()){
  118. throw new BusinessException(ResultCode.UNZIP_ERROR);
  119. }
  120. List<File> fileList = new ArrayList<>();
  121. FileWriterUtil.getCanRunList(fileList,unZipFile);
  122. if(fileList.size() <=0){
  123. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  124. }
  125. File modelFile = fileList.get(0);
  126. if(FileWriterUtil.isChinese(modelFile.getName())){
  127. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  128. }
  129. String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
  130. String url = null;
  131. String resultFormat = modelFileFormat;
  132. switch (modelFileFormat){
  133. case "obj" : resultFormat = "glb";
  134. url = uploadObjOss(unzipPath,modelFile);break;
  135. case "laz" :
  136. case "shp" : url = uploadOss(unzipPath,modelFile); break;
  137. case "b3dm" : url = uploadB3dm(unzipPath,modelFile); break;
  138. case "las" :
  139. case "ply" : url = uploadLasOrPly(unzipPath,modelFile);break;
  140. case "osgb": resultFormat = "b3dm";
  141. uploadOsgb(unzipPath,modelFile) ;break;
  142. default: break;
  143. }
  144. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
  145. if(fileTypeEnum == null){
  146. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  147. }
  148. Integer status = StringUtils.isNotBlank(url) ?1:0;
  149. CommonUpload commonUpload = commonUploadService.add(oldName,url, String.valueOf(tempFile.length()),
  150. null, fileTypeEnum, modelFileFormat,resultFormat,status,unzipPath,dictId);
  151. tempFile.delete();
  152. return ResultData.ok(commonUpload);
  153. }
  154. private String uploadObjOss(String unzipPath, File modelFile) {
  155. OBJToGLBUtil.checkObj(modelFile.getPath());
  156. String localGlbPath = modelFile.getPath().replace(".obj",".glb");
  157. OBJToGLBUtil.objToGlb2(modelFile.getPath(),localGlbPath);
  158. String ossPath = unzipPath.replace(OssPath.localPath,"");
  159. ShellUtil.yunUpload(unzipPath,ossPath);
  160. if(!fYunFileServiceInterface.fileExist(localGlbPath.replace(OssPath.localPath,""))){
  161. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  162. }
  163. FileUtil.del(unzipPath);
  164. return ossUrlPrefix + localGlbPath.replace(OssPath.localPath,"");
  165. }
  166. private String uploadB3dm(String unzipPath, File modelFile) {
  167. String b3dmJsonPath = FileWriterUtil.checkB3dmTileset(new File(unzipPath));
  168. if(b3dmJsonPath == null){
  169. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  170. }
  171. String ossPath = b3dmJsonPath.replace(OssPath.localPath, "");
  172. uploadOss(unzipPath,modelFile);
  173. if(!fYunFileServiceInterface.fileExist(ossPath)){
  174. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  175. }
  176. return ossUrlPrefix + ossPath;
  177. }
  178. private void uploadOsgb(String unzipPath, File modelFile) {
  179. //容器不支持转化,通知fusion容器执行
  180. HashMap<String,String > map = new HashMap<>();
  181. map.put("path",unzipPath);
  182. rabbitMqProducer.sendByWorkQueue("queue-model-osgbToB3dm",map);
  183. }
  184. private String uploadOss(String unzipPath,File modelFile) {
  185. String ossPath = unzipPath.replace(OssPath.localPath,"");
  186. String modelOssPath = modelFile.getPath().replace(OssPath.localPath, "");
  187. ShellUtil.yunUpload(unzipPath,ossPath);
  188. if(!fYunFileServiceInterface.fileExist(modelOssPath)){
  189. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  190. }
  191. FileUtil.del(unzipPath);
  192. return ossUrlPrefix + modelOssPath;
  193. }
  194. private String uploadLasOrPly(String unzipPath ,File modelFile) {
  195. File mntFile = OBJToGLBUtil.lasOrPlyToBin(modelFile);
  196. String ossPath = mntFile.getPath().replace(OssPath.localPath,"");
  197. ShellUtil.yunUpload(mntFile.getPath(),ossPath);
  198. if(!fYunFileServiceInterface.fileExist(ossPath+"/webcloud/cloud.js")){
  199. throw new BusinessException(-1,"缺少cloud.js文件");
  200. }
  201. FileUtil.del(unzipPath);
  202. return ossUrlPrefix + ossPath + "/webcloud";
  203. }
  204. }