Parcourir la source

move the data out of the project folder

Miaomz il y a 6 ans
Parent
commit
59e0ca254d

+ 25 - 9
src/main/java/com/moekr/moocoder/logic/service/impl/ProjectPath.java

@@ -1,26 +1,42 @@
 package com.moekr.moocoder.logic.service.impl;
 
+import com.moekr.moocoder.util.PathUtil;
+import lombok.extern.apachecommons.CommonsLog;
+
 import java.io.File;
+import java.io.IOException;
 
 /**
  * @author miaomuzhi
  * @since 2020/5/8
  */
+@CommonsLog
 public class ProjectPath {
     private static String projectDir;
     private static String walaOutputDir;
     static String pmdOutputDir;
 
-    //deprecated paths
-    static String tempZipDir;
-
     static {
-        projectDir = ProjectPath.class.getResource("/data").getFile();
-        walaOutputDir = ProjectPath.class.getResource("/wala/outputs").getFile();
-        pmdOutputDir = ProjectPath.class.getResource("/wala/pmd").getFile();
-
-        //deprecated paths
-        tempZipDir = ProjectPath.class.getResource("/wala/zips").getFile();
+        File projectDirFile = new File(PathUtil.getProjectsDir());
+        File walaOutputDirFile = new File(PathUtil.getWalaOutputDir());
+        File pmdOutputDirFile = new File(PathUtil.getPmdOutputDir());
+        if (!projectDirFile.exists() && !projectDirFile.mkdirs()) {
+            throw new IllegalStateException("Failed to create the project dir!");
+        }
+        if (!walaOutputDirFile.exists() && !walaOutputDirFile.mkdirs()) {
+            throw new IllegalStateException("Failed to create the WALA output dir!");
+        }
+        if (!pmdOutputDirFile.exists() && !pmdOutputDirFile.mkdirs()) {
+            throw new IllegalStateException("Failed to create the PMD output dir!");
+        }
+
+        try {
+            projectDir = projectDirFile.getCanonicalPath();
+            walaOutputDir = walaOutputDirFile.getCanonicalPath();
+            pmdOutputDir = pmdOutputDirFile.getCanonicalPath();
+        } catch (IOException e) {
+            log.error(e);
+        }
     }
 
     private ProjectPath() {}

+ 1 - 54
src/main/java/com/moekr/moocoder/logic/service/impl/ProjectServiceImpl.java

@@ -9,22 +9,16 @@ import com.moekr.moocoder.util.MavenUtil;
 import com.moekr.moocoder.util.ToolKit;
 import com.moekr.moocoder.util.enums.Granularity;
 import com.moekr.moocoder.web.dto.CodeDTO;
-import org.apache.tomcat.util.http.fileupload.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Base64;
 import java.util.Map;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
 
 import static com.moekr.moocoder.logic.service.impl.ProjectPath.getProjectFullPath;
-import static com.moekr.moocoder.logic.service.impl.ProjectPath.tempZipDir;
 
 /**
  * @author miaomuzhi
@@ -114,54 +108,7 @@ public class ProjectServiceImpl implements ProjectService {
     }
 
     private boolean addClassesProject(String project, MultipartFile projectFile) {
-        try {
-            String projectPath = getProjectFullPath(project);
-            File root = new File(projectPath);
-
-            if (root.exists()){
-                FileUtils.cleanDirectory(root);
-            } else{
-                root.mkdirs();
-            }
-
-            File temp = new File(tempZipDir + File.separator + projectFile.getOriginalFilename());
-            projectFile.transferTo(temp);
-
-            byte[] buffer = new byte[1024];
-            try (FileInputStream fis = new FileInputStream(temp)) {
-                try (ZipInputStream zis = new ZipInputStream(fis)) {
-                    ZipEntry zipEntry;
-                    while ((zipEntry = zis.getNextEntry()) != null) {
-                        File newFile = newFile(root, zipEntry);
-                        if (zipEntry.isDirectory() && !newFile.exists()){
-                            newFile.mkdirs();
-                        } else if (! zipEntry.isDirectory()) {
-                            try (FileOutputStream fos = new FileOutputStream(newFile)){
-                                int len;
-                                while ((len = zis.read(buffer)) > 0) {
-                                    fos.write(buffer, 0, len);
-                                }
-                            }
-                        }
-                    }
-                    zis.closeEntry();
-                }
-            }
-            return true;
-        } catch (IOException e) {
-            e.printStackTrace();
-            return false;
-        }
+        throw new UnsupportedOperationException("ProjectService.addProject is not supported!");
     }
 
-    private File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
-        File destFile = new File(destinationDir, zipEntry.getName());
-        String destDirPath = destinationDir.getCanonicalPath();
-        String destFilePath = destFile.getCanonicalPath();
-
-        if (!destFilePath.startsWith(destDirPath + File.separator)) {
-            throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
-        }
-        return destFile;
-    }
 }

+ 20 - 3
src/main/java/com/moekr/moocoder/util/PathUtil.java

@@ -9,26 +9,43 @@ import java.util.Properties;
 /**
  * @author 王川源
  * 管理资源路径
- * 迭代三改用数据库,无需初始化文件夹
  */
 @CommonsLog
 public class PathUtil {
-
     private static String mavenHome;
+    private static String projectsDir;
+    private static String walaOutputDir;
+    private static String pmdOutputDir;
 
     static {
         Properties pathProperty = new Properties();
         try {
             pathProperty.load(new InputStreamReader(PathUtil.class.getResourceAsStream("/path.properties"),"UTF-8"));
             mavenHome = pathProperty.getProperty("maven_home");
+            projectsDir = pathProperty.getProperty("projects_dir");
+            walaOutputDir = pathProperty.getProperty("wala_output_dir");
+            pmdOutputDir = pathProperty.getProperty("pmd_output_dir");
         } catch (IOException e) {
             log.error(e);
         }
     }
 
+    private PathUtil() { }
+
+
     public static String getMavenHome() {
         return mavenHome;
     }
 
-    private PathUtil() { }
+    public static String getProjectsDir() {
+        return projectsDir;
+    }
+
+    public static String getWalaOutputDir() {
+        return walaOutputDir;
+    }
+
+    public static String getPmdOutputDir() {
+        return pmdOutputDir;
+    }
 }

+ 7 - 1
src/main/resources/path.properties

@@ -1 +1,7 @@
-maven_home=/usr/local/Cellar/maven/3.5.3/libexec
+maven_home=/usr/local/Cellar/maven/3.5.3/libexec
+
+projects_dir=../data/projects
+wala_output_dir=../data/wala
+pmd_output_dir=../data/pmd
+
+

+ 18 - 0
src/test/java/com/moekr/moocoder/util/PathUtilTest.java

@@ -0,0 +1,18 @@
+package com.moekr.moocoder.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * @author miaomuzhi
+ * @since 2020/6/2
+ */
+class PathUtilTest {
+
+    @Test
+    void getProjectsDir() {
+        String projectDir = PathUtil.getProjectsDir();
+        assertNotNull(projectDir);
+    }
+}