CommonUploadServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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,null);
  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. }finally {
  97. if(tempFile != null){
  98. tempFile.delete();
  99. }
  100. }
  101. }
  102. private ResultData uploadModelZip(String oldName,File file,Integer dictId) {
  103. File unZipFile = null;
  104. try {
  105. Long fileSize = file.length();
  106. String ossZipPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, UUID.randomUUID().toString().replace("-", ""));
  107. String sourcePath = String.format(OssPath.MANAGE_SOURCE_FILE_PATH, UUID.randomUUID().toString().replace("-", ""));
  108. String unzipPath = CacheUtil.basePath + ossZipPath;
  109. ShellUtil.yunUpload(file.getPath(),sourcePath);
  110. ShellUtil.unZip(file.getPath(),unzipPath);
  111. //FileUtil.copyContent(file,new File(unzipPath),true);
  112. unZipFile = new File(unzipPath);
  113. if(!unZipFile.exists() || !unZipFile.isDirectory() ){
  114. throw new BusinessException(ResultCode.UNZIP_ERROR);
  115. }
  116. List<File> fileList = new ArrayList<>();
  117. FileWriterUtil.getCanRunList(fileList,unZipFile);
  118. if(fileList.size() <=0){
  119. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  120. }
  121. File modelFile = fileList.get(0);
  122. String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
  123. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
  124. if(fileTypeEnum == null){
  125. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  126. }
  127. if(dictId != null){
  128. Dict dict = dictService.getById(dictId);
  129. if(dict == null ){
  130. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  131. }
  132. if("animation".equals(dict.getUseType()) && !(modelFileFormat.equals("glb") || modelFileFormat.equals("obj"))){
  133. throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
  134. }
  135. }
  136. if(FileWriterUtil.isChinese(modelFile.getName())){
  137. throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
  138. }
  139. if(FileWriterUtil.isChinese(modelFile.getPath())){
  140. throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
  141. }
  142. String url = null;
  143. String resultFormat = modelFileFormat;
  144. switch (modelFileFormat){
  145. case "obj" : resultFormat = "obj";
  146. url = uploadObjOss(unzipPath,modelFile);break;
  147. case "glb" : url = uploadOss(unzipPath,modelFile);break;
  148. case "laz" : url = uploadLazOss(unzipPath,modelFile); break;
  149. case "shp" : url = uploadOss(unzipPath,modelFile); break;
  150. case "b3dm" : url = uploadB3dm(unzipPath,modelFile); break;
  151. case "las" :
  152. case "ply" : url = uploadLasOrPly(unzipPath,modelFile);break;
  153. case "osgb": resultFormat = "b3dm";
  154. break;
  155. default: break;
  156. }
  157. Integer status = StringUtils.isNotBlank(url) ?1:-1;
  158. url = StringUtils.isNotBlank(url) ?ossUrlPrefix + url:null;
  159. sourcePath = StringUtils.isNotBlank(sourcePath) ?ossUrlPrefix + sourcePath:null;
  160. CommonUpload commonUpload = commonUploadService.add(oldName,url, String.valueOf(fileSize),
  161. null, fileTypeEnum, modelFileFormat,resultFormat,status,unZipFile.getPath(),dictId,sourcePath);
  162. if("osgb".equals(modelFileFormat)){
  163. uploadOsgb(commonUpload.getId()) ;
  164. commonUpload = commonUploadService.getById(commonUpload.getId());
  165. }
  166. return ResultData.ok(commonUpload);
  167. } catch (BusinessException e){
  168. throw e;
  169. }catch (Exception e){
  170. log.info("上传失败:{}",e);
  171. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  172. }finally {
  173. if(unZipFile != null){
  174. unZipFile.delete();
  175. }
  176. }
  177. }
  178. private String uploadObjOss(String unzipPath, File modelFile) {
  179. OBJToGLBUtil.checkObj(modelFile.getPath());
  180. String localGlbPath = modelFile.getPath().replace(".obj",".glb");
  181. OBJToGLBUtil.objToGlb2(modelFile.getPath(),localGlbPath);
  182. String ossPath = unzipPath.replace(OssPath.localPath,"");
  183. ShellUtil.yunUpload(unzipPath,ossPath);
  184. if(!uploadToOssUtil.fileExist(localGlbPath.replace(OssPath.localPath,""))){
  185. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  186. }
  187. return localGlbPath.replace(OssPath.localPath,"");
  188. }
  189. private String uploadB3dm(String unzipPath, File modelFile) {
  190. String b3dmJsonPath = FileWriterUtil.checkB3dmTileset(new File(unzipPath));
  191. if(b3dmJsonPath == null){
  192. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  193. }
  194. String ossPath = b3dmJsonPath.replace(OssPath.localPath, "");
  195. uploadOss(unzipPath,modelFile);
  196. if(!uploadToOssUtil.fileExist(ossPath)){
  197. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  198. }
  199. return ossPath;
  200. }
  201. @Autowired
  202. OsgbToB3dmConsumer osgbToB3dmConsumer;
  203. private void uploadOsgb(Integer uploadId) {
  204. osgbToB3dmConsumer.consumerQueue(uploadId);
  205. }
  206. private String uploadLazOss(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.fileExist(modelOssPath)){
  211. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  212. }
  213. return ossPath;
  214. }
  215. private String uploadOss(String unzipPath,File modelFile) {
  216. String ossPath = unzipPath.replace(OssPath.localPath,"");
  217. String modelOssPath = modelFile.getPath().replace(OssPath.localPath, "");
  218. ShellUtil.yunUpload(unzipPath,ossPath);
  219. if(!uploadToOssUtil.existKey(modelOssPath)){
  220. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  221. }
  222. return modelOssPath;
  223. }
  224. private String uploadLasOrPly(String unzipPath ,File modelFile) {
  225. File mntFile = OBJToGLBUtil.lasOrPlyToBin(modelFile);
  226. String ossPath = mntFile.getPath().replace(OssPath.localPath,"");
  227. ShellUtil.yunUpload(mntFile.getPath(),ossPath);
  228. if(!uploadToOssUtil.existKey(ossPath+"/webcloud/cloud.js")){
  229. throw new BusinessException(-1,"缺少cloud.js文件");
  230. }
  231. return ossPath + "/webcloud";
  232. }
  233. @Override
  234. public CommonUpload add(String fileName, String url, String fileSize, String uuid, FileTypeEnum fileTypeEnum, String resultFormat,String replace1, Integer status, String unzipPath, Integer dictId,String sourcePath) {
  235. CommonUpload upload = new CommonUpload();
  236. upload.setFileName(fileName);
  237. upload.setFileUrl(url);
  238. upload.setFileSize(fileSize);
  239. upload.setNewFileName(uuid);
  240. upload.setFileType(fileTypeEnum.getCode());
  241. upload.setFileTypeStr(fileTypeEnum.getMsg());
  242. upload.setFileFormat(resultFormat);
  243. upload.setResultFileFormat(replace1);
  244. upload.setStatus(status);
  245. upload.setSourceFile(sourcePath);
  246. upload.setUnzipPath(unzipPath);
  247. if("osgb".equals(resultFormat)){
  248. upload.setConvertType(1);
  249. }
  250. this.save(upload);
  251. DictFile dictFile = new DictFile();
  252. dictFile.setName(fileName);
  253. dictFile.setTypeKey("media-library");
  254. dictFile.setUploadId(upload.getId());
  255. dictFile.setDictId(dictId);
  256. dictFile.setSysUserId((String) StpUtil.getLoginId());
  257. dictFileService.saveOrUpdate(dictFile);
  258. return upload;
  259. }
  260. @Override
  261. public void updateByPath(Integer uploadId, String url) {
  262. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  263. wrapper.eq(CommonUpload::getId,uploadId);
  264. wrapper.set(CommonUpload::getStatus,1);
  265. wrapper.set(CommonUpload::getFileUrl,url);
  266. this.update(wrapper);
  267. }
  268. @Override
  269. public void updateStatus(Integer uploadId,Integer status) {
  270. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  271. wrapper.eq(CommonUpload::getId,uploadId);
  272. wrapper.set(CommonUpload::getStatus,status);
  273. this.update(wrapper);
  274. }
  275. @Override
  276. public void updateByPath(Integer uploadId, String url,String wgs84 ,String gcj02) {
  277. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  278. wrapper.eq(CommonUpload::getId,uploadId);
  279. wrapper.set(CommonUpload::getStatus,1);
  280. wrapper.set(CommonUpload::getFileUrl,url);
  281. wrapper.set(CommonUpload::getWgs84,wgs84);
  282. wrapper.set(CommonUpload::getGcj02,gcj02);
  283. this.update(wrapper);
  284. }
  285. public static long getDirectorySize(File directory) {
  286. long size = 0;
  287. try {
  288. File[] files = directory.listFiles();
  289. if (files != null) {
  290. for (File file : files) {
  291. if (file.isFile()) {
  292. size += file.length();
  293. } else if (file.isDirectory()) {
  294. size += getDirectorySize(file);
  295. }
  296. }
  297. }
  298. }catch (Exception e){
  299. log.info("getDirectorySize:{}",e);
  300. }
  301. return size;
  302. }
  303. @Override
  304. public List<CommonUpload> getDelData() {
  305. return this.getBaseMapper().getDelData();
  306. }
  307. @Override
  308. public void delByIds(List<Integer> ids) {
  309. this.getBaseMapper().delByIds(ids);
  310. }
  311. @Override
  312. public void updateFileName(Integer uploadId, String name) {
  313. LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
  314. wrapper.eq(CommonUpload::getId,uploadId);
  315. wrapper.set(CommonUpload::getFileName,name);
  316. this.update(wrapper);
  317. }
  318. }