CommonUploadServiceImpl.java 12 KB

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