deason 6 ani în urmă
părinte
comite
6bc0a9160a

+ 1 - 1
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/Constants.java

@@ -49,7 +49,7 @@ public interface Constants {
     String SUFFIX_EXCEL = ".xlsx";
 
     static String rootFileDir() {
-        return Constants.class.getClassLoader().getResource("").getPath() + "/files";
+        return Constants.class.getClassLoader().getResource("").getPath() + "files";
     }
 
 }

+ 6 - 6
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/upyun/UpYunClient.java

@@ -9,7 +9,6 @@ package cn.com.qmth.examcloud.core.print.common.upyun;
 
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.core.print.common.Constants;
-import cn.com.qmth.examcloud.core.print.common.Result;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import main.java.com.UpYun;
@@ -36,8 +35,9 @@ public class UpYunClient {
     public String upload(File file) {
         Check.isEmpty(file, "上传的文件不能为空!");
         if (!file.exists() || !file.isFile()) {
-            throw new StatusException(PRT_CODE_400, "上传的文件不正确!");
+            throw new StatusException(PRT_CODE_400, "上传的文件不存在!");
         }
+
         try {
             final String newFileName = FileUtils.newFileName(file.getName());
             final String newFilePath = upYunProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
@@ -51,21 +51,21 @@ public class UpYunClient {
         }
     }
 
-    public Result upload(byte[] bytes, String originalFilename) {
+    public String upload(byte[] bytes, String originalFilename) {
         Check.isEmpty(bytes, "上传的文件不能为空!");
         Check.isBlank(originalFilename, "上传的文件名不正确!");
+
         try {
             final String newFileName = FileUtils.newFileName(originalFilename);
             final String newFilePath = upYunProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
             this.getInstance().writeFile(newFilePath, bytes, true);
 
             //成功后,返回文件访问地址
-            final String url = upYunProperty.getFileUrl() + newFilePath;
-            return Result.success(url);
+            return upYunProperty.getFileUrl() + newFilePath;
         } catch (Exception e) {
             log.error(e.getMessage(), e);
+            return null;
         }
-        return Result.error();
     }
 
     public File download(String filePath) {

+ 13 - 14
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/utils/ElectronUtils.java

@@ -31,18 +31,25 @@ public class ElectronUtils {
             return false;
         }
 
-        String cmd = String.format("electron-pdf %s %s -p A3 -l true -e view-ready", url, path);
+        final String script = "electron-pdf %s %s -p A3 -l true -e view-ready";
         if (isWindows()) {
-            cmd = WIN_PREFIX + cmd;
-        }
+            //Windows带盘符路径,去掉首个"/"字符
+            if (path.indexOf(":") > 0 && path.startsWith("/")) {
+                path = path.replaceFirst("/", "");
+            }
 
-        return executeCommand(cmd);
+            String cmd = WIN_PREFIX + String.format(script, url, path);
+            return executeCommand(cmd, "GBK");
+        } else {
+            String cmd = String.format(script, url, path);
+            return executeCommand(cmd, "UTF-8");
+        }
     }
 
     /**
      * 执行命令
      */
-    private static boolean executeCommand(String cmd) {
+    private static boolean executeCommand(String cmd, String charsetName) {
         Process process;
 
         try {
@@ -52,11 +59,6 @@ public class ElectronUtils {
             return false;
         }
 
-        String charsetName = "UTF-8";
-        if (isWindows()) {
-            charsetName = "GBK";
-        }
-
         try (BufferedReader out = new BufferedReader(new InputStreamReader(process.getInputStream(), charsetName));
              BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream(), charsetName));) {
 
@@ -89,10 +91,7 @@ public class ElectronUtils {
         return false;
     }
 
-    /**
-     * 判断是否为Windows系统
-     */
-    private static boolean isWindows() {
+    public static boolean isWindows() {
         String os = System.getProperty("os.name").toLowerCase();
         if (os.contains("windows") || os.startsWith("win")) {
             return true;

+ 8 - 1
examcloud-core-print-provider/src/main/java/cn/com/qmth/examcloud/core/print/api/controller/CommonController.java

@@ -14,6 +14,7 @@ import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -48,9 +49,15 @@ public class CommonController extends ControllerSupport {
         } catch (Exception e) {
             log.warn(e.getMessage());
         }
+
         Check.isNull(file, "上传的文件不能为空!");
         Check.isTrue(file.isEmpty(), "上传的文件不能为空!");
-        return upYunClient.upload(file.getBytes(), file.getOriginalFilename());
+
+        String fileUrl = upYunClient.upload(file.getBytes(), file.getOriginalFilename());
+        if (StringUtils.isNotEmpty(fileUrl)) {
+            return Result.success(fileUrl);
+        }
+        return Result.error();
     }
 
     @GetMapping("/download")

+ 13 - 8
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CoursePaperServiceImpl.java

@@ -13,6 +13,7 @@ import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
 import cn.com.qmth.examcloud.core.print.common.upyun.UpYunClient;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
+import cn.com.qmth.examcloud.core.print.common.utils.ElectronUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.ExcelUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import cn.com.qmth.examcloud.core.print.entity.CoursePaper;
@@ -97,23 +98,24 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         Check.isEmpty(coursePaper.getCourseName(), "课程名称不能为空!");
         Check.isEmpty(coursePaper.getPaperId(), "试卷ID不能为空!");
         Check.isEmpty(coursePaper.getPaperName(), "试卷名称不能为空!");
-//        Check.isBlank(coursePaper.getPaperHtmlUrl(), "试卷页面地址不能为空!");
-//        Check.isBlank(coursePaper.getAnswerHtmlUrl(), "答案页面地址不能为空!");
-//        Check.isNull(coursePaper.getPaperP(), "试卷页数不能为空!");
 
-        //试卷地址和答案地址
+        //试卷页面地址和答案页面地址
         String paperHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/pdf/").concat(coursePaper.getPaperId());
         String answerHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/answer/pdf/").concat(coursePaper.getPaperId());
 
         //转换PDF文件
-        String rootDir = Constants.rootFileDir();
+        final String rootDir = Constants.rootFileDir();
+        final String paperPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
+        final String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
         FileUtils.makeDirs(rootDir);
-        String paperPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
-        String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
+        ElectronUtils.toPdf(paperHtmlUrl, paperPdfPath);
+        ElectronUtils.toPdf(answerHtmlUrl, answerPdfPath);
+
+        //上传PDF至又拍云
         String paperPdfUrl = upYunClient.upload(new File(paperPdfPath));
         String answerPdfUrl = upYunClient.upload(new File(answerPdfPath));
 
-        //获取PDF页数
+        //获取试卷PDF页数
         int paperP = 0;
         try (FileInputStream fis = new FileInputStream(paperPdfPath);) {
             PdfReader pdfReader = new PdfReader(fis);
@@ -121,6 +123,9 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         } catch (IOException e) {
             log.error(e.getMessage());
         }
+        if (paperP == 0) {
+            throw new StatusException(PRT_CODE_400, "试卷页数不能为空!");
+        }
 
         CoursePaper oldCoursePaper = coursePaperRepository.findByExamIdAndPaperId(coursePaper.getExamId(), coursePaper.getPaperId());
         if (oldCoursePaper != null) {

+ 1 - 6
examcloud-core-print-starter/src/test/java/cn/com/qmth/examcloud/core/print/test/CoursePaperServiceTest.java

@@ -49,13 +49,8 @@ public class CoursePaperServiceTest extends BaseTest {
         coursePaper.setCourseId(18L);
         coursePaper.setCourseCode("123456");
         coursePaper.setCourseName("测试课程");
-        coursePaper.setPaperId("xyz");
+        coursePaper.setPaperId("e965c24e-68a7-4f4d-ac1f-00ca0c904b10");
         coursePaper.setPaperName("测试试卷");
-        coursePaper.setPaperP(5);
-        coursePaper.setPaperHtmlUrl("https://ecs-test-static.qmth.com.cn/ecs-print/test/demo.pdf");
-        coursePaper.setPaperPdfUrl("https://ecs-test-static.qmth.com.cn/ecs-print/test/demo.pdf");
-        coursePaper.setAnswerHtmlUrl("https://ecs-test-static.qmth.com.cn/ecs-print/test/demo.pdf");
-        coursePaper.setAnswerPdfUrl("https://ecs-test-static.qmth.com.cn/ecs-print/test/demo.pdf");
         coursePaperService.syncCoursePaper(coursePaper);
     }