Ver Fonte

印刷任务管理-批量下载pdf修改

wangliang há 2 anos atrás
pai
commit
b279ce9708

+ 27 - 80
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -925,25 +925,20 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 throw ExceptionResultEnum.ERROR.exception("数组id为空");
             }
 
-            //查询examDetail
-//            List<ExamDetail> examDetailList = examDetailService.listByIds(Arrays.asList(arraysParams.getIds()));
             List<ExamDetailPdfDownloadDto> examDetailList = examDetailService.findPdfDownload(Arrays.asList(arraysParams.getIds()));
             if (!CollectionUtils.isEmpty(examDetailList)) {
-                StringJoiner stringJoiner = new StringJoiner("");
                 Long time = System.currentTimeMillis();
-                if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                    stringJoiner = stringJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
-                    zipLocalRootPath = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + time;
-                } else {
-                    stringJoiner = stringJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-                    zipLocalRootPath = dictionaryConfig.fssPublicDomain().getConfig() + File.separator + time;
-                }
-                StringJoiner dirName = SystemConstant.getDirName(UploadFileEnum.FILE, true);
-                dirName.add(SystemConstant.getNanoId()).add(SystemConstant.ZIP_PREFIX);
-                zipFile = new File(stringJoiner.toString() + dirName.toString());
-                if (!zipFile.getParentFile().exists()) {
-                    zipFile.getParentFile().mkdirs();
+                boolean oss = dictionaryConfig.sysDomain().isOss();
+                StringJoiner stringJoiner = new StringJoiner("");
+                if (!oss && Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                    stringJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
                 }
+                stringJoiner = SystemConstant.getDirName(stringJoiner, UploadFileEnum.FILE, true);
+                stringJoiner.add("印刷任务管理_批量下载PDF_" + time).add(SystemConstant.ZIP_PREFIX);
+
+                String zipDirName = stringJoiner.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
+                zipFile = SystemConstant.getFileTempDirVar(SystemConstant.ZIP_PREFIX);
+                zipLocalRootPath = zipFile.getParent() + File.separator + time;
 
                 sourceFileList = new ArrayList<>(examDetailList.size() * 5);
                 for (ExamDetailPdfDownloadDto e : examDetailList) {
@@ -996,19 +991,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                     }
                 }
                 if (!CollectionUtils.isEmpty(sourceFileList)) {
-                    Zip4jUtil.zipEncryptFile(zipFile.getPath(), zipLocalRootPath, SystemConstant.ZIP_ENCRYPT_PWD);
-                    String ossDirName = dirName.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
-                    JSONObject jsonObject = new JSONObject();
-                    boolean oss = dictionaryConfig.sysDomain().isOss();
-                    if (oss) {//上传至oss
-                        fileStoreUtil.ossUpload(ossDirName, zipFile, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(zipFile)))), fileStoreUtil.getUploadEnumByPath(ossDirName).getFssType());
-                        jsonObject.put(SystemConstant.PATH, ossDirName);
-                        jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
-                    } else {
-                        jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-                        jsonObject.put(SystemConstant.PATH, stringJoiner + ossDirName);
-                    }
-                    jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+                    JSONObject jsonObject = SystemConstant.createZip(zipFile, zipLocalRootPath, zipDirName);
                     tbTask.setResultFilePath(jsonObject.toJSONString());
                 }
             }
@@ -1021,12 +1004,15 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 ResultUtil.error(e.getMessage());
             }
         } finally {
-            if (Objects.nonNull(zipLocalRootPath)) {
-                ConvertUtil.delFolder(zipLocalRootPath);
-            }
-            if (Objects.nonNull(dictionaryConfig.sysDomain()) && dictionaryConfig.sysDomain().isOss() && Objects.nonNull(zipFile)) {
+            if (Objects.nonNull(zipFile)) {
                 zipFile.delete();
             }
+            ConvertUtil.delFolder(zipLocalRootPath);
+            if (!CollectionUtils.isEmpty(sourceFileList)) {
+                for (File file : sourceFileList) {
+                    file.delete();
+                }
+            }
         }
         return map;
     }
@@ -1040,24 +1026,12 @@ public class TaskLogicServiceImpl implements TaskLogicService {
      * @throws Exception
      */
     private File getJsonFile(JSONObject jsonObject, String pathName) throws Exception {
-        File file = null;
+        File file = SystemConstant.getFileTempDirVar(pathName, null);
         String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
-        StringJoiner stringJoinerPdf = new StringJoiner("");
-        if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-            stringJoinerPdf = stringJoinerPdf.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
-        } else {
-            stringJoinerPdf = stringJoinerPdf.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-        }
-        stringJoinerPdf = stringJoinerPdf.add(pathName);
         if (Objects.nonNull(attachmentType) && !Objects.equals(attachmentType, SystemConstant.LOCAL)) {
-            file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PDF_PATH), stringJoinerPdf.toString(), UploadFileEnum.PDF.getFssType());
+            file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PDF_PATH), file, UploadFileEnum.PDF.getFssType());
         } else {
-            file = new File(stringJoinerPdf.toString());
-            if (!file.exists()) {
-                file.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
-                file.createNewFile();
-            }
-            IOUtils.copy(new FileInputStream((String) jsonObject.get(SystemConstant.PDF_PATH)), new FileOutputStream(file));
+            FileCopyUtils.copy(new File((String) jsonObject.get(SystemConstant.PDF_PATH)), file);
         }
         return file;
     }
@@ -1065,7 +1039,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     /**
      * 获取附件文件
      *
-     * @param pathName
+     * @param attachment
      * @param pathName
      * @return
      * @throws Exception
@@ -1073,25 +1047,14 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     private File getAttachmentFile(BasicAttachment attachment, String pathName) throws Exception {
         File file = null;
         if (Objects.nonNull(attachment) && Objects.nonNull(attachment.getPath())) {
+            file = SystemConstant.getFileTempDirVar(pathName, null);
             JSONObject jsonObject = JSONObject.parseObject(attachment.getPath());
             String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
             UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
-            StringJoiner stringJoinerPdf = new StringJoiner("");
-            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                stringJoinerPdf = stringJoinerPdf.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
-            } else {
-                stringJoinerPdf = stringJoinerPdf.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-            }
-            stringJoinerPdf = stringJoinerPdf.add(pathName);
             if (Objects.nonNull(attachmentType) && !Objects.equals(attachmentType, SystemConstant.LOCAL)) {
-                file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), stringJoinerPdf.toString(), uploadFileEnum.getFssType());
+                file = fileStoreUtil.ossDownload((String) jsonObject.get(SystemConstant.PATH), file, uploadFileEnum.getFssType());
             } else {
-                file = new File(stringJoinerPdf.toString());
-                if (!file.exists()) {
-                    file.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
-                    file.createNewFile();
-                }
-                IOUtils.copy(new FileInputStream((String) jsonObject.get(SystemConstant.PATH)), new FileOutputStream(file));
+                FileCopyUtils.copy(new File((String) jsonObject.get(SystemConstant.PATH)), file);
             }
         }
         return file;
@@ -1946,7 +1909,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             stringJoiner = SystemConstant.getDirName(stringJoiner, UploadFileEnum.FILE, true);
             stringJoiner.add("下载管理_批量下载_" + System.currentTimeMillis()).add(SystemConstant.ZIP_PREFIX);
 
-            String dirNameTmp = stringJoiner.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
+            String zipDirName = stringJoiner.toString().replaceAll("\\\\", SystemConstant.ORG_SPLIT);
             zipFile = SystemConstant.getFileTempDirVar(SystemConstant.getNanoId(), SystemConstant.ZIP_PREFIX);
             zipLocalRootPath = zipFile.getParent() + File.separator + System.currentTimeMillis();
 
@@ -2163,23 +2126,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 }
                 count = count + courseCount;
             }
-            Zip4jUtil.zipEncryptFile(zipFile.getPath(), zipLocalRootPath, SystemConstant.ZIP_ENCRYPT_PWD);
-
-            JSONObject jsonObject = new JSONObject();
-            jsonObject.put(SystemConstant.PATH, dirNameTmp);
-            String zipFileMd5 = DigestUtils.md5Hex(new FileInputStream(zipFile));
-            if (oss) {//上传至oss
-                fileStoreUtil.ossUpload(dirNameTmp, zipFile, zipFileMd5, fileStoreUtil.getUploadEnumByPath(dirNameTmp).getFssType());
-                jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
-            } else {
-                if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                    fileStoreUtil.localUpload(dirNameTmp, new FileInputStream(zipFile), zipFileMd5, LocalCatalogEnum.LOCAL_PDF);
-                } else {
-                    fileStoreUtil.ossUpload(dirNameTmp, zipFile, zipFileMd5, fileStoreUtil.getUploadEnumByPath(dirNameTmp).getFssType());
-                }
-                jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-            }
-            jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+            JSONObject jsonObject = SystemConstant.createZip(zipFile, zipLocalRootPath, zipDirName);
             tbTask.setResultFilePath(jsonObject.toJSONString());
             map.put("count", count);
             map.put("specialSuccessMessage", specialSuccessMessage);

+ 41 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -17,6 +17,7 @@ import com.qmth.teachcloud.common.service.CommonCacheService;
 import com.qmth.teachcloud.common.util.Base64Util;
 import com.qmth.teachcloud.common.util.FileStoreUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
+import com.qmth.teachcloud.common.util.Zip4jUtil;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -816,7 +817,13 @@ public class SystemConstant {
      * @return
      */
     public static File getFileTempDirVar(String fileName, String suffix) {
-        File file = new File(System.getProperty(SystemConstant.TMP_DIR), fileName + suffix);
+        String fileNameNew = null;
+        if (!StringUtils.isBlank(suffix)) {
+            fileNameNew = fileName + suffix;
+        } else {
+            fileNameNew = fileName;
+        }
+        File file = new File(System.getProperty(SystemConstant.TMP_DIR), fileNameNew);
         log.info("getFileTempDirVar_absolutePath:{}", file.getAbsolutePath());
         return file;
     }
@@ -976,4 +983,37 @@ public class SystemConstant {
             }
         }
     }
+
+    /**
+     * 创建加密的zip
+     *
+     * @param zipFile
+     * @param zipLocalRootPath
+     * @param zipDirName
+     * @return
+     * @throws Exception
+     */
+    public static JSONObject createZip(File zipFile, String zipLocalRootPath, String zipDirName) throws Exception {
+        FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        Zip4jUtil.zipEncryptFile(zipFile.getPath(), zipLocalRootPath, SystemConstant.ZIP_ENCRYPT_PWD);
+
+        boolean oss = dictionaryConfig.sysDomain().isOss();
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put(SystemConstant.PATH, zipDirName);
+        String zipFileMd5 = DigestUtils.md5Hex(new FileInputStream(zipFile));
+        if (oss) {//上传至oss
+            fileStoreUtil.ossUpload(zipDirName, zipFile, zipFileMd5, fileStoreUtil.getUploadEnumByPath(zipDirName).getFssType());
+            jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
+        } else {
+            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                fileStoreUtil.localUpload(zipDirName, new FileInputStream(zipFile), zipFileMd5, LocalCatalogEnum.LOCAL_FILE);
+            } else {
+                fileStoreUtil.ossUpload(zipDirName, zipFile, zipFileMd5, fileStoreUtil.getUploadEnumByPath(zipDirName).getFssType());
+            }
+            jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
+        }
+        jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
+        return jsonObject;
+    }
 }