CommonUploadServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. FileWriterUtil.checkSpace(file.getSize(),3.0);
  69. return uploadModelZip(name.replace(extName, ""),tempFile,dictId);
  70. }
  71. //localToOssUtil.uploadOss(tempFile.getPath(), ossPath);
  72. //String url = this.ossUrlPrefix + ossPath;
  73. FileWriterUtil.checkSpace(file.getSize(),1.1);
  74. String url = CacheUtil.mapping + ossPath;
  75. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(extName.replace(".", ""));
  76. if(fileTypeEnum == null){
  77. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  78. }
  79. String format = extName.replace(".", "");
  80. CommonUpload commonUpload = commonUploadService.add(name.replace(extName, ""), url, String.valueOf(file.getSize()), uuid, fileTypeEnum, format,format,1,tempFile.getPath(),dictId);
  81. return ResultData.ok(commonUpload);
  82. }catch ( BusinessException e){
  83. log.info("upload-file-error:{}",e);
  84. throw e;
  85. }catch (Exception e){
  86. log.info("upload-file-error:{}",e);
  87. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  88. }
  89. }
  90. private ResultData uploadModelZip(String oldName,File file,Integer dictId) {
  91. String ossZipPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, UUID.randomUUID().toString().replace("-", ""));
  92. String unzipPath = CacheUtil.basePath + ossZipPath;
  93. ShellUtil.unZip(file.getPath(),unzipPath);
  94. try {
  95. Thread.sleep(1000L);
  96. FileUtil.del(file.getPath());
  97. }catch (Exception e){
  98. log.info("删除文件失败:{}",e);
  99. }
  100. //FileUtil.copyContent(file,new File(unzipPath),true);
  101. File unZipFile = new File(unzipPath);
  102. if(!unZipFile.exists() || !unZipFile.isDirectory() ){
  103. throw new BusinessException(ResultCode.UNZIP_ERROR);
  104. }
  105. List<File> fileList = new ArrayList<>();
  106. FileWriterUtil.getCanRunList(fileList,unZipFile);
  107. if(fileList.size() <=0){
  108. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  109. }
  110. File modelFile = fileList.get(0);
  111. if(FileWriterUtil.isChinese(modelFile.getName())){
  112. throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
  113. }
  114. if(FileWriterUtil.isChinese(modelFile.getPath())){
  115. throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
  116. }
  117. String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
  118. String url = null;
  119. String resultFormat = modelFileFormat;
  120. switch (modelFileFormat){
  121. case "obj" : resultFormat = "obj";
  122. url = uploadObjOss(ossZipPath,modelFile);break;
  123. case "laz" : url = uploadLazOss(ossZipPath,modelFile); break;
  124. case "shp" : url = uploadOss(ossZipPath,modelFile); break;
  125. case "b3dm" : url = uploadB3dm(ossZipPath,modelFile); break;
  126. case "las" :
  127. case "ply" : url = uploadLasOrPly(ossZipPath,modelFile);break;
  128. case "osgb":
  129. resultFormat = "b3dm";
  130. break;
  131. default: break;
  132. }
  133. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
  134. if(fileTypeEnum == null){
  135. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  136. }
  137. Integer status = StringUtils.isNotBlank(url) ?1:-1;
  138. url = StringUtils.isNotBlank(url) ?CacheUtil.mapping + url:null;
  139. CommonUpload commonUpload = commonUploadService.add(oldName,url, String.valueOf(getDirectorySize(unZipFile)),
  140. null, fileTypeEnum, modelFileFormat,resultFormat,status,unZipFile.getPath(),dictId);
  141. if("osgb".equals(modelFileFormat)){
  142. commonUploadService.updateStatus(commonUpload.getId(),0);
  143. commonUpload.setStatus(0);
  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. return unzipPath +File.separator+modelFile.getName();
  157. }
  158. private String uploadB3dm(String unzipPath, File modelFile) {
  159. String b3dmJsonPath = FileWriterUtil.checkB3dmTileset(new File(CacheUtil.basePath + unzipPath));
  160. if(b3dmJsonPath == null){
  161. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  162. }
  163. //uploadOss(unzipPath,modelFile);
  164. File file = new File(b3dmJsonPath);
  165. if(!file.exists()){
  166. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  167. }
  168. return unzipPath +File.separator +file.getName();
  169. }
  170. @Autowired
  171. OsgbToB3dmConsumer osgbToB3dmConsumer;
  172. private void uploadOsgb(Integer uploadId) {
  173. //osgbToB3dmConsumer.consumerQueue(CacheUtil.basePath + unzipPath);
  174. osgbToB3dmConsumer.consumerQueue(uploadId);
  175. }
  176. private String uploadLazOss(String unzipPath,File modelFile) {
  177. if(!modelFile.exists()){
  178. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  179. }
  180. return unzipPath ;
  181. // return modelFile.getParentFile().getPath();
  182. }
  183. private String uploadOss(String unzipPath,File modelFile) {
  184. if(!modelFile.exists()){
  185. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  186. }
  187. return unzipPath;
  188. // return modelFile.getPath();
  189. }
  190. private String uploadLasOrPly(String unzipPath ,File modelFile) {
  191. File mntFile = OBJToGLBUtil.lasOrPlyToBin(modelFile);
  192. File file = new File(mntFile.getPath() + "/webcloud/cloud.js");
  193. if(!file.exists()){
  194. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  195. }
  196. return unzipPath + File.separator +"webcloud";
  197. // return mntFile.getPath()+ File.separator +"webcloud";
  198. }
  199. @Override
  200. public CommonUpload add(String fileName, String url, String fileSize, String uuid, FileTypeEnum fileTypeEnum, String resultFormat,String replace1, Integer status, String unzipPath, Integer dictId) {
  201. CommonUpload upload = new CommonUpload();
  202. upload.setFileName(fileName);
  203. upload.setFileUrl(url);
  204. upload.setFileSize(fileSize);
  205. upload.setNewFileName(uuid);
  206. upload.setFileType(fileTypeEnum.getCode());
  207. upload.setFileTypeStr(fileTypeEnum.getMsg());
  208. upload.setFileFormat(resultFormat);
  209. upload.setResultFileFormat(replace1);
  210. upload.setStatus(status);
  211. upload.setUnzipPath(unzipPath);
  212. this.save(upload);
  213. DictFile dictFile = new DictFile();
  214. dictFile.setName(fileName);
  215. dictFile.setTypeKey("media-library");
  216. dictFile.setUploadId(upload.getId());
  217. dictFile.setDictId(dictId);
  218. dictFileService.saveOrUpdate(dictFile);
  219. return upload;
  220. }
  221. @Override
  222. public void updateByPath(Integer uploadId, String url,String unzipPath) {
  223. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  224. wrapper.eq(CommonUpload::getId,uploadId);
  225. wrapper.set(CommonUpload::getStatus,1);
  226. wrapper.set(CommonUpload::getFileUrl,url);
  227. wrapper.set(CommonUpload::getUnzipPath,unzipPath);
  228. this.update(wrapper);
  229. }
  230. @Override
  231. public void updateStatus(Integer uploadId,Integer status) {
  232. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  233. wrapper.eq(CommonUpload::getId,uploadId);
  234. wrapper.set(CommonUpload::getStatus,status);
  235. this.update(wrapper);
  236. }
  237. @Override
  238. public void updateByPath(Integer uploadId, String url,String wgs84 ,String gcj02,String unzipPath) {
  239. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  240. wrapper.eq(CommonUpload::getId,uploadId);
  241. wrapper.set(CommonUpload::getStatus,1);
  242. wrapper.set(CommonUpload::getFileUrl,url);
  243. wrapper.set(CommonUpload::getWgs84,wgs84);
  244. wrapper.set(CommonUpload::getGcj02,gcj02);
  245. wrapper.set(CommonUpload::getUnzipPath,unzipPath);
  246. this.update(wrapper);
  247. }
  248. public static long getDirectorySize(File directory) {
  249. long size = 0;
  250. try {
  251. File[] files = directory.listFiles();
  252. if (files != null) {
  253. for (File file : files) {
  254. if (file.isFile()) {
  255. size += file.length();
  256. } else if (file.isDirectory()) {
  257. size += getDirectorySize(file);
  258. }
  259. }
  260. }
  261. }catch (Exception e){
  262. }
  263. return size;
  264. }
  265. @Override
  266. public List<CommonUpload> getDelData() {
  267. return this.getBaseMapper().getDelData();
  268. }
  269. @Override
  270. public void delByIds(List<Integer> ids) {
  271. this.getBaseMapper().delByIds(ids);
  272. }
  273. }