package com.fdkankan.fusion.service.impl; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSONArray; 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 glbOssPath = String.format(FilePath.GLB_OSS_PATH,environment, model.getModelId()); String name = file1.getName(); if(name.contains("obj") || name.contains("OBJ")){ glbOssPath = glbOssPath.replace("mesh.glb",file1.getName().replace(".obj",".glb")); model.setModelDateType("obj"); model.setModelType("glb"); OBJToGLBUtil.objToGlb2(file1.getPath(), file1.getPath().replace(".obj",".glb")); redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65"); uploadToOssUtil.uploadOss(file1.getPath().replace(".obj",".glb"),glbOssPath); if(!uploadToOssUtil.existKey(glbOssPath)){ throw new BusinessException(-1,"缺少.glb文件"); } } 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); glbOssPath = mntFile.getPath().replace("/mnt/","")+"/webcloud"; redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65"); uploadToOssUtil.uploadFileOss(mntFile ); if(!uploadToOssUtil.existKey(glbOssPath+"/cloud.js")){ throw new BusinessException(-1,"缺少cloud.js文件"); } } model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath + glbOssPath))); String b3dmPath = objPathFile.getPath().replace(FilePath.LOCAL_BASE_PATH,"fusion/"); if(name.contains(".osgb")){ model.setModelDateType("osgb"); model.setModelType("osgb"); 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/") + "/cloud.js")))); } 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()); } } }