Sfoglia il codice sorgente

上传模型支持ply和las

lyhzzz 3 anni fa
parent
commit
680a72bb44

+ 12 - 0
src/main/java/com/fdkankan/fusion/common/util/OBJToGLBUtil.java

@@ -22,6 +22,18 @@ public class OBJToGLBUtil {
         ShellUtil.execCmd(command);
         log.info("obj转换glb完毕:" + command);
     }
+    public static void objToGlb(String objPath, String name,String glbPath)  {
+        log.info("obj转换glb开始,{}",objPath);
+        if(!checkObj(objPath)){
+            throw new BusinessException(-1,"obj文件错误");
+        }
+        objPath +="/"+name;
+        log.info("obj转换glb开始");
+        String command = "obj2gltf -i " + objPath + " -o " + glbPath;
+        log.info("执行obj转换glb命令路径-{}", command);
+        ShellUtil.execCmd(command);
+        log.info("obj转换glb完毕:" + command);
+    }
 
     public static boolean checkObj(String objPath) {
         File file1 = new File(objPath);

+ 34 - 8
src/main/java/com/fdkankan/fusion/service/impl/ModelServiceImpl.java

@@ -4,6 +4,7 @@ import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -86,14 +87,12 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
         model.setModelType("glb");
         model.setModelSize(file.getSize());
         model.setUserName(username);
-        this.save(model);
         File newObjFile = null;
         try {
-            String objPath = String.format(OBJ_PATH , "modelId_"+model.getModelId());
-            String glbPath = String.format(GLB_PATH , "modelId_"+model.getModelId()) +"/"+modelName +"/mesh.glb";
+            String objPath = String.format(OBJ_PATH , "modelId_"+model.getModelId()) ;
+            String glbPath = String.format(GLB_PATH , "modelId_"+model.getModelId());
             String glbOssPath = String.format(FilePath.GLB_OSS_PATH, model.getModelId());
 
-
             newObjFile = new File(objPath +"/" + fileName);
             if(!newObjFile.getParentFile().exists()){
                 newObjFile.mkdirs();
@@ -105,16 +104,43 @@ public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implement
             if(fileName.toLowerCase().contains("rar")){
                 ShellUtil.unRar(newObjFile.getPath(),objPath);
             }
-
-            OBJToGLBUtil.objToGlb(objPath+"/"+modelName,glbPath);
+            File objPathFile = new File(objPath + "/"+modelName);
+            if(!objPathFile.isDirectory()){
+                throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
+            }
+            File[] files = objPathFile.listFiles();
+            for (File file1 : files) {
+                String name = file1.getName();
+                if(name.contains("obj") || name.contains("OBJ")){
+                    String glbName = "";
+                    if(glbPath.contains(".obj")){
+                        glbName = name.replace(".obj",".glb");
+                    }
+                    if(glbPath.contains(".OBJ")){
+                        glbName = name.replace(".OBJ",".glb");
+                    }
+                    OBJToGLBUtil.objToGlb(objPath+"/"+modelName,name,  glbPath+"/"+glbName);
+                    glbOssPath = glbOssPath.replace("mesh.glb",glbName);
+                }
+                if(name.contains(".ply")){
+                    glbOssPath = glbOssPath.replace("mesh.glb",name);
+                    model.setModelDateType("ply");
+                    model.setModelType("ply");
+                }
+                if(name.contains(".las")){
+                    glbOssPath = glbOssPath.replace("mesh.glb",name);
+                    model.setModelDateType("las");
+                    model.setModelType("las");
+                }
+            }
             uploadToOssUtil.upload(glbPath,glbOssPath);
             model.setModelObjUrl(objPath);
             model.setModelGlbUrl(queryPath + glbOssPath);
             model.setCreateStatus(1);  //上传成功
-            this.updateById(model);
+            this.saveOrUpdate(model);
         }catch (Exception e){
             model.setCreateStatus(-1);
-            this.updateById(model);
+            this.saveOrUpdate(model);
             throw e;
         }finally {
             if(newObjFile!=null){