CommonUploadServiceImpl.java 13 KB

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