CommonUploadServiceImpl.java 11 KB

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