package com.fdkankan.fusion.service.impl; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.util.*; import com.fdkankan.fusion.entity.Model; import com.fdkankan.fusion.exception.BusinessException; import com.fdkankan.fusion.service.IModelService; import com.fdkankan.redis.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @Service @Slf4j public class ThreadService { @Autowired RedisUtil redisUtil; @Autowired IModelService modelService; @Autowired UploadToOssUtil uploadToOssUtil; @Value("${spring.profiles.active}") private String environment; @Value("${upload.query-path}") private String queryPath; @Async public void uploadModelObj(String fileName, String objPath,File newObjFile,Model model){ File objPathFile = null; File mntFile = null; File b3dmFile = null; File osgbFile = null; try { if(fileName.toLowerCase().endsWith(".zip")){ ShellUtil.unZip(newObjFile.getPath(),objPath); } objPathFile = new File(objPath ); if(!objPathFile.isDirectory()){ throw new BusinessException(-1,"解压错误"); } List fileList = new ArrayList<>(); FileWriterUtil.getCanRunList(fileList,objPathFile); if(fileList.size() <=0){ throw new BusinessException(-1,"可上传文件不存在"); } File file1 = fileList.get(0); if(file1 == null){ throw new BusinessException(-1,"可上传文件不存在"); } if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){ throw new BusinessException(-1,"压缩包中文"); } String b3dmJsonPath = null; if(file1.getName().endsWith(".b3dm") ){ b3dmJsonPath = FileWriterUtil.checkB3dmTileset(objPathFile); if(b3dmJsonPath == null){ throw new BusinessException(-1,"缺少tileset.json文件"); } } redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20"); String ossPath = String.format(FilePath.MODEL_OSS_PATH,environment, model.getModelId()); String name = file1.getName(); if(name.contains("obj") || name.contains("OBJ")){ model.setModelDateType("b3dm"); model.setModelType("b3dm"); String localPath = file1.getParentFile().getPath()+"/b3dm"; OBJToGLBUtil.objToB3dm(file1.getPath(),localPath); redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65"); ossPath = localPath.replace("/mnt/",""); ShellUtil.yunUpload(localPath,ossPath); model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath+"/tileset.json"))); } if(name.contains(".ply")){ model.setModelDateType("ply"); model.setModelType("ply"); } if(name.contains(".las")){ model.setModelDateType("las"); model.setModelType("las"); } if("las".equals(model.getModelType()) || "ply".equals(model.getModelType()) ){ mntFile = OBJToGLBUtil.lasOrPlyToBin(file1); ossPath = mntFile.getPath().replace("/mnt/","")+"/webcloud"; redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65"); uploadToOssUtil.uploadFileOss(mntFile ); model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + ossPath))); if(!uploadToOssUtil.existKey(ossPath+"/cloud.js")){ throw new BusinessException(-1,"缺少cloud.js文件"); } } String b3dmPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/"); if(name.contains(".osgb")){ model.setModelDateType("osgb"); model.setModelType("b3dm"); redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60"); String localPath = OBJToGLBUtil.OsgbToB3dm(objPathFile); osgbFile = new File(localPath.replace("mnt/fusion/b3dm","/mnt/fusion/osgb")); b3dmFile = new File(localPath); b3dmJsonPath = FileWriterUtil.checkB3dmTileset(b3dmFile); if(b3dmJsonPath == null){ throw new BusinessException(-1,"缺少tileset.json文件"); } redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"80"); String replace = b3dmJsonPath.replace(FilePath.MNT_BASE_PATH, "fusion/"+environment+"/"); File file2 = new File(replace); ShellUtil.yunUpload(localPath,file2.getParent()); model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + replace)))); } if(name.contains(".b3dm") && b3dmJsonPath != null){ model.setModelDateType("b3dm"); model.setModelType("b3dm"); redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"60"); ShellUtil.yunUpload(objPathFile.getPath(),b3dmPath); model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + b3dmJsonPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/"))))); } if(name.contains(".laz")){ model.setModelDateType("laz"); model.setModelType("laz"); redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65"); ShellUtil.yunUpload(objPathFile.getPath(),objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/")); model.setModelGlbUrl((JSONArray.toJSONString(Arrays.asList(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") )))); } model.setFileNewName(queryPath + objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/") +"/" + fileName); setCreateStatus(model,1); modelService.saveOrUpdate(model); redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"100"); }catch (Exception e){ setCreateStatus(model,-1); log.error("uploadObj--ThreadError-modeId:{},error:{}",model.getModelId(),e); }finally { if(newObjFile!=null){ FileUtil.del(newObjFile); } if(objPathFile!=null){ FileUtil.del(objPathFile); } if(mntFile!=null){ FileUtil.del(mntFile.getParentFile()); } if(b3dmFile != null){ FileUtil.del(b3dmFile.getParentFile()); } if(osgbFile != null){ FileUtil.del(osgbFile.getParentFile()); } } } private void setCreateStatus(Model model,Integer status){ String redisKey = RedisKeyUtil.modelCancelUpload+model.getModelId(); if(redisUtil.hasKey(redisKey)){ if(redisUtil.get(redisKey).equals("-2")){ return; } } model.setCreateStatus(status); modelService.saveOrUpdate(model); if(status != 1){ redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),status.toString()); } } @Async public void objToB3dm(String num,Integer type) { String objPath = String.format(FilePath.OBJ_LOCAL_PATH ,environment,num); ShellUtil.yunDownload(String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh" ,objPath); List localGlbPaths = new ArrayList<>(); List ossGlbPaths = new ArrayList<>(); File localFile = new File(objPath); Long size = 0L; this.toGlB(localFile, localGlbPaths,size); String sizeStr = FileWriterUtil.setFileSize(size); if(!localGlbPaths.isEmpty()){ for (String localGlbPath : localGlbPaths) { String ossPath = localGlbPath.replace("/mnt/",""); ShellUtil.yunUpload(localGlbPath,ossPath); ossGlbPaths.add(queryPath +ossPath+"/tileset.json"); } log.info("转换完成修改数据库:{}:{}:{}",ossGlbPaths,num,type); String ossGlbPath = JSONArray.toJSONString(ossGlbPaths); LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(Model::getType,type); wrapper.eq(Model::getNum,num); wrapper.set(Model::getModelGlbUrl,ossGlbPath); wrapper.set(Model::getModelDateType,"b3dm"); wrapper.set(Model::getModelType,"b3dm"); wrapper.set(Model::getCreateStatus,1); wrapper.set(Model::getModelSize,sizeStr); modelService.update(wrapper); } } private void toGlB(File localFile, List localGlbPath,Long size) { File[] files = localFile.listFiles(); for (File file : files) { if(file.isDirectory()){ toGlB(file,localGlbPath,size); } if(file.getPath().contains("lod_") ){ if(file.getPath().contains("lod_0") && file.getName().contains(".obj")){ String glbPath = OBJToGLBUtil.objToB3dm(file.getPath(),file.getParentFile().getPath()+"/b3dm"); size += file.length(); localGlbPath.add(glbPath); } continue; } if(file.getName().contains(".obj")){ String glbPath = OBJToGLBUtil.objToB3dm(file.getPath(),file.getParentFile().getPath()+"/b3dm"); size += file.length(); localGlbPath.add(glbPath); } } } }