CommonUploadServiceImpl.java 13 KB

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