Procházet zdrojové kódy

三维模型异步上传

lyhzzz před 2 roky
rodič
revize
63987a2af6

+ 1 - 0
src/main/java/com/fdkankan/fusion/common/util/RedisKeyUtil.java

@@ -3,5 +3,6 @@ package com.fdkankan.fusion.common.util;
 public class RedisKeyUtil {
 
     public static String mergerVideoKey = "fusion:merger:video:";
+    public static String modelUpload = "fusion:model:upload:";
     public static String loginToken = "fusion:token:";
 }

+ 10 - 2
src/main/java/com/fdkankan/fusion/controller/ModelController.java

@@ -41,8 +41,16 @@ public class ModelController extends BaseController{
     @PostMapping("/uploadObj")
     public ResultData uploadObj(@RequestParam(required = false) MultipartFile file) throws Exception {
         String username = JwtUtil.getUsername(getToken());
-        modelService.uploadObj(file,username);
-        return ResultData.ok();
+        return ResultData.ok(modelService.uploadObj(file,username));
+    }
+
+    @GetMapping("/uploadObjProgress")
+    public ResultData uploadAddVideoProgress(@RequestParam(required = false) Integer modelId) throws Exception {
+        String code = modelService.uploadObjProgress(modelId);
+        if("-1".equals(code)){
+            return ResultData.error("上传失败");
+        }
+        return ResultData.ok(Integer.valueOf(code));
     }
 
     @PostMapping("/list")

+ 3 - 3
src/main/java/com/fdkankan/fusion/service/IModelService.java

@@ -4,10 +4,8 @@ import com.fdkankan.fusion.common.PageInfo;
 import com.fdkankan.fusion.entity.Model;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.fdkankan.fusion.request.ModelPram;
-import com.fdkankan.fusion.request.ScenePram;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.IOException;
 import java.util.List;
 
 /**
@@ -20,7 +18,7 @@ import java.util.List;
  */
 public interface IModelService extends IService<Model> {
 
-    void uploadObj(MultipartFile file, String username) throws Exception;
+    Model uploadObj(MultipartFile file, String username) throws Exception;
 
     PageInfo pageList(ModelPram param, String token);
 
@@ -39,4 +37,6 @@ public interface IModelService extends IService<Model> {
     Model getIsNullNewByNum(String num);
 
     Object getInfo(Integer modelId);
+
+    String uploadObjProgress(Integer modelId);
 }

+ 67 - 70
src/main/java/com/fdkankan/fusion/service/impl/CaseVideoServiceImpl.java

@@ -87,77 +87,74 @@ public class CaseVideoServiceImpl extends ServiceImpl<ICaseVideoMapper, CaseVide
     }
 
     private void runThread(MultipartFile[] files,CaseVideoFolder videoFolder,ICaseVideoService caseVideoService){
-        new Runnable() {
-            @Override
-            public void run() {
-                String mergeVideoUrl = null;
-                if(StringUtils.isNotBlank(videoFolder.getVideoMergeUrl())){
-                    mergeVideoUrl = videoFolder.getVideoMergeUrl().replace(queryPath,"/home/");
-                    uploadToOssUtil.downFormAli(videoFolder.getVideoMergeUrl().replace(queryPath,""),mergeVideoUrl);
-                    uploadService.deleteOssUrl(videoFolder.getVideoMergeUrl());
-                }
-                if(StringUtils.isNotBlank(videoFolder.getVideoMergeUrl())){
-                    uploadService.deleteOssUrl(videoFolder.getVideoFolderCover());
-                }
-                caseVideoService.deleteByFolderId(videoFolder.getVideoFolderId());
-                redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"10");
-                //替换文件夹中文件
-                List<CaseVideo> videoList = new ArrayList<>();
-                Integer sort = 1;
-                LinkedHashSet<String> filePathSet = new LinkedHashSet<>();
-                LinkedHashSet<String> filePathSetAll = new LinkedHashSet<>();
-                for (MultipartFile file : files) {
-                    String ossPath = uploadService.uploadFile(file, true, String.format(FilePath.VIDEO_OSS_PATH,videoFolder.getVideoFolderId())+"/",filePathSet);
-                    String suffixName = ossPath.substring(ossPath.lastIndexOf("."));
-
-                    CaseVideo caseVideo = new CaseVideo();
-                    caseVideo.setFolderId(videoFolder.getVideoFolderId());
-                    caseVideo.setVideoPath(ossPath);
-                    caseVideo.setVideoName(file.getName());
-                    caseVideo.setVideoCover(ossPath.replace(suffixName,".jpg"));
-                    caseVideo.setSort(sort);
-                    videoList.add(caseVideo);
-                    sort ++;
-                }
-                caseVideoService.saveBatch(videoList);
-                if(videoList.size() <=0){
-                    return;
-                }
-
-                redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"20");
-                if(StringUtils.isNotBlank(mergeVideoUrl)){
-                    filePathSetAll.add(mergeVideoUrl);
-                }
-                filePathSetAll.addAll(filePathSet);
-                String mergeLocalName = VideoUtil.mergeVideo(filePathSetAll, FilePath.VIDEO_LOCAL_PATH);
-                redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"30");
-                if(mergeLocalName == null){
-                    updateCaseVideoStatus(videoFolder.getVideoFolderId(),-1);
-                    redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"-1");
-                    return;
-                }
-                String mergeLocalPath =  FilePath.VIDEO_LOCAL_PATH +"/"+mergeLocalName;
-                String ossKey =  String.format(FilePath.VIDEO_OSS_PATH,videoFolder.getVideoFolderId()) +"/"+mergeLocalName;
-
-                String mergeLocalPathImg =  FilePath.VIDEO_LOCAL_PATH +"/"+mergeLocalName.replace(".mp4",".jpg");
-                String ossKeyImg =  String.format(FilePath.VIDEO_OSS_PATH,videoFolder.getVideoFolderId()) +"/"+mergeLocalName.replace(".mp4",".jpg");
-                uploadToOssUtil.uploadOss(mergeLocalPath,ossKey);
-                uploadToOssUtil.uploadOss(mergeLocalPathImg,ossKeyImg);
-                if(!uploadToOssUtil.existKey(ossKey)&& !uploadToOssUtil.existKey(ossKeyImg)){
-                    updateCaseVideoStatus(videoFolder.getVideoFolderId(),-1);
-                    redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"-1");
-                    return;
-                }
-                redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"70");
-
-                videoFolder.setVideoMergeUrl(queryPath + ossKey);
-                videoFolder.setVideoFolderCover(queryPath + ossKeyImg);
-                videoFolderService.updateById(videoFolder);
-              //  FileUtil.del(FilePath.VIDEO_LOCAL_PATH);
-                redisUtil.set(RedisKeyUtil.mergerVideoKey+videoFolder.getVideoFolderId(),"100");
-                updateCaseVideoStatus(videoFolder.getVideoFolderId(),1);
+        ((Runnable) () -> {
+            String mergeVideoUrl = null;
+            if (StringUtils.isNotBlank(videoFolder.getVideoMergeUrl())) {
+                mergeVideoUrl = videoFolder.getVideoMergeUrl().replace(queryPath, "/home/");
+                uploadToOssUtil.downFormAli(videoFolder.getVideoMergeUrl().replace(queryPath, ""), mergeVideoUrl);
+                uploadService.deleteOssUrl(videoFolder.getVideoMergeUrl());
             }
-        }.run();
+            if (StringUtils.isNotBlank(videoFolder.getVideoMergeUrl())) {
+                uploadService.deleteOssUrl(videoFolder.getVideoFolderCover());
+            }
+            caseVideoService.deleteByFolderId(videoFolder.getVideoFolderId());
+            redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "10");
+            //替换文件夹中文件
+            List<CaseVideo> videoList = new ArrayList<>();
+            Integer sort = 1;
+            LinkedHashSet<String> filePathSet = new LinkedHashSet<>();
+            LinkedHashSet<String> filePathSetAll = new LinkedHashSet<>();
+            for (MultipartFile file : files) {
+                String ossPath = uploadService.uploadFile(file, true, String.format(FilePath.VIDEO_OSS_PATH, videoFolder.getVideoFolderId()) + "/", filePathSet);
+                String suffixName = ossPath.substring(ossPath.lastIndexOf("."));
+
+                CaseVideo caseVideo = new CaseVideo();
+                caseVideo.setFolderId(videoFolder.getVideoFolderId());
+                caseVideo.setVideoPath(ossPath);
+                caseVideo.setVideoName(file.getName());
+                caseVideo.setVideoCover(ossPath.replace(suffixName, ".jpg"));
+                caseVideo.setSort(sort);
+                videoList.add(caseVideo);
+                sort++;
+            }
+            caseVideoService.saveBatch(videoList);
+            if (videoList.size() <= 0) {
+                return;
+            }
+
+            redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "20");
+            if (StringUtils.isNotBlank(mergeVideoUrl)) {
+                filePathSetAll.add(mergeVideoUrl);
+            }
+            filePathSetAll.addAll(filePathSet);
+            String mergeLocalName = VideoUtil.mergeVideo(filePathSetAll, FilePath.VIDEO_LOCAL_PATH);
+            redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "30");
+            if (mergeLocalName == null) {
+                updateCaseVideoStatus(videoFolder.getVideoFolderId(), -1);
+                redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "-1");
+                return;
+            }
+            String mergeLocalPath = FilePath.VIDEO_LOCAL_PATH + "/" + mergeLocalName;
+            String ossKey = String.format(FilePath.VIDEO_OSS_PATH, videoFolder.getVideoFolderId()) + "/" + mergeLocalName;
+
+            String mergeLocalPathImg = FilePath.VIDEO_LOCAL_PATH + "/" + mergeLocalName.replace(".mp4", ".jpg");
+            String ossKeyImg = String.format(FilePath.VIDEO_OSS_PATH, videoFolder.getVideoFolderId()) + "/" + mergeLocalName.replace(".mp4", ".jpg");
+            uploadToOssUtil.uploadOss(mergeLocalPath, ossKey);
+            uploadToOssUtil.uploadOss(mergeLocalPathImg, ossKeyImg);
+            if (!uploadToOssUtil.existKey(ossKey) && !uploadToOssUtil.existKey(ossKeyImg)) {
+                updateCaseVideoStatus(videoFolder.getVideoFolderId(), -1);
+                redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "-1");
+                return;
+            }
+            redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "70");
+
+            videoFolder.setVideoMergeUrl(queryPath + ossKey);
+            videoFolder.setVideoFolderCover(queryPath + ossKeyImg);
+            videoFolderService.updateById(videoFolder);
+            //  FileUtil.del(FilePath.VIDEO_LOCAL_PATH);
+            redisUtil.set(RedisKeyUtil.mergerVideoKey + videoFolder.getVideoFolderId(), "100");
+            updateCaseVideoStatus(videoFolder.getVideoFolderId(), 1);
+        }).run();
     }
 
     private void updateCaseVideoStatus(Integer folderId, Integer status) {

+ 97 - 80
src/main/java/com/fdkankan/fusion/service/impl/ModelServiceImpl.java

@@ -1,9 +1,6 @@
 package com.fdkankan.fusion.service.impl;
 import java.io.File;
-import java.io.IOException;
-import java.math.BigDecimal;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -14,21 +11,17 @@ import com.fdkankan.fusion.common.PageInfo;
 import com.fdkankan.fusion.common.util.*;
 import com.fdkankan.fusion.common.FilePath;
 import com.fdkankan.fusion.common.ResultCode;
-import com.fdkankan.fusion.entity.CaseEntity;
-import com.fdkankan.fusion.entity.CaseNumEntity;
-import com.fdkankan.fusion.entity.FusionNum;
-import com.fdkankan.fusion.entity.Model;
+import com.fdkankan.fusion.entity.*;
 import com.fdkankan.fusion.exception.BusinessException;
 import com.fdkankan.fusion.mapper.IModelMapper;
 import com.fdkankan.fusion.request.ModelPram;
-import com.fdkankan.fusion.request.ScenePram;
 import com.fdkankan.fusion.response.SceneVo;
 import com.fdkankan.fusion.service.ICaseNumService;
 import com.fdkankan.fusion.service.ICaseService;
 import com.fdkankan.fusion.service.IFusionNumService;
 import com.fdkankan.fusion.service.IModelService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import jdk.nashorn.tools.Shell;
+import com.fdkankan.redis.util.RedisUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -36,8 +29,6 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.xml.transform.Result;
-
 /**
  * <p>
  *  服务实现类
@@ -59,6 +50,9 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
     ICaseService caseService;
     @Autowired
     IFusionNumService fusionNumService;
+    @Autowired
+    RedisUtil redisUtil;
+
     @Value("${local.obj_path}")
     private String OBJ_PATH;
     @Value("${local.glb_path}")
@@ -67,7 +61,7 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
     private String queryPath;
 
     @Override
-    public void uploadObj(MultipartFile file, String username) throws Exception {
+    public Model uploadObj(MultipartFile file, String username) throws Exception {
         if(file.isEmpty()){
             throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
         }
@@ -82,7 +76,7 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
         if(com.fdkankan.fusion.common.util.StringUtils.isChinese(fileName)){
             throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
         }
-        if(!fileName.toLowerCase().contains("zip") && !fileName.toLowerCase().contains("rar")){
+        if(!fileName.toLowerCase().contains("zip")){
             throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
         }
         //获取文件后缀名
@@ -93,81 +87,95 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
         model.setModelSize(FileWriterUtil.setFileSize(file.getSize()));
         model.setUserName(username);
         this.save(model);
+        redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"0");
+
         File newObjFile = null;
         File objPathFile = null;
-        File mntFile = null;
         String objPath = String.format(OBJ_PATH , "modelId_"+model.getModelId()) ;
-        try {
-            String glbOssPath = String.format(FilePath.GLB_OSS_PATH, model.getModelId());
 
-            newObjFile = new File(objPath +"/" + fileName);
-            if(!newObjFile.getParentFile().exists()){
-                newObjFile.mkdirs();
-            }
-            file.transferTo(newObjFile);
-            if(fileName.toLowerCase().contains("zip")){
-                ShellUtil.unZip(newObjFile.getPath(),objPath);
-            }
-            if(fileName.toLowerCase().contains("rar")){
-                ShellUtil.unRar(newObjFile.getPath(),objPath);
-            }
-            objPathFile = new File(objPath );
-            if(!objPathFile.isDirectory()){
-                throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
-            }
-            List<File> fileList = new ArrayList<>();
-            FileWriterUtil.getCanRunList(fileList,objPathFile);
+        newObjFile = new File(objPath +"/" + fileName);
+        if(!newObjFile.getParentFile().exists()){
+            newObjFile.mkdirs();
+        }
+        file.transferTo(newObjFile);
+        if(fileName.toLowerCase().contains("zip")){
+            ShellUtil.unZip(newObjFile.getPath(),objPath);
+        }
+        objPathFile = new File(objPath );
+        if(!objPathFile.isDirectory()){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
+        }
+        List<File> fileList = new ArrayList<>();
+        FileWriterUtil.getCanRunList(fileList,objPathFile);
 
-            if(fileList.size() != 1){
-                throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
-            }
-            File file1 = fileList.get(0);
-            if(file1 == null){
-                throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
-            }
+        if(fileList.size() != 1){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
+        }
+        File file1 = fileList.get(0);
+        if(file1 == null){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
+        }
+        if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
+        }
+        redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20");
+        runThread(file1,objPath,model,newObjFile,objPathFile,this);
+        return model;
+    }
 
-            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.objToGlb(objPath,name,  file1.getPath().replace("obj","glb"));
-                uploadToOssUtil.uploadOss(file1.getPath(),glbOssPath);
-            }
-            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";
-                uploadToOssUtil.uploadFileOss(mntFile );
-            }
+    private void runThread(File file1,String objPath,Model model,File newObjFile,File objPathFile,IModelService modelService){
+        new Thread() {
+            @Override
+            public void run() {
+                File mntFile = null;
+                try {
+                    String glbOssPath = String.format(FilePath.GLB_OSS_PATH, model.getModelId());
 
-            model.setModelObjUrl(objPath);
-            model.setModelGlbUrl(queryPath + glbOssPath);
-            model.setCreateStatus(1);  //上传成功
-            this.saveOrUpdate(model);
-        }catch (Exception e){
-            model.setCreateStatus(-1);
-            this.saveOrUpdate(model);
-            throw e;
-        }finally {
-            if(newObjFile!=null){
-                FileUtil.del(newObjFile);
-            }
-            if(objPathFile!=null){
-                FileUtil.del(objPathFile);
-            }
-            if(mntFile!=null){
-                FileUtil.del(mntFile.getParentFile());
-            }
-        }
+                    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.objToGlb(objPath,name,  file1.getPath().replace("obj","glb"));
+                        redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
+                        uploadToOssUtil.uploadOss(file1.getPath(),glbOssPath);
+                    }
+                    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(),"665");
+                        uploadToOssUtil.uploadFileOss(mntFile );
+                    }
 
+                    model.setModelObjUrl(objPath);
+                    model.setModelGlbUrl(queryPath + glbOssPath);
+                    model.setCreateStatus(1);  //上传成功
+                    modelService.saveOrUpdate(model);
+                    redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"100");
+                }catch (Exception e){
+                    model.setCreateStatus(-1);
+                    modelService.saveOrUpdate(model);
+                }finally {
+                    if(newObjFile!=null){
+                        FileUtil.del(newObjFile);
+                    }
+                    if(objPathFile!=null){
+                        FileUtil.del(objPathFile);
+                    }
+                    if(mntFile!=null){
+                        FileUtil.del(mntFile.getParentFile());
+                    }
+                }
+            }
+        }.start();
     }
 
     @Override
@@ -280,4 +288,13 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
         BeanUtils.copyProperties(model,sceneVo);
         return sceneVo;
     }
+
+    @Override
+    public String uploadObjProgress(Integer modelId) {
+        String redisKey = RedisKeyUtil.modelUpload+modelId;
+        if(redisUtil.hasKey(redisKey)){
+            return redisUtil.get(redisKey);
+        }
+        return "0";
+    }
 }