123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package com.fdkankan.manage.service.impl;
- import cn.dev33.satoken.stp.StpUtil;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.json.JSONObject;
- import com.alibaba.fastjson.JSONArray;
- import com.fdkankan.common.util.SecurityUtil;
- import com.fdkankan.manage.common.OssPath;
- import com.fdkankan.manage.common.ResultCode;
- import com.fdkankan.manage.common.ResultData;
- import com.fdkankan.common.util.DateExtUtil;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.manage.constant.FileTypeEnum;
- import com.fdkankan.manage.entity.CommonUpload;
- import com.fdkankan.manage.entity.DictFile;
- import com.fdkankan.manage.exception.BusinessException;
- import com.fdkankan.manage.service.ICommonService;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- import com.fdkankan.manage.service.ICommonUploadService;
- import com.fdkankan.manage.service.IDictFileService;
- import com.fdkankan.manage.util.FileWriterUtil;
- import com.fdkankan.manage.util.OBJToGLBUtil;
- import com.fdkankan.manage.util.ShellUtil;
- import com.fdkankan.manage.vo.response.DictFileVo;
- import com.fdkankan.rabbitmq.util.RabbitMqProducer;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- /**
- * <p>
- * TODO
- * </p>
- *
- * @author dengsixing
- * @since 2022/6/7
- **/
- @Service
- @Slf4j
- public class CommonServiceImpl implements ICommonService {
- @Value("${fyun.host:https://4dkk.4dage.com/}")
- private String ossUrlPrefix;
- @Autowired
- private FYunFileServiceInterface fYunFileServiceInterface;
- @Autowired
- RabbitMqProducer rabbitMqProducer;
- @Override
- public ResultData uploadFile(MultipartFile file) {
- File tempFile = null;
- try {
- String uuid = UUID.randomUUID().toString();
- String originalFilename = file.getOriginalFilename();
- String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
- String ossPath = String.format(OssPath.MANAGE_FILE_PATH, DateUtil.format(Calendar.getInstance()
- .getTime(), DateExtUtil.dateStyle6), uuid + extName);
- tempFile = new File(OssPath.localPath + ossPath);
- file.transferTo(tempFile);
- fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
- String url = this.ossUrlPrefix + ossPath;
- return ResultData.ok(url);
- }catch (Exception e){
- log.info("upload-file-error:{}",e);
- throw new BusinessException(ResultCode.UPLOAD_ERROR);
- }finally {
- if(tempFile != null){
- tempFile.delete();
- }
- }
- }
- @Autowired
- ICommonUploadService commonUploadService;
- @Autowired
- IDictFileService dictFileService;
- @Override
- public ResultData uploadFileNew(MultipartFile file,Integer dictId) {
- if(file.isEmpty() ){
- throw new BusinessException(ResultCode.UPLOAD_ERROR);
- }
- File tempFile = null;
- try {
- String uuid = UUID.randomUUID().toString().replace("-","");
- String originalFilename = file.getOriginalFilename();
- String extName = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
- String ossPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, uuid + extName);
- tempFile = new File(OssPath.localPath + ossPath);
- if(!tempFile.getParentFile().exists()){
- tempFile.getParentFile().mkdirs();
- }
- file.transferTo(tempFile);
- if(extName.equals(".zip")){
- return uploadModelZip(originalFilename.replace(extName, ""),tempFile,dictId);
- }
- fYunFileServiceInterface.uploadFile(tempFile.getPath(), ossPath);
- String url = this.ossUrlPrefix + ossPath;
- FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(extName.replace(".", ""));
- if(fileTypeEnum == null){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
- }
- String format = extName.replace(".", "");
- CommonUpload commonUpload = commonUploadService.add(originalFilename.replace(extName, ""), url, String.valueOf(file.getSize()), uuid, fileTypeEnum, format,format,1,null,dictId);
- tempFile.delete();
- return ResultData.ok(commonUpload);
- }catch (Exception e){
- log.info("upload-file-error:{}",e);
- throw new BusinessException(ResultCode.UPLOAD_ERROR);
- }
- }
- private ResultData uploadModelZip(String oldName,File tempFile,Integer dictId) {
- String unzipPath = tempFile.getParentFile().getPath() +"/result/"+UUID.randomUUID().toString().replace("-","");
- ShellUtil.unZip(tempFile.getPath(),unzipPath);
- File unZipFile = new File(unzipPath);
- if(!unZipFile.exists() || !unZipFile.isDirectory()){
- throw new BusinessException(ResultCode.UNZIP_ERROR);
- }
- List<File> fileList = new ArrayList<>();
- FileWriterUtil.getCanRunList(fileList,unZipFile);
- if(fileList.size() <=0){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- File modelFile = fileList.get(0);
- if(FileWriterUtil.isChinese(modelFile.getName())){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
- }
- String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
- String url = null;
- String resultFormat = modelFileFormat;
- switch (modelFileFormat){
- case "obj" : resultFormat = "glb";
- url = uploadObjOss(unzipPath,modelFile);break;
- case "laz" :
- case "shp" : url = uploadOss(unzipPath,modelFile); break;
- case "b3dm" : url = uploadB3dm(unzipPath,modelFile); break;
- case "las" :
- case "ply" : url = uploadLasOrPly(unzipPath,modelFile);break;
- case "osgb": resultFormat = "b3dm";
- uploadOsgb(unzipPath,modelFile) ;break;
- default: break;
- }
- FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
- if(fileTypeEnum == null){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
- }
- Integer status = StringUtils.isNotBlank(url) ?1:0;
- CommonUpload commonUpload = commonUploadService.add(oldName,url, String.valueOf(tempFile.length()),
- null, fileTypeEnum, modelFileFormat,resultFormat,status,unzipPath,dictId);
- tempFile.delete();
- return ResultData.ok(commonUpload);
- }
- private String uploadObjOss(String unzipPath, File modelFile) {
- OBJToGLBUtil.checkObj(modelFile.getPath());
- String localGlbPath = modelFile.getPath().replace(".obj",".glb");
- OBJToGLBUtil.objToGlb2(modelFile.getPath(),localGlbPath);
- String ossPath = unzipPath.replace(OssPath.localPath,"");
- ShellUtil.yunUpload(unzipPath,ossPath);
- if(!fYunFileServiceInterface.fileExist(localGlbPath.replace(OssPath.localPath,""))){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- FileUtil.del(unzipPath);
- return ossUrlPrefix + localGlbPath.replace(OssPath.localPath,"");
- }
- private String uploadB3dm(String unzipPath, File modelFile) {
- String b3dmJsonPath = FileWriterUtil.checkB3dmTileset(new File(unzipPath));
- if(b3dmJsonPath == null){
- throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
- }
- String ossPath = b3dmJsonPath.replace(OssPath.localPath, "");
- uploadOss(unzipPath,modelFile);
- if(!fYunFileServiceInterface.fileExist(ossPath)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- return ossUrlPrefix + ossPath;
- }
- private void uploadOsgb(String unzipPath, File modelFile) {
- //容器不支持转化,通知fusion容器执行
- HashMap<String,String > map = new HashMap<>();
- map.put("path",unzipPath);
- rabbitMqProducer.sendByWorkQueue("queue-model-osgbToB3dm",map);
- }
- private String uploadOss(String unzipPath,File modelFile) {
- String ossPath = unzipPath.replace(OssPath.localPath,"");
- String modelOssPath = modelFile.getPath().replace(OssPath.localPath, "");
- ShellUtil.yunUpload(unzipPath,ossPath);
- if(!fYunFileServiceInterface.fileExist(modelOssPath)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- FileUtil.del(unzipPath);
- return ossUrlPrefix + modelOssPath;
- }
- private String uploadLasOrPly(String unzipPath ,File modelFile) {
- File mntFile = OBJToGLBUtil.lasOrPlyToBin(modelFile);
- String ossPath = mntFile.getPath().replace(OssPath.localPath,"");
- ShellUtil.yunUpload(mntFile.getPath(),ossPath);
- if(!fYunFileServiceInterface.fileExist(ossPath+"/webcloud/cloud.js")){
- throw new BusinessException(-1,"缺少cloud.js文件");
- }
- FileUtil.del(unzipPath);
- return ossUrlPrefix + ossPath + "/webcloud";
- }
- }
|