CommonUploadServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.baomidou.dynamic.datasource.annotation.DS;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.fusion.common.OssPath;
  6. import com.fdkankan.fusion.common.ResultCode;
  7. import com.fdkankan.fusion.common.ResultData;
  8. import com.fdkankan.fusion.common.enums.FileTypeEnum;
  9. import com.fdkankan.fusion.common.util.FileWriterUtil;
  10. import com.fdkankan.fusion.common.util.OBJToGLBUtil;
  11. import com.fdkankan.fusion.common.util.ShellUtil;
  12. import com.fdkankan.fusion.common.util.LocalToOssUtil;
  13. import com.fdkankan.fusion.config.CacheUtil;
  14. import com.fdkankan.fusion.config.FusionConfig;
  15. import com.fdkankan.fusion.entity.CommonUpload;
  16. import com.fdkankan.fusion.entity.DictFile;
  17. import com.fdkankan.fusion.exception.BusinessException;
  18. import com.fdkankan.fusion.mapper.ICommonUploadMapper;
  19. import com.fdkankan.fusion.mq.consumer.OsgbToB3dmConsumer;
  20. import com.fdkankan.fusion.service.ICommonUploadService;
  21. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  22. import com.fdkankan.fusion.service.IDictFileService;
  23. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  24. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.web.multipart.MultipartFile;
  31. import scala.Int;
  32. import java.io.File;
  33. import java.util.*;
  34. /**
  35. * <p>
  36. * 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2025-02-10
  41. */
  42. @Service
  43. @Slf4j
  44. public class CommonUploadServiceImpl extends ServiceImpl<ICommonUploadMapper, CommonUpload> implements ICommonUploadService {
  45. @Autowired
  46. ICommonUploadService commonUploadService;
  47. @Autowired
  48. IDictFileService dictFileService;
  49. @Autowired
  50. FusionConfig fusionConfig;
  51. @Autowired
  52. FYunFileServiceInterface fYunFileServiceInterface;
  53. @Autowired
  54. RabbitMqProducer rabbitMqProducer;
  55. @Override
  56. public ResultData uploadFileNew( Integer dictId,MultipartFile file,Long userId) {
  57. if( file.isEmpty() ){
  58. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  59. }
  60. File tempFile = null;
  61. try {
  62. String uuid = UUID.randomUUID().toString().replace("-","");
  63. String name = file.getOriginalFilename();
  64. String extName = name.substring(name.lastIndexOf(".")).toLowerCase();
  65. String ossPath = String.format(OssPath.MANAGE_FILE_PATH, uuid + extName);
  66. tempFile = new File(OssPath.localPath + ossPath);
  67. if(!tempFile.getParentFile().exists()){
  68. tempFile.getParentFile().mkdirs();
  69. }
  70. file.transferTo(tempFile);
  71. if(extName.equals(".zip")){
  72. return uploadModelZip(name.replace(extName, ""),tempFile,dictId,userId);
  73. }
  74. fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
  75. //String url = this.ossUrlPrefix + ossPath;
  76. String url = fusionConfig.getOssUrlPrefix() + ossPath;
  77. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(extName.replace(".", ""));
  78. if(fileTypeEnum == null){
  79. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  80. }
  81. String format = extName.replace(".", "");
  82. CommonUpload commonUpload = commonUploadService.add(name.replace(extName, ""), url, String.valueOf(file.getSize()), uuid, fileTypeEnum, format,format,1,null,dictId,userId);
  83. return ResultData.ok(commonUpload);
  84. }catch ( BusinessException e){
  85. log.info("upload-file-error:{}",e);
  86. throw e;
  87. }catch (Exception e){
  88. log.info("upload-file-error:{}",e);
  89. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  90. }
  91. }
  92. private ResultData uploadModelZip(String oldName,File file,Integer dictId,Long userId) {
  93. String ossZipPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, UUID.randomUUID().toString().replace("-", ""));
  94. String unzipPath = OssPath.localPath + ossZipPath;
  95. ShellUtil.unZip(file.getPath(),unzipPath);
  96. try {
  97. Thread.sleep(1000L);
  98. //FileUtil.del(file.getPath());
  99. }catch (Exception e){
  100. log.info("删除文件失败:{}",e);
  101. }
  102. //FileUtil.copyContent(file,new File(unzipPath),true);
  103. File unZipFile = new File(unzipPath);
  104. if(!unZipFile.exists() || !unZipFile.isDirectory() ){
  105. throw new BusinessException(ResultCode.UNZIP_ERROR);
  106. }
  107. List<File> fileList = new ArrayList<>();
  108. FileWriterUtil.getCanRunList(fileList,unZipFile);
  109. if(fileList.size() <=0){
  110. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  111. }
  112. File modelFile = fileList.get(0);
  113. if(FileWriterUtil.isChinese(modelFile.getName())){
  114. throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
  115. }
  116. if(FileWriterUtil.isChinese(modelFile.getPath())){
  117. throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
  118. }
  119. String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
  120. String url = null;
  121. String resultFormat = modelFileFormat;
  122. switch (modelFileFormat){
  123. case "obj" : resultFormat = "obj";
  124. url = uploadObjOss(unzipPath,modelFile);break;
  125. case "laz" : url = uploadLazOss(unzipPath,modelFile); break;
  126. case "shp" : url = uploadOss(unzipPath,modelFile); break;
  127. case "b3dm" : url = uploadB3dm(unzipPath,modelFile); break;
  128. case "las" :
  129. case "ply" : url = uploadLasOrPly(unzipPath,modelFile);break;
  130. case "osgb":
  131. resultFormat = "b3dm";
  132. break;
  133. default: break;
  134. }
  135. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
  136. if(fileTypeEnum == null){
  137. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  138. }
  139. Integer status = StringUtils.isNotBlank(url) ? 1: -1;
  140. url = StringUtils.isNotBlank(url) ? fusionConfig.getOssUrlPrefix() + url : null;
  141. CommonUpload commonUpload = commonUploadService.add(oldName,url, String.valueOf(getDirectorySize(unZipFile)),
  142. null, fileTypeEnum, modelFileFormat,resultFormat,status,unZipFile.getPath(),dictId,userId);
  143. if("osgb".equals(modelFileFormat)){
  144. uploadOsgb(commonUpload.getId()) ;
  145. }
  146. return ResultData.ok(commonUpload);
  147. }
  148. private String uploadObjOss(String unzipPath, File modelFile) {
  149. OBJToGLBUtil.checkObj(modelFile.getPath());
  150. //String localGlbPath = modelFile.getPath().replace(".obj",".glb");
  151. //OBJToGLBUtil.objToGlb2(modelFile.getPath(),localGlbPath);
  152. // File file = new File(localGlbPath);
  153. // if(!file.exists()){
  154. // throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  155. // }
  156. String ossKey = unzipPath.replace(OssPath.localPath,"");
  157. ShellUtil.yunUpload(unzipPath,ossKey);
  158. return ossKey +File.separator+modelFile.getName();
  159. }
  160. private String uploadB3dm(String unzipPath, File modelFile) {
  161. String b3dmJsonPath = FileWriterUtil.checkB3dmTileset(new File(OssPath.localPath + unzipPath));
  162. if(b3dmJsonPath == null){
  163. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  164. }
  165. //uploadOss(unzipPath,modelFile);
  166. File file = new File(b3dmJsonPath);
  167. if(!file.exists()){
  168. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  169. }
  170. String ossKey = unzipPath.replace(OssPath.localPath,"");
  171. ShellUtil.yunUpload(unzipPath,ossKey);
  172. return ossKey +File.separator +file.getName();
  173. }
  174. private void uploadOsgb(Integer uploadId) {
  175. //osgbToB3dmConsumer.consumerQueue(CacheUtil.basePath + unzipPath);
  176. HashMap<String,Integer> map = new HashMap<>();
  177. map.put("uploadId",uploadId);
  178. rabbitMqProducer.sendByWorkQueue("queue-model-osgbToB3dm",map);
  179. }
  180. private String uploadLazOss(String unzipPath,File modelFile) {
  181. if(!modelFile.exists()){
  182. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  183. }
  184. String ossKey = unzipPath.replace(OssPath.localPath,"");
  185. ShellUtil.yunUpload(unzipPath,ossKey);
  186. return ossKey ;
  187. // return modelFile.getParentFile().getPath();
  188. }
  189. private String uploadOss(String unzipPath,File modelFile) {
  190. if(!modelFile.exists()){
  191. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  192. }
  193. String ossKey = unzipPath.replace(OssPath.localPath,"");
  194. ShellUtil.yunUpload(unzipPath,ossKey);
  195. return ossKey;
  196. // return modelFile.getPath();
  197. }
  198. private String uploadLasOrPly(String unzipPath ,File modelFile) {
  199. File mntFile = OBJToGLBUtil.lasOrPlyToBin(modelFile);
  200. File file = new File(mntFile.getPath() + "/webcloud/cloud.js");
  201. if(!file.exists()){
  202. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  203. }
  204. String ossKey = unzipPath.replace(OssPath.localPath,"");
  205. ShellUtil.yunUpload(unzipPath,ossKey);
  206. return ossKey + File.separator +"webcloud";
  207. // return mntFile.getPath()+ File.separator +"webcloud";
  208. }
  209. @Override
  210. public CommonUpload add(String fileName, String url, String fileSize, String uuid, FileTypeEnum fileTypeEnum, String resultFormat,String replace1, Integer status, String unzipPath, Integer dictId,Long userId) {
  211. CommonUpload upload = new CommonUpload();
  212. upload.setFileName(fileName);
  213. upload.setFileUrl(url);
  214. upload.setFileSize(fileSize);
  215. upload.setNewFileName(uuid);
  216. upload.setFileType(fileTypeEnum.getCode());
  217. upload.setFileTypeStr(fileTypeEnum.getMsg());
  218. upload.setFileFormat(resultFormat);
  219. upload.setResultFileFormat(replace1);
  220. upload.setStatus(status);
  221. upload.setUnzipPath(unzipPath);
  222. this.save(upload);
  223. DictFile dictFile = new DictFile();
  224. dictFile.setName(fileName);
  225. dictFile.setTypeKey("media-library");
  226. dictFile.setUploadId(upload.getId());
  227. dictFile.setDictId(dictId);
  228. dictFile.setSysUserId(userId);
  229. dictFileService.saveOrUpdate(dictFile);
  230. return upload;
  231. }
  232. @Override
  233. public void updateByPath(Integer uploadId, String url) {
  234. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  235. wrapper.eq(CommonUpload::getId,uploadId);
  236. wrapper.set(CommonUpload::getStatus,1);
  237. wrapper.set(CommonUpload::getFileUrl,url);
  238. this.update(wrapper);
  239. }
  240. @Override
  241. public void updateStatus(Integer uploadId,Integer status) {
  242. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  243. wrapper.eq(CommonUpload::getId,uploadId);
  244. wrapper.set(CommonUpload::getStatus,status);
  245. this.update(wrapper);
  246. }
  247. @Override
  248. public void updateByPath(Integer uploadId, String url,String wgs84 ,String gcj02) {
  249. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  250. wrapper.eq(CommonUpload::getId,uploadId);
  251. wrapper.set(CommonUpload::getStatus,1);
  252. wrapper.set(CommonUpload::getFileUrl,url);
  253. wrapper.set(CommonUpload::getWgs84,wgs84);
  254. wrapper.set(CommonUpload::getGcj02,gcj02);
  255. this.update(wrapper);
  256. }
  257. public static long getDirectorySize(File directory) {
  258. long size = 0;
  259. try {
  260. File[] files = directory.listFiles();
  261. if (files != null) {
  262. for (File file : files) {
  263. if (file.isFile()) {
  264. size += file.length();
  265. } else if (file.isDirectory()) {
  266. size += getDirectorySize(file);
  267. }
  268. }
  269. }
  270. }catch (Exception e){
  271. }
  272. return size;
  273. }
  274. }