Przeglądaj źródła

3.4.4 update-20250124修改bug

xiaofei 4 miesięcy temu
rodzic
commit
371f9d1ee4

+ 20 - 17
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamCardServiceImpl.java

@@ -436,36 +436,39 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
     public void downloadCard(HttpServletResponse response, String id) {
         String rootPath = null;
         try {
-            // 题卡详细信息
             ExamCard examCard = this.getById(id);
+            // 去掉题卡名称中的空格,否则创建目录报错
+            String title = StringUtils.deleteWhitespace(examCard.getTitle());
             // 本地保存目录
-            File tempFile = SystemConstant.getFileTempParentDirVar(SystemConstant.TEMP_PREFIX);
-            rootPath = tempFile.getPath() + File.separator + SystemConstant.getNanoId();
+            rootPath = FileUtil.getFileTempDirPath(true);
             // 下载文件保存路径(以题卡名称命名)
-            String downloadFilePath = rootPath + File.separator + examCard.getTitle();
+            String downloadFilePath = rootPath + File.separator + title;
             File downloadPathFile = new File(downloadFilePath);
             if (!downloadPathFile.exists()) {
                 downloadPathFile.mkdirs();
             }
 
-            // 通用模板
+            // HTML文件
             String htmlContent = createPdfUtil.replaceBlankHtmlContent(examCard.getHtmlContent(), examCard.getCourseId());
-            // html文件
-            File htmlFile = new File(downloadFilePath, examCard.getTitle() + SystemConstant.HTML_PREFIX);
+            File htmlFile = new File(downloadFilePath, title + SystemConstant.HTML_PREFIX);
             if (!htmlFile.exists()) {
                 htmlFile.createNewFile();
             }
-            // 生成html文件
             FileUtil.writeContent(htmlFile, htmlContent);
 
-            // pdf文件
-            File pdfFile = new File(downloadFilePath, examCard.getTitle() + SystemConstant.PDF_PREFIX);
-            if (!pdfFile.exists()) {
-                pdfFile.createNewFile();
+            // PDF文件
+            if (StringUtils.isNotBlank(examCard.getBlankCardPath())) {
+                String pdfFileName = downloadFilePath + File.separator + title + SystemConstant.PDF_PREFIX;
+                fileUploadService.downloadFile(examCard.getBlankCardPath(), pdfFileName);
+            } else {
+                File pdfFile = new File(downloadFilePath, title + SystemConstant.PDF_PREFIX);
+                if (!pdfFile.exists()) {
+                    pdfFile.createNewFile();
+                }
+                HtmlToPdfUtil.convert(htmlFile.getPath(), pdfFile.getPath(), "8K".equals(examCard.getPageSize()) ? PageSizeEnum.K8 : PageSizeEnum.valueOf(examCard.getPageSize()));
             }
-            HtmlToPdfUtil.convert(htmlFile.getPath(), pdfFile.getPath(), "8K".equals(examCard.getPageSize()) ? PageSizeEnum.K8 : PageSizeEnum.valueOf(examCard.getPageSize()));
 
-            // jpg文件
+            // JPG文件
             String jpgAttachmentIds = examCard.getJpgAttachment();
             if (StringUtils.isNotBlank(jpgAttachmentIds)) {
                 List<ConvertJpgStorage> jsonObjectList = JSONObject.parseArray(jpgAttachmentIds, ConvertJpgStorage.class);
@@ -475,7 +478,7 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
                     if (jpgAttachmentId != null) {
                         BasicAttachment attachment = basicAttachmentService.getById(jpgAttachmentId);
                         if (attachment != null) {
-                            String fileName = downloadFilePath + File.separator + examCard.getTitle() + SystemConstant.HYPHEN + index + attachment.getType();
+                            String fileName = downloadFilePath + File.separator + title + SystemConstant.HYPHEN + index + attachment.getType();
                             fileUploadService.downloadFile(attachment.getPath(), fileName);
                         }
                     }
@@ -485,11 +488,11 @@ public class ExamCardServiceImpl extends ServiceImpl<ExamCardMapper, ExamCard> i
             // json文件
             String content = examCard.getContent();
             if (StringUtils.isNotBlank(content)) {
-                String jsonPath = downloadFilePath + File.separator + examCard.getTitle() + SystemConstant.JSON_PREFIX;
+                String jsonPath = downloadFilePath + File.separator + title + SystemConstant.JSON_PREFIX;
                 FileUtil.writeContent(new File(jsonPath), content);
             }
 
-            String zipFileName = examCard.getTitle() + SystemConstant.ZIP_PREFIX;
+            String zipFileName = title + SystemConstant.ZIP_PREFIX;
             SysUser sysUser = sysUserService.getById(ServletUtil.getRequestUserId());
             FileUtil.downloadEncryptZip(response, downloadPathFile, zipFileName, new String(Base64Util.decode(sysUser.getPassword())));
         } catch (Exception e) {

+ 16 - 15
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamTaskServiceImpl.java

@@ -1005,11 +1005,10 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
         String rootPath = null;
         try {
             // 本地保存目录
-            File tempFile = SystemConstant.getFileTempParentDirVar(SystemConstant.TEMP_PREFIX);
-            rootPath = tempFile.getPath() + File.separator + SystemConstant.getNanoId();
+            rootPath = FileUtil.getFileTempDirPath(true);
 
             // 下载文件保存目录(用uuid命名)
-            String downloadFilePath = rootPath + File.separator + examTaskDetailPdfDownloadDto.getCourseNameCode().replaceAll(" ", "") + "试卷包";
+            String downloadFilePath = rootPath + File.separator + StringUtils.deleteWhitespace(examTaskDetailPdfDownloadDto.getCourseNameCode()) + "试卷包";
             File downloadPathFile = new File(downloadFilePath);
             if (!downloadPathFile.exists()) {
                 downloadPathFile.mkdirs();
@@ -1043,25 +1042,27 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
                         ExamCard examCard = examCardService.getById(cardId);
                         Optional.ofNullable(examCard).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未查询到题卡"));
 
-                        String cardHtmlPath = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.HTML_PREFIX;
-                        String cardPdfPath = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.PDF_PREFIX;
-
+                        // HTML文件
                         String htmlContent = createPdfUtil.replaceBlankHtmlContent(examCard.getHtmlContent(), examCard.getCourseId());
-                        // html
-                        File htmlFile = new File(cardHtmlPath);
+                        String htmlFilePathName = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.HTML_PREFIX;
+                        File htmlFile = new File(htmlFilePathName);
                         if (!htmlFile.exists()) {
                             htmlFile.getParentFile().mkdirs();
                             htmlFile.createNewFile();
                         }
-                        // 生成html文件
                         FileUtil.writeContent(htmlFile, htmlContent);
-                        // 转pdf文件
-                        File pdfFile = new File(cardPdfPath);
-                        if (!pdfFile.exists()) {
-                            pdfFile.getParentFile().mkdirs();
-                            pdfFile.createNewFile();
+
+                        // PDF文件
+                        String pdfFilePathName = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.PDF_PREFIX;
+                        if (StringUtils.isNotBlank(examCard.getBlankCardPath())) {
+                            fileUploadService.downloadFile(examCard.getBlankCardPath(), pdfFilePathName);
+                        } else {
+                            File pdfFile = new File(pdfFilePathName);
+                            if (!pdfFile.exists()) {
+                                pdfFile.createNewFile();
+                            }
+                            HtmlToPdfUtil.convert(htmlFilePathName, pdfFilePathName, "8K".equals(examCard.getPageSize()) ? PageSizeEnum.K8 : PageSizeEnum.valueOf(examCard.getPageSize()));
                         }
-                        HtmlToPdfUtil.convert(cardHtmlPath, cardPdfPath, "8K".equals(examCard.getPageSize()) ? PageSizeEnum.K8 : PageSizeEnum.valueOf(examCard.getPageSize()));
                     }
                 }
             }

+ 18 - 15
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/DownloadLogicServiceImpl.java

@@ -278,9 +278,9 @@ public class DownloadLogicServiceImpl implements DownloadLogicService {
                 dirPath = dirPath.add(zipLocalRootPath).add(File.separator)
                         .add(examTaskDetailPdfDownloadDto.getSemesterName()).add(File.separator)
                         .add(examTaskDetailPdfDownloadDto.getExamName()).add(File.separator)
-                        .add(examTaskDetailPdfDownloadDto.getCourseNameCode().replaceAll(" ", "")).add(File.separator)
+                        .add(StringUtils.deleteWhitespace(examTaskDetailPdfDownloadDto.getCourseNameCode())).add(File.separator)
                         .add(examTaskDetailPdfDownloadDto.getPaperNumber()).add(File.separator)
-                        .add(examTaskDetailPdfDownloadDto.getCourseNameCode().replaceAll(" ", "")).add(SystemConstant.HYPHEN)
+                        .add(StringUtils.deleteWhitespace(examTaskDetailPdfDownloadDto.getCourseNameCode())).add(SystemConstant.HYPHEN)
                         .add(examTaskDetailPdfDownloadDto.getPaperNumber()).add(SystemConstant.HYPHEN);
                 // 试卷
                 List<ExamTaskDetail> examTaskDetailList = examTaskDetailService.listByExamTaskId(examTask.getId());
@@ -312,27 +312,30 @@ public class DownloadLogicServiceImpl implements DownloadLogicService {
                                 ExamCard examCard = examCardService.getById(cardId);
                                 Optional.ofNullable(examCard).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("课程[" + examTask.getCourseName() + "(" + examTask.getCourseCode() + ")]、试卷编号[" + examTask.getPaperNumber() + "]命题任务未查询到卷型[" + paperInfoVo.getName() + "]题卡"));
 
-                                String cardHtmlPath = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.HTML_PREFIX;
-                                String cardPdfPath = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.PDF_PREFIX;
+                                String htmlFilePathName = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.HTML_PREFIX;
                                 // 通用题卡
                                 String htmlContent = createPdfUtil.replaceBlankHtmlContent(examCard.getHtmlContent(), examCard.getCourseId());
-                                // html
-                                File localFile = new File(cardHtmlPath);
+                                // html文件
+                                File localFile = new File(htmlFilePathName);
                                 if (!localFile.exists()) {
                                     localFile.getParentFile().mkdirs();
                                     localFile.createNewFile();
                                 }
-                                // 生成html文件
                                 FileCopyUtils.copy(htmlContent.getBytes(StandardCharsets.UTF_8), localFile);
                                 sourceFileList.add(localFile);
-                                // 转pdf文件
-                                File file = new File(cardPdfPath);
-                                if (!file.exists()) {
-                                    file.getParentFile().mkdirs();
-                                    file.createNewFile();
+
+                                // PDF文件
+                                String pdfFilePathName = dirPath + "卷" + examTaskDetail.getSerialNumber() + SystemConstant.HYPHEN + "题卡" + SystemConstant.HYPHEN + name + SystemConstant.PDF_PREFIX;
+                                if (StringUtils.isNotBlank(examCard.getBlankCardPath())) {
+                                    fileUploadService.downloadFile(examCard.getBlankCardPath(), pdfFilePathName);
+                                } else {
+                                    File pdfFile = new File(pdfFilePathName);
+                                    if (!pdfFile.exists()) {
+                                        pdfFile.createNewFile();
+                                    }
+                                    HtmlToPdfUtil.convert(htmlFilePathName, pdfFilePathName, "8K".equals(examCard.getPageSize()) ? PageSizeEnum.K8 : PageSizeEnum.valueOf(examCard.getPageSize()));
+                                    sourceFileList.add(pdfFile);
                                 }
-                                HtmlToPdfUtil.convert(cardHtmlPath, cardPdfPath, "8K".equals(examCard.getPageSize()) ? PageSizeEnum.K8 : PageSizeEnum.valueOf(examCard.getPageSize()));
-                                sourceFileList.add(file);
                             }
                         }
                     }
@@ -354,7 +357,7 @@ public class DownloadLogicServiceImpl implements DownloadLogicService {
                 zipFile.delete();
             }
             if (Objects.nonNull(zipLocalRootPath)) {
-                ConvertUtil.delFolder(zipLocalRootPath);
+                FileUtil.deleteDirectory(zipLocalRootPath);
             }
             if (!CollectionUtils.isEmpty(sourceFileList)) {
                 for (File file : sourceFileList) {

+ 10 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/HtmlToJpgUtil.java

@@ -9,6 +9,8 @@ import com.qmth.teachcloud.common.enums.UploadFileEnum;
 import com.qmth.teachcloud.common.service.BasicAttachmentService;
 import com.qmth.teachcloud.common.service.FileUploadService;
 import com.qmth.teachcloud.common.util.ConvertUtil;
+import com.qmth.teachcloud.common.util.FileUtil;
+import org.apache.commons.collections4.CollectionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
@@ -49,9 +51,10 @@ public class HtmlToJpgUtil {
         List<ConvertJpgStorage> convertJpgStorages = new ArrayList<>();
         List<BasicAttachment> basicAttachmentList = new ArrayList<>();
         // html
+        List<File> jpgFileList = null;
         try {
             // pdf文件转jpg
-            List<File> jpgFileList = ConvertUtil.pdfToImageFile(file);
+            jpgFileList = ConvertUtil.pdfToImageFile(file);
             for (int i = 0; i < jpgFileList.size(); i++) {
                 int index = i + 1;
                 String fileName = cardId + "-" + index + SystemConstant.JPG_PREFIX;
@@ -64,6 +67,12 @@ public class HtmlToJpgUtil {
             }
         } catch (Exception e) {
             basicAttachmentService.batchDeleteAttachment(basicAttachmentList);
+        } finally {
+            if (CollectionUtils.isNotEmpty(jpgFileList)) {
+                for (File jpgFile : jpgFileList) {
+                    FileUtil.deleteFile(jpgFile);
+                }
+            }
         }
         return convertJpgStorages;
     }

+ 2 - 1
distributed-print/install/mysql/init/teachcloud_db.sql

@@ -1692,7 +1692,8 @@ CREATE TABLE IF NOT EXISTS `mark_question` (
   `create_time` bigint DEFAULT NULL,
   `update_id` bigint DEFAULT NULL,
   `update_time` bigint DEFAULT NULL,
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+    INDEX `index_1`(`exam_id` ASC, `paper_number` ASC, `main_number` ASC, `sub_number` ASC) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='小题信息表';
 
 -- ----------------------------

+ 3 - 0
distributed-print/install/mysql/upgrade/3.4.4.sql

@@ -0,0 +1,3 @@
+USE teachcloud_db;
+
+ALTER TABLE `mark_question` ADD INDEX `index_1` (`exam_id` ASC, `paper_number` ASC, `main_number` ASC, `sub_number` ASC);

+ 2 - 2
distributed-print/src/main/resources/application.properties

@@ -12,7 +12,7 @@ spring.application.name=teachcloud
 #\u6570\u636E\u6E90\u914D\u7F6E
 db.host=localhost
 db.port=3306
-db.name=teachcloud-test
+db.name=teachcloud-3.4.4
 db.username=root
 db.password=12345678
 
@@ -64,7 +64,7 @@ spring.activiti.history-level=audit
 com.qmth.fss.public.config=/Users/xiaofei/qmth/temporary/zxzk/file-temp
 com.qmth.fss.public.server=/file-temp/
 com.qmth.fss.private.config=/Users/xiaofei/qmth/temporary/zxzk/pdf-temp
-com.qmth.fss.private.server=/fss/
+com.qmth.fss.private.server=/pdf-temp/
 
 #\u7CFB\u7EDF\u914D\u7F6E
 sys.config.oss=true

+ 31 - 80
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/FileUtil.java

@@ -19,6 +19,7 @@ import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Enumeration;
 import java.util.LinkedList;
@@ -144,20 +145,14 @@ public class FileUtil {
         return true;
     }
 
-    /**
-     * 保存字符串到文件中 @throws IOException @throws
-     */
-    public static void saveAsFile(String path, String content) {
-        saveAsFile(path, content, null);
-    }
 
-    public static void saveAsFile(String path, String content, String encoding) {
+    public static void saveAsFile(String path, String content, Charset encoding) {
         if (path == null || content == null) {
             return;
         }
 
         if (encoding == null) {
-            encoding = SystemConstant.CHARSET_NAME;
+            encoding = StandardCharsets.UTF_8;
         }
         BufferedWriter bw = null;
         try {
@@ -170,7 +165,7 @@ public class FileUtil {
                     }
                 }
             }
-            IOUtils.write(content.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
+            IOUtils.write(content.getBytes(encoding), new FileOutputStream(file));
         } catch (IOException e) {
             throw ExceptionResultEnum.ERROR.exception("保存文件出错" + e.getMessage());
         } finally {
@@ -409,74 +404,6 @@ public class FileUtil {
         deleteDirectory(dirFile);
     }
 
-    private static Cipher initAESCipher(String sKey, String vector, int cipherMode) throws Exception {
-        byte[] raw;
-        raw = sKey.getBytes();
-        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
-        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-        IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
-        cipher.init(cipherMode, skeySpec, iv);
-        return cipher;
-    }
-
-    /**
-     * 解密文件
-     *
-     * @param sourceFile  源文件
-     * @param decryptFile 解密后的文件
-     * @param sKey        密钥
-     * @param vc          向量
-     * @throws Exception
-     */
-    public static void decryptFile(File sourceFile, File decryptFile, String sKey, String vc) {
-        decryptOrEncryptFile(Cipher.DECRYPT_MODE, sourceFile, decryptFile, sKey, vc);
-    }
-
-    /**
-     * 加密文件
-     *
-     * @param sourceFile  源文件
-     * @param encryptFile 加密后的文件
-     * @param sKey        密钥
-     * @param vc          向量
-     * @throws Exception
-     */
-    public static void encryptFile(File sourceFile, File encryptFile, String sKey, String vc) {
-        decryptOrEncryptFile(Cipher.ENCRYPT_MODE, sourceFile, encryptFile, sKey, vc);
-    }
-
-    private static void decryptOrEncryptFile(int cipherMode, File sourceFile, File targetFile, String sKey, String vc) {
-        InputStream inputStream = null;
-        OutputStream outputStream = null;
-        try {
-            Cipher cipher = initAESCipher(sKey, vc, cipherMode);
-            inputStream = new FileInputStream(sourceFile);
-            outputStream = new FileOutputStream(targetFile);
-            CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
-            byte[] buffer = new byte[1024];
-            int r;
-            while ((r = inputStream.read(buffer)) >= 0) {
-                cipherOutputStream.write(buffer, 0, r);
-            }
-            cipherOutputStream.close();
-        } catch (Exception e) {
-            throw ExceptionResultEnum.ERROR.exception("加解密文件失败!" + e.getMessage());
-        } finally {
-            try {
-                if (inputStream != null) {
-                    inputStream.close();
-                }
-            } catch (IOException e) {
-            }
-            try {
-                if (outputStream != null) {
-                    outputStream.close();
-                }
-            } catch (IOException e) {
-            }
-        }
-    }
-
     /**
      * 文件转数组
      *
@@ -640,11 +567,35 @@ public class FileUtil {
         }
     }
 
+    /**
+     * 获取临时文件根目录
+     *
+     * @param containNanoId 是否使用NanoId创建子目录
+     * @return 文件路径
+     */
+    public static String getFileTempDirPath(Boolean containNanoId) {
+        File fileTmpDir;
+        try {
+            if (containNanoId) {
+                fileTmpDir = new File(System.getProperty(SystemConstant.TMP_DIR), SystemConstant.getNanoId());
+            } else {
+                fileTmpDir = new File(System.getProperty(SystemConstant.TMP_DIR));
+            }
+            if (!fileTmpDir.exists()) {
+                fileTmpDir.mkdirs();
+            }
+            log.info("getFileTempDirPath_path:{}", fileTmpDir.getPath());
+            return fileTmpDir.getPath();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * 生成文本文件
      *
-     * @param file    文件
-     * @param content 要生成的文件内容
+     * @param file    目标文件
+     * @param content 文件内容
      */
     public static File writeContent(File file, String content) {
         try {
@@ -667,7 +618,7 @@ public class FileUtil {
      * @param zipFileName      压缩zip文件路径+文件名
      */
     public static void downloadEncryptZip(HttpServletResponse response, File downloadPathFile, String zipFileName, String password) {
-        if(StringUtils.isBlank(password)){
+        if (StringUtils.isBlank(password)) {
             password = SystemConstant.ZIP_ENCRYPT_PWD;
         }
         // 压缩zip文件和图片保存文件夹放在同一级