yyy2015 7 lat temu
rodzic
commit
f12c4d3dd5

+ 2 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/common/constant/Constants.java

@@ -42,6 +42,8 @@ public class Constants {
 
     public static final String SAVE_PATH = "/var/www/download/";
 
+//    public static final String SAVE_PATH = "C:\\Users\\18260\\Desktop\\testImportZip";
+
     public static final String WORKSPACE_NAME = "workspace";
 
     public static final String META_NAME = "meta.json";

+ 1 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/TargetServiceImpl.java

@@ -173,6 +173,7 @@ public class TargetServiceImpl extends BaseService implements TargetService {
                 propertyName);
         if(targetExtends == null){
             targetExtends = new TargetExtends();
+            targetExtends.setTargetId(targetId);
             targetExtends.setPropertyName(propertyName);
         }
         targetExtends.setPropertyValue(propertyValue);

+ 2 - 2
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/ctrl/TargetController.java

@@ -100,8 +100,8 @@ public class TargetController extends BaseSearchController {
 
     @RequiresRoles("manager")
     @RequestMapping(value = UrlConstants.API + "target/downloadZip", method = RequestMethod.POST)
-    public String downloadTargetZip(@RequestParam("targetId")long targetId) throws IOException {
-        return null;
+    public String downloadTargetZip(@RequestParam("targetId")long targetId) throws Exception {
+        return targetLogic.generateTargetZip(targetId);
     }
 
     @RequiresRoles("manager")

+ 25 - 12
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/impl/TargetLogicImpl.java

@@ -295,16 +295,18 @@ public class TargetLogicImpl extends BaseLogic implements TargetLogic {
         }
 
         //解压zip中的workspace到目标文件夹下
+        File importZipFile = null;
         if(zipPath != null && zipPath.endsWith(".zip")){
             zipPath = Paths.get(System.getProperty("java.io.tmpdir"),Constants.TARGET_PATH,zipPath)
                     .toString();
-            File zipFile = new File(zipPath);
-            if(zipFile.exists()){
+            importZipFile = new File(zipPath);
+            if(importZipFile.exists()){
                 FileUtils.unZipMultiFile(zipPath,targetPath);
-                org.apache.commons.io.FileUtils.copyDirectory(
-                        Paths.get(targetPath, Constants.WORKSPACE_NAME).toFile()
-                        ,new File(targetContentPath));
-                FileUtils.deleteDir(new File(zipPath));
+                File workspaceDir = Paths.get(targetPath, Constants.WORKSPACE_NAME).toFile();
+                if(workspaceDir.exists() && workspaceDir.isDirectory()){
+                    org.apache.commons.io.FileUtils.copyDirectory(
+                            workspaceDir,new File(targetContentPath));
+                }
             }else{
                 log.warn("zip "+zipPath+" not exist!");
             }
@@ -348,14 +350,17 @@ public class TargetLogicImpl extends BaseLogic implements TargetLogic {
             }
             target.setUrl(url);
             targetService.updateTarget(target);
-            Map<String, String> propertyMap = new HashMap<>();
-            propertyMap.put("lang",lang);
-            targetService.saveTargetProperties(propertyMap,targetId);
+            targetService.setTargetProperty(targetId,Constants.LANG_PROPERTY,lang);
             TargetExtends importZipExtends = targetService.getTargetProperty(targetId,Constants
                     .TARGET_IMPORT_ZIP_PATH);
             if(importZipExtends != null){
                 targetService.removeTargetProperty(importZipExtends.getId());
             }
+
+            //target新建成功,import zip可被删除
+            if(importZipFile != null){
+                FileUtils.deleteDir(importZipFile);
+            }
             return true;
         }
         return false;
@@ -391,9 +396,9 @@ public class TargetLogicImpl extends BaseLogic implements TargetLogic {
         FileUtils.unZipMultiFile(tmpZipPath,tmpUnzipPath);
 
         //检查zip包格式
-        File workspace = new File(Paths.get(tmpUnzipPath,Constants.WORKSPACE_NAME).toString());
+//        File workspace = new File(Paths.get(tmpUnzipPath,Constants.WORKSPACE_NAME).toString());
         File meta = new File(Paths.get(tmpUnzipPath,Constants.META_NAME).toString());
-        if(workspace.exists() && workspace.isDirectory() && meta.exists() && meta.isFile()){
+        if(meta.exists() && meta.isFile()){
             //zip包格式正确
             String fileContent = FileUtils.readToString(Paths.get(tmpUnzipPath,Constants.META_NAME)
                     .toString());
@@ -407,7 +412,7 @@ public class TargetLogicImpl extends BaseLogic implements TargetLogic {
             log.info("delete tmp unzip dir: "+FileUtils.deleteDir(new File(tmpUnzipPath)));
             log.info("delete error format zip: "+FileUtils.deleteDir(new File(tmpZipPath)));
             throw new HttpBadRequestException("Zip format wrong! Please ensure you have 'meta"
-                    + ".json' FILE and 'workspace' DIRECTORY under the zip DIRECTLY!");
+                    + ".json' FILE under the zip DIRECTLY!");
         }
 
     }
@@ -455,13 +460,21 @@ public class TargetLogicImpl extends BaseLogic implements TargetLogic {
                 .LANG_PROPERTY);
         targetDataVo.setLang(targetExtends==null?null:targetExtends.getPropertyValue());
 
+        //构造meta.json
         String metaJson = new Gson().toJson(targetDataVo);
         log.info("Meta json is: \n"+metaJson);
         FileUtils.writingStringToFile(metaJson,Paths.get(downloadPath,Constants.META_NAME)
                 .toString());
 
+        //删除不需要的文件
+        FileUtils.deleteDir(Paths.get(workspacePath,Constants.MAIN_NAME).toFile());
+        FileUtils.deleteDir(Paths.get(workspacePath,Constants.MOOCTEST_PATH,Constants
+                .ANSWER_NAME).toFile());
+        FileUtils.deleteDir(Paths.get(workspacePath,Constants.MOOCTEST_PATH,Constants.TEST_CASE_NAME).toFile());
         String finalZipPath = Paths.get(Constants.SAVE_PATH,target.getName()+"_"+System.currentTimeMillis
                 ()+".zip").toString();
+
+        //压缩文件
         FileUtils.zipMultiFile(downloadPath,finalZipPath,false);
         targetService.setTargetProperty(targetId,Constants.TARGET_IMPORT_ZIP_PATH,finalZipPath);
         FileUtils.deleteDir(new File(targetPath));