123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- package com.fdkankan.fusion.service.impl;
- import cn.dev33.satoken.stp.StpUtil;
- import cn.hutool.core.io.FileUtil;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.fdkankan.fusion.common.OssPath;
- import com.fdkankan.fusion.common.ResultCode;
- import com.fdkankan.fusion.common.ResultData;
- import com.fdkankan.fusion.common.enums.FileTypeEnum;
- import com.fdkankan.fusion.common.util.*;
- import com.fdkankan.fusion.config.CacheUtil;
- import com.fdkankan.fusion.entity.CommonUpload;
- import com.fdkankan.fusion.entity.Dict;
- import com.fdkankan.fusion.entity.DictFile;
- import com.fdkankan.fusion.exception.BusinessException;
- import com.fdkankan.fusion.mapper.ICommonUploadMapper;
- import com.fdkankan.fusion.mq.consumer.OsgbToB3dmConsumer;
- import com.fdkankan.fusion.service.ICommonUploadService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.fusion.service.IDictFileService;
- import com.fdkankan.fusion.service.IDictService;
- 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 java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.UUID;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2024-12-06
- */
- @Service
- @Slf4j
- public class CommonUploadServiceImpl extends ServiceImpl<ICommonUploadMapper, CommonUpload> implements ICommonUploadService {
- @Autowired
- ICommonUploadService commonUploadService;
- @Autowired
- IDictFileService dictFileService;
- @Autowired
- private UploadToOssUtil uploadToOssUtil;
- @Value("${upload.query-path}")
- private String ossUrlPrefix;
- @Autowired
- IDictService dictService;
- @Override
- public ResultData uploadFileNew( Integer dictId,MultipartFile file) {
- if(file == null || 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();
- if(dictId != null){
- Dict dict = dictService.getById(dictId);
- if(dict == null ){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- if("animation".equals(dict.getUseType()) && !(extName.equalsIgnoreCase(".zip") || extName.equalsIgnoreCase(".glb"))){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
- }
- }
- 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);
- }
- ShellUtil.yunUpload(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,null);
- return ResultData.ok(commonUpload);
- }catch ( BusinessException e){
- log.info("upload-file-error:{}",e);
- throw e;
- }catch (Exception e){
- log.info("upload-file-error:{}",e);
- throw new BusinessException(ResultCode.UPLOAD_ERROR);
- }finally {
- if(tempFile != null){
- tempFile.delete();
- }
- }
- }
- private ResultData uploadModelZip(String oldName,File file,Integer dictId) {
- File unZipFile = null;
- try {
- Long fileSize = file.length();
- String ossZipPath = String.format(OssPath.MANAGE_MODEL_FILE_PATH, UUID.randomUUID().toString().replace("-", ""));
- String sourcePath = String.format(OssPath.MANAGE_SOURCE_FILE_PATH, UUID.randomUUID().toString().replace("-", ""));
- String unzipPath = CacheUtil.basePath + ossZipPath;
- ShellUtil.yunUpload(file.getPath(),sourcePath);
- ShellUtil.unZip(file.getPath(),unzipPath);
- //FileUtil.copyContent(file,new File(unzipPath),true);
- 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);
- String modelFileFormat = modelFile.getName().split("\\.")[1].toLowerCase();
- FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(modelFileFormat);
- if(fileTypeEnum == null){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
- }
- if(dictId != null){
- Dict dict = dictService.getById(dictId);
- if(dict == null ){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- if("animation".equals(dict.getUseType()) && !(modelFileFormat.equals("glb") || modelFileFormat.equals("obj"))){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR2);
- }
- }
- if(FileWriterUtil.isChinese(modelFile.getName())){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
- }
- if(FileWriterUtil.isChinese(modelFile.getPath())){
- throw new BusinessException(ResultCode.FILE_TYPE_ERROR23);
- }
- String url = null;
- String resultFormat = modelFileFormat;
- switch (modelFileFormat){
- case "obj" : resultFormat = "obj";
- url = uploadObjOss(unzipPath,modelFile);break;
- case "glb" : url = uploadOss(unzipPath,modelFile);break;
- case "laz" : url = uploadLazOss(unzipPath,modelFile); break;
- 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";
- break;
- default: break;
- }
- Integer status = StringUtils.isNotBlank(url) ?1:-1;
- url = StringUtils.isNotBlank(url) ?ossUrlPrefix + url:null;
- sourcePath = StringUtils.isNotBlank(sourcePath) ?ossUrlPrefix + sourcePath:null;
- CommonUpload commonUpload = commonUploadService.add(oldName,url, String.valueOf(fileSize),
- null, fileTypeEnum, modelFileFormat,resultFormat,status,unZipFile.getPath(),dictId,sourcePath);
- if("osgb".equals(modelFileFormat)){
- uploadOsgb(commonUpload.getId()) ;
- commonUpload = commonUploadService.getById(commonUpload.getId());
- }
- return ResultData.ok(commonUpload);
- } catch (BusinessException e){
- throw e;
- }catch (Exception e){
- log.info("上传失败:{}",e);
- throw new BusinessException(ResultCode.UPLOAD_ERROR);
- }finally {
- if(unZipFile != null){
- unZipFile.delete();
- }
- }
- }
- 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(!uploadToOssUtil.fileExist(localGlbPath.replace(OssPath.localPath,""))){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- return 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(!uploadToOssUtil.fileExist(ossPath)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- return ossPath;
- }
- @Autowired
- OsgbToB3dmConsumer osgbToB3dmConsumer;
- private void uploadOsgb(Integer uploadId) {
- osgbToB3dmConsumer.consumerQueue(uploadId);
- }
- private String uploadLazOss(String unzipPath,File modelFile) {
- String ossPath = unzipPath.replace(OssPath.localPath,"");
- String modelOssPath = modelFile.getPath().replace(OssPath.localPath, "");
- ShellUtil.yunUpload(unzipPath,ossPath);
- if(!uploadToOssUtil.fileExist(modelOssPath)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- return ossPath;
- }
- 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(!uploadToOssUtil.existKey(modelOssPath)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- return 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(!uploadToOssUtil.existKey(ossPath+"/webcloud/cloud.js")){
- throw new BusinessException(-1,"缺少cloud.js文件");
- }
- return ossPath + "/webcloud";
- }
- @Override
- 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) {
- CommonUpload upload = new CommonUpload();
- upload.setFileName(fileName);
- upload.setFileUrl(url);
- upload.setFileSize(fileSize);
- upload.setNewFileName(uuid);
- upload.setFileType(fileTypeEnum.getCode());
- upload.setFileTypeStr(fileTypeEnum.getMsg());
- upload.setFileFormat(resultFormat);
- upload.setResultFileFormat(replace1);
- upload.setStatus(status);
- upload.setSourceFile(sourcePath);
- upload.setUnzipPath(unzipPath);
- if("osgb".equals(resultFormat)){
- upload.setConvertType(1);
- }
- this.save(upload);
- DictFile dictFile = new DictFile();
- dictFile.setName(fileName);
- dictFile.setTypeKey("media-library");
- dictFile.setUploadId(upload.getId());
- dictFile.setDictId(dictId);
- dictFile.setSysUserId((String) StpUtil.getLoginId());
- dictFileService.saveOrUpdate(dictFile);
- return upload;
- }
- @Override
- public void updateByPath(Integer uploadId, String url) {
- LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CommonUpload::getId,uploadId);
- wrapper.set(CommonUpload::getStatus,1);
- wrapper.set(CommonUpload::getFileUrl,url);
- this.update(wrapper);
- }
- @Override
- public void updateStatus(Integer uploadId,Integer status) {
- LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CommonUpload::getId,uploadId);
- wrapper.set(CommonUpload::getStatus,status);
- this.update(wrapper);
- }
- @Override
- public void updateByPath(Integer uploadId, String url,String wgs84 ,String gcj02) {
- LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CommonUpload::getId,uploadId);
- wrapper.set(CommonUpload::getStatus,1);
- wrapper.set(CommonUpload::getFileUrl,url);
- wrapper.set(CommonUpload::getWgs84,wgs84);
- wrapper.set(CommonUpload::getGcj02,gcj02);
- this.update(wrapper);
- }
- public static long getDirectorySize(File directory) {
- long size = 0;
- try {
- File[] files = directory.listFiles();
- if (files != null) {
- for (File file : files) {
- if (file.isFile()) {
- size += file.length();
- } else if (file.isDirectory()) {
- size += getDirectorySize(file);
- }
- }
- }
- }catch (Exception e){
- log.info("getDirectorySize:{}",e);
- }
- return size;
- }
- @Override
- public List<CommonUpload> getDelData() {
- return this.getBaseMapper().getDelData();
- }
- @Override
- public void delByIds(List<Integer> ids) {
- this.getBaseMapper().delByIds(ids);
- }
- @Override
- public void updateFileName(Integer uploadId, String name) {
- LambdaUpdateWrapper<CommonUpload> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CommonUpload::getId,uploadId);
- wrapper.set(CommonUpload::getFileName,name);
- this.update(wrapper);
- }
- }
|