浏览代码

生成pdf修改逻辑

wangliang 2 年之前
父节点
当前提交
73bc514b8b

+ 0 - 52
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicTemplateServiceImpl.java

@@ -16,7 +16,6 @@ import com.qmth.distributed.print.business.service.BasicPrintConfigService;
 import com.qmth.distributed.print.business.service.BasicTemplateService;
 import com.qmth.distributed.print.business.service.ExamPrintPlanService;
 import com.qmth.distributed.print.business.util.PdfFillUtils;
-import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicAttachment;
 import com.qmth.teachcloud.common.entity.BasicPrintConfig;
@@ -24,12 +23,9 @@ import com.qmth.teachcloud.common.entity.SysUser;
 import com.qmth.teachcloud.common.enums.ClassifyEnum;
 import com.qmth.teachcloud.common.enums.EnumResult;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
-import com.qmth.teachcloud.common.enums.UploadFileEnum;
 import com.qmth.teachcloud.common.service.BasicAttachmentService;
 import com.qmth.teachcloud.common.service.TeachcloudCommonService;
-import com.qmth.teachcloud.common.util.FileStoreUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
-import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -38,11 +34,9 @@ import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.io.File;
-import java.io.FileInputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
-import java.util.StringJoiner;
 import java.util.stream.Collectors;
 
 /**
@@ -71,12 +65,6 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
     @Resource
     ExamPrintPlanService examPrintPlanService;
 
-    @Resource
-    DictionaryConfig dictionaryConfig;
-
-    @Resource
-    FileStoreUtil fileStoreUtil;
-
     @Override
     public IPage<TemplateDto> list(Boolean enable, String type, String name, Long startTime, Long endTime, Integer pageNumber, Integer pageSize) {
         Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
@@ -213,10 +201,6 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
         }
         ClassifyEnum classifyEnum = ClassifyEnum.valueOf(classifyStr);
 
-        StringJoiner pdfStringJoiner = SystemConstant.getDirName(UploadFileEnum.PDF, true);
-        pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
-        String pdfDirName = pdfStringJoiner.toString();
-
         File file = null;
         try {
             file = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
@@ -225,7 +209,6 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
             if (ClassifyEnum.SIGN.equals(classifyEnum)) {
                 if (StringUtils.isBlank(basicTemplate.getPreviewPath())) {
                     JSONObject json = PdfFillUtils.parseSignTempData(basicTemplate.getDisplayRange(), destUrl);
-                    json = this.getPreviewPath(json, pdfDirName, file);
                     basicTemplate.setPreviewPath(JSON.toJSONString(json));
                     basicTemplateMapper.updateById(basicTemplate);
                 }
@@ -233,7 +216,6 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
             } else if (ClassifyEnum.PACKAGE.equals(classifyEnum)) {
                 if (StringUtils.isBlank(basicTemplate.getPreviewPath())) {
                     JSONObject json = PdfFillUtils.packageTempData(basicTemplate.getDisplayRange(), destUrl);
-                    json = this.getPreviewPath(json, pdfDirName, file);
                     basicTemplate.setPreviewPath(JSON.toJSONString(json));
                     basicTemplateMapper.updateById(basicTemplate);
                 }
@@ -254,40 +236,6 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
         return null;
     }
 
-    /**
-     * 获取预览路径
-     *
-     * @param json
-     * @param pdfDirName
-     * @param file
-     * @return
-     * @throws Exception
-     */
-    private JSONObject getPreviewPath(JSONObject json, String pdfDirName, File file) throws Exception {
-        boolean oss = dictionaryConfig.sysDomain().isOss();
-        if (Objects.nonNull(json) && !StringUtils.isBlank(json.getString(SystemConstant.PATH))) {
-            if (oss) {
-                fileStoreUtil.ossUpload(pdfDirName, file, DigestUtils.md5Hex(new FileInputStream(file)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
-                json.put(SystemConstant.PATH, pdfDirName);
-            } else {
-                if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                    String destUrl = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator + pdfDirName;
-                    File newFile = new File(destUrl);
-                    if (!newFile.exists()) {
-                        newFile.getParentFile().mkdirs();
-                        newFile.createNewFile();
-                    }
-                    FileUtils.copyFile(file, newFile);
-                    json.put(SystemConstant.PATH, destUrl);
-                } else {
-                    fileStoreUtil.ossUpload(pdfDirName, file, DigestUtils.md5Hex(new FileInputStream(file)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
-                    json.put(SystemConstant.PATH, pdfDirName);
-                }
-            }
-        }
-        return json;
-    }
-
     @Override
     public boolean remove(Long id) {
         BasicTemplate basicTemplate = this.getById(id);

+ 126 - 195
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/PrintCommonServiceImpl.java

@@ -172,66 +172,54 @@ public class PrintCommonServiceImpl implements PrintCommonService {
     @Override
     @Transactional
     public BasicAttachment saveAttachmentPdf(ClassifyEnum classifyEnum, ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException {
+        File htmlFileTemp = null;
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
-            String type = basicAttachment.getType();
-            String filePath = null;
-            String url = null;
+            String type = basicAttachment.getType(), filePath = null, url = null, ossStr = null;
             File htmlFile = null;
-            if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.FTL_PREFIX)) {
-                filePath = (String) jsonObject.get(SystemConstant.HTML_PATH);
-                if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig()) && filePath.contains(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                    url = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + filePath;
-                    htmlFile = new File(url);
-                } else {
-                    htmlFile = new File(dictionaryConfig.fssPublicDomain().getConfig(), filePath);
-                }
-            } else if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.HTML_PREFIX)) {
+
+            StringJoiner pdfStringJoiner = new StringJoiner("");
+            if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.HTML_PREFIX)) {
                 filePath = (String) jsonObject.get(SystemConstant.PATH);
                 UploadFileEnum uploadType = Enum.valueOf(UploadFileEnum.class, (String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
                 if (oss) {
-                    htmlFile = fileStoreUtil.ossDownload(filePath, url, uploadType.getFssType());
+                    ossStr = SystemConstant.OSS;
+                    htmlFileTemp = SystemConstant.getFileTempVar(SystemConstant.HTML_PREFIX);
+                    htmlFile = fileStoreUtil.ossDownload(filePath, htmlFileTemp.getPath(), uploadType.getFssType());
                 } else {
+                    ossStr = SystemConstant.LOCAL;
                     if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig()) && filePath.contains(dictionaryConfig.fssLocalFileDomain().getConfig())) {
                         url = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + filePath;
                         htmlFile = new File(url);
                     } else {
                         htmlFile = new File(dictionaryConfig.fssPublicDomain().getConfig(), filePath);
                     }
+                    if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                        pdfStringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
+                    }
                 }
             }
-            StringJoiner pdfStringJoiner = SystemConstant.getDirName(UploadFileEnum.PDF, true);
-            pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
 
+            pdfStringJoiner = SystemConstant.getDirName(pdfStringJoiner, UploadFileEnum.PDF, true);
+            pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
             String pdfDirName = pdfStringJoiner.toString();
-            String pdfDirNameStr = pdfStringJoiner.toString().replace("\\", "/");
-            String destUrl = null;
-            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                destUrl = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + pdfDirName;
-            } else {
-                destUrl = dictionaryConfig.fssPublicDomain().getConfig() + File.separator + pdfDirName;
-            }
-
-            File pdfFile = new File(destUrl);
-            if (!pdfFile.exists()) {
-                pdfFile.getParentFile().mkdirs();
-                pdfFile.createNewFile();
-            }
-            HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
+            File pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+            HtmlToPdfUtil.convert(htmlFile.getPath(), pdfFileTemp.getPath(), PageSizeEnum.A4);
 
 //            File pdfFile = asposePdfUtil.documentToPdf(htmlFile.getPath(), destUrl, PaperSize.A4);
             String htmlFileMd5 = DigestUtils.md5Hex(new FileInputStream(htmlFile));
-            byte[] data = ByteArray.fromFile(pdfFile).value();
-            String md5 = ByteArray.md5(data).toHexString();
-            InputStream ins = new ByteArrayInputStream(data);
-            //String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
-            PdfDto pdfDto = PdfUtil.addPdfPage(pdfFile);
-            if (oss) {//上传至oss
-                fileStoreUtil.ossUpload(pdfDirNameStr, ins, md5, fileStoreUtil.getUploadEnumByPath(pdfDirNameStr).getFssType());
-//                htmlFile.delete();
-                ins.close();
+            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFileTemp));
+            if (oss) {
+                fileStoreUtil.ossUpload(pdfDirName, pdfFileTemp, pdfFileMd5, fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
+            } else {
+                if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                    File file = new File(pdfStringJoiner.toString());
+                    FileCopyUtils.copy(file, pdfFileTemp);
+                }
             }
+
+            PdfDto pdfDto = PdfUtil.addPdfPage(pdfFileTemp);
             JSONObject attachmentPath = JSONObject.parseObject(examDetail.getAttachmentPath());
             attachmentPath = Objects.isNull(attachmentPath) ? new JSONObject() : attachmentPath;
             JSONArray jsonArray = (JSONArray) attachmentPath.get(SystemConstant.PATH);
@@ -240,14 +228,9 @@ public class PrintCommonServiceImpl implements PrintCommonService {
             object.put("printType", classifyEnum.name());
             object.put(SystemConstant.HTML_PATH, filePath);
             object.put("htmlMd5", htmlFileMd5);
-            object.put("pdfMd5", md5);
-            if (!oss) {
-                object.put(SystemConstant.PDF_PATH, pdfFile.getPath());
-                object.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-            } else {
-                object.put(SystemConstant.PDF_PATH, pdfDirNameStr);
-                object.put(SystemConstant.TYPE, SystemConstant.OSS);
-            }
+            object.put("pdfMd5", pdfFileMd5);
+            object.put(SystemConstant.PDF_PATH, pdfDirName);
+            object.put(SystemConstant.TYPE, ossStr);
             object.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
                     UploadFileEnum.HTML,
                     UploadFileEnum.PDF
@@ -257,7 +240,7 @@ public class PrintCommonServiceImpl implements PrintCommonService {
             examDetail.setAttachmentPath(attachmentPath.toJSONString());
             basicAttachment.setPages(pdfDto.getActualPageCount());
             for (int i = 0; i < printCount; i++) {
-                pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), sequence));
+                pdfList.add(new PdfDto(pdfFileTemp.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), sequence));
             }
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);
@@ -280,51 +263,44 @@ public class PrintCommonServiceImpl implements PrintCommonService {
     @Override
     @Transactional
     public void saveAttachmentSignPdf(PdfSignDto pdfFillDto, ExamDetail examDetail, List<PdfDto> pdfList, Integer printCount) {
+        File pdfFileTemp = null;
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
-            StringJoiner pdfStringJoiner = SystemConstant.getDirName(UploadFileEnum.PDF, true);
+            StringJoiner pdfStringJoiner = new StringJoiner("");
+            String ossStr = null;
+            if (!oss && Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                pdfStringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
+            }
+            pdfStringJoiner = SystemConstant.getDirName(pdfStringJoiner, UploadFileEnum.PDF, true);
             pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
 
-            String pdfDirName = pdfStringJoiner.toString();
-            String pdfDirNameStr = pdfStringJoiner.toString().replace("\\", "/");
+            pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+            createPrintPdfUtil.createSignPdf(pdfFillDto, pdfFileTemp.getPath());
 
-            String destUrl = null;
-            if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                destUrl = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator + pdfDirName;
+            String pdfDirName = pdfStringJoiner.toString().replaceAll("\\\\", "/");
+            //pdf生成和上传
+            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFileTemp));
+            if (oss) {
+                ossStr = SystemConstant.OSS;
+                fileStoreUtil.ossUpload(pdfDirName, pdfFileTemp, pdfFileMd5, fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
             } else {
-                destUrl = dictionaryConfig.fssPublicDomain().getConfig() + File.separator + pdfDirName;
-            }
-
-            createPrintPdfUtil.createSignPdf(pdfFillDto, destUrl);
-
-            File pdfFile = new File(destUrl);
-            if (!pdfFile.exists()) {
-                pdfFile.getParentFile().mkdirs();
-                pdfFile.createNewFile();
+                ossStr = SystemConstant.LOCAL;
+                if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                    File file = new File(pdfStringJoiner.toString());
+                    FileCopyUtils.copy(file, pdfFileTemp);
+                }
             }
-            PdfDto pdfDto = PdfUtil.addPdfPage(pdfFile);
+            PdfDto pdfDto = PdfUtil.addPdfPage(pdfFileTemp);
 
-            byte[] data = ByteArray.fromFile(pdfFile).value();
-            String md5 = ByteArray.md5(data).toHexString();
-            InputStream ins = new ByteArrayInputStream(data);
-            if (oss) {//上传至oss
-                fileStoreUtil.ossUpload(pdfDirNameStr, ins, md5, fileStoreUtil.getUploadEnumByPath(pdfDirNameStr).getFssType());
-                ins.close();
-            }
             JSONObject attachmentPath = JSONObject.parseObject(examDetail.getAttachmentPath());
             attachmentPath = Objects.isNull(attachmentPath) ? new JSONObject() : attachmentPath;
             JSONArray jsonArray = (JSONArray) attachmentPath.get(SystemConstant.PATH);
             jsonArray = Objects.isNull(jsonArray) ? new JSONArray() : jsonArray;
             JSONObject object = new JSONObject();
             object.put("printType", ClassifyEnum.SIGN.name());
-            object.put("pdfMd5", md5);
-            if (!oss) {
-                object.put(SystemConstant.PDF_PATH, pdfFile.getPath());
-                object.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-            } else {
-                object.put(SystemConstant.PDF_PATH, pdfDirNameStr);
-                object.put(SystemConstant.TYPE, SystemConstant.OSS);
-            }
+            object.put("pdfMd5", pdfFileMd5);
+            object.put(SystemConstant.PDF_PATH, pdfDirName);
+            object.put(SystemConstant.TYPE, ossStr);
             object.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
                     UploadFileEnum.PDF
             });
@@ -332,7 +308,7 @@ public class PrintCommonServiceImpl implements PrintCommonService {
             attachmentPath.put(SystemConstant.PATH, jsonArray);
             examDetail.setAttachmentPath(attachmentPath.toJSONString());
             for (int i = 0; i < printCount; i++) {
-                pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), 2));
+                pdfList.add(new PdfDto(pdfFileTemp.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), 2));
             }
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);
@@ -359,93 +335,67 @@ public class PrintCommonServiceImpl implements PrintCommonService {
     public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<PdfDto> localFileList) throws IOException {
         BasicAttachment basicAttachment = null;
         PdfDto pdfDto = null;
+        File htmlFileTemp = null, pdfFileTemp = null;
         try {
-            byte[] bytes = htmlContent.getBytes();
-            int size = bytes.length;
+            htmlFileTemp = SystemConstant.getFileTempVar(SystemConstant.HTML_PREFIX);
+            pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+            FileCopyUtils.copy(htmlContent.getBytes(), htmlFileTemp);
             boolean oss = dictionaryConfig.sysDomain().isOss();
             StringJoiner stringJoiner = new StringJoiner("");
+            StringJoiner pdfStringJoiner = new StringJoiner("");
             if (!oss && Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
                 stringJoiner.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator);
             }
+            if (!oss && Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                pdfStringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
+            }
             stringJoiner = SystemConstant.getDirName(stringJoiner, UploadFileEnum.HTML, true);
             stringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.HTML_PREFIX);
 
+            pdfStringJoiner = SystemConstant.getDirName(pdfStringJoiner, UploadFileEnum.PDF, true);
+            pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
+
             JSONObject jsonObject = new JSONObject();
-            String fileMd5 = null;
-            if (oss) {//上传至oss
-                String dirName = stringJoiner.toString().replaceAll("\\\\", "/");
-//                ossUtil.ossUpload(dirName, htmlContent);
-                fileStoreUtil.ossUpload(dirName, htmlContent, fileStoreUtil.getUploadEnumByPath(dirName).getFssType());
-                jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
-                jsonObject.put(SystemConstant.HTML_PATH, dirName);
+            String htmlDirName = stringJoiner.toString().replaceAll("\\\\", "/");
+            String pdfDirName = pdfStringJoiner.toString().replaceAll("\\\\", "/");
 
-                String url = null;
+            //html生成和上传
+            String htmlFileMd5 = DigestUtils.md5Hex(new FileInputStream(htmlFileTemp));
+            if (oss) {
+                jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
+                fileStoreUtil.ossUpload(htmlDirName, htmlFileTemp, htmlFileMd5, fileStoreUtil.getUploadEnumByPath(htmlDirName).getFssType());
+            } else {
                 if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
-                    url = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + dirName;
-                } else {
-                    url = dirName;
+                    File file = new File(stringJoiner.toString());
+                    FileCopyUtils.copy(file, htmlFileTemp);
                 }
-//                File localHtmlFile = ossUtil.ossDownload(dirName, url);
-                File localHtmlFile = fileStoreUtil.ossDownload(dirName, url, fileStoreUtil.getUploadEnumByPath(dirName).getFssType());
-                StringJoiner pdfStringJoiner = SystemConstant.getDirName(UploadFileEnum.PDF, true);
-                pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
+                jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
+            }
+            jsonObject.put(SystemConstant.HTML_PATH, htmlDirName);
 
-                String pdfDirName = pdfStringJoiner.toString();
+            HtmlToPdfUtil.convert(htmlFileTemp.getPath(), pdfFileTemp.getPath(), PageSizeEnum.A3);
 
-                String destUrl = null;
+            //pdf生成和上传
+            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFileTemp));
+            if (oss) {
+                fileStoreUtil.ossUpload(pdfDirName, pdfFileTemp, pdfFileMd5, fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
+            } else {
                 if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                    destUrl = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator + pdfDirName;
-                } else {
-                    destUrl = pdfDirName;
+                    File file = new File(pdfStringJoiner.toString());
+                    FileCopyUtils.copy(file, pdfFileTemp);
                 }
-//                destUrl = destUrl.replaceAll("\\\\","/");
+            }
 
-                HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
-//                File pdfFile = asposePdfUtil.documentToPdf(localHtmlFile.getPath(), destUrl, PaperSize.A3);
-                File pdfFile = new File(destUrl);
-                if (!pdfFile.exists()) {
-                    pdfFile.getParentFile().mkdirs();
-                    pdfFile.createNewFile();
-                }
-                pdfDto = PdfUtil.addPdfPage(pdfFile);
-                localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
-                fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
-//                ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
-                pdfDirName = pdfDirName.replaceAll("\\\\", "/");
-                fileStoreUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
-//                localHtmlFile.delete();
-                jsonObject.put(SystemConstant.PATH, pdfDirName);
-                // htmlMd5
-                jsonObject.put("htmlMd5", DigestUtils.md5Hex(new FileInputStream(localHtmlFile)));
-                jsonObject.put("pdfMd5", fileMd5);
-            } else {//上传至服务器
-                File finalFile = new File(stringJoiner.toString());
-                if (!finalFile.exists()) {
-                    finalFile.getParentFile().mkdirs();
-                    finalFile.createNewFile();
-                }
-                FileCopyUtils.copy(bytes, new File(stringJoiner.toString()));
-                jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-                jsonObject.put(SystemConstant.HTML_PATH, stringJoiner.toString());
-                String destUrl = finalFile.getPath().replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
-                HtmlToPdfUtil.convert(finalFile.getPath(), destUrl, PageSizeEnum.A3);
-                File pdfFile = new File(destUrl);
-                if (!pdfFile.exists()) {
-                    pdfFile.getParentFile().mkdirs();
-                    pdfFile.createNewFile();
-                }
-                pdfDto = PdfUtil.addPdfPage(pdfFile);
-                fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
-                localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
+            pdfDto = PdfUtil.addPdfPage(pdfFileTemp);
+            localFileList.add(new PdfDto(pdfFileTemp.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
+            jsonObject.put(SystemConstant.PATH, pdfDirName);
+            jsonObject.put("htmlMd5", htmlFileMd5);
+            jsonObject.put("pdfMd5", pdfFileMd5);
 
-                jsonObject.put(SystemConstant.PATH, destUrl);
-                jsonObject.put("htmlMd5", DigestUtils.md5Hex(new FileInputStream(finalFile)));
-                jsonObject.put("pdfMd5", fileMd5);
-            }
             jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
                     UploadFileEnum.HTML, UploadFileEnum.PDF
             });
-            basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(size), fileMd5, userId);
+            basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(htmlContent.getBytes().length), htmlFileMd5, userId);
             basicAttachment.setPages(pdfDto.getActualPageCount());
             basicAttachmentService.save(basicAttachment);
         } catch (Exception e) {
@@ -576,9 +526,8 @@ public class PrintCommonServiceImpl implements PrintCommonService {
      */
     @Override
     public BasicAttachment saveAttachmentCommon(MultipartFile file, String md5, UploadFileEnum type, Long objId) {
-        if (Objects.isNull(type)) {
-            throw ExceptionResultEnum.ATTACHMENT_TYPE_EMPTY.exception();
-        }
+        Optional.ofNullable(type).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_TYPE_EMPTY.exception());
+
         BasicAttachment basicAttachment = null;
         try {
             SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
@@ -1355,51 +1304,44 @@ public class PrintCommonServiceImpl implements PrintCommonService {
 
     @Override
     public void saveAttachmentPackagePdf(PdfPackageDto pdfPackageDto, ExamDetail examDetail, List<PdfDto> pdfList, Integer printCount) {
+        File pdfFileTemp = null;
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
-            StringJoiner pdfStringJoiner = SystemConstant.getDirName(UploadFileEnum.PDF, true);
+            StringJoiner pdfStringJoiner = new StringJoiner("");
+            String ossStr = null;
+            if (!oss && Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                pdfStringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
+            }
+            pdfStringJoiner = SystemConstant.getDirName(pdfStringJoiner, UploadFileEnum.PDF, true);
             pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
 
-            String pdfDirName = pdfStringJoiner.toString();
-            String pdfDirNameStr = pdfStringJoiner.toString().replace("\\", "/");
-
-            String destUrl = null;
-            if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                destUrl = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator + pdfDirName;
+            String pdfDirName = pdfStringJoiner.toString().replaceAll("\\\\", "/");
+            pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+            createPrintPdfUtil.createPackagePdf(pdfPackageDto, pdfFileTemp.getPath());
+            //pdf生成和上传
+            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFileTemp));
+            if (oss) {
+                ossStr = SystemConstant.OSS;
+                fileStoreUtil.ossUpload(pdfDirName, pdfFileTemp, pdfFileMd5, fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
             } else {
-                destUrl = dictionaryConfig.fssPublicDomain().getConfig() + File.separator + pdfDirName;
+                ossStr = SystemConstant.LOCAL;
+                if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                    File file = new File(pdfStringJoiner.toString());
+                    FileCopyUtils.copy(file, pdfFileTemp);
+                }
             }
 
-            createPrintPdfUtil.createPackagePdf(pdfPackageDto, destUrl);
+            PdfDto pdfDto = PdfUtil.addPdfPage(pdfFileTemp);
 
-            File pdfFile = new File(destUrl);
-            if (!pdfFile.exists()) {
-                pdfFile.getParentFile().mkdirs();
-                pdfFile.createNewFile();
-            }
-            PdfDto pdfDto = PdfUtil.addPdfPage(pdfFile);
-
-            byte[] data = ByteArray.fromFile(pdfFile).value();
-            String md5 = ByteArray.md5(data).toHexString();
-            InputStream ins = new ByteArrayInputStream(data);
-            if (oss) {//上传至oss
-                fileStoreUtil.ossUpload(pdfDirNameStr, ins, md5, fileStoreUtil.getUploadEnumByPath(pdfDirNameStr).getFssType());
-                ins.close();
-            }
             JSONObject attachmentPath = JSON.parseObject(examDetail.getAttachmentPath());
             attachmentPath = Objects.isNull(attachmentPath) ? new JSONObject() : attachmentPath;
             JSONArray jsonArray = (JSONArray) attachmentPath.get(SystemConstant.PATH);
             jsonArray = Objects.isNull(jsonArray) ? new JSONArray() : jsonArray;
             JSONObject object = new JSONObject();
             object.put("printType", ClassifyEnum.PACKAGE.name());
-            object.put("pdfMd5", md5);
-            if (!oss) {
-                object.put(SystemConstant.PDF_PATH, pdfFile.getPath());
-                object.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-            } else {
-                object.put(SystemConstant.PDF_PATH, pdfDirNameStr);
-                object.put(SystemConstant.TYPE, SystemConstant.OSS);
-            }
+            object.put("pdfMd5", pdfFileMd5);
+            object.put(SystemConstant.PDF_PATH, pdfDirName);
+            object.put(SystemConstant.TYPE, ossStr);
             object.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
                     UploadFileEnum.HTML,
                     UploadFileEnum.PDF
@@ -1408,7 +1350,7 @@ public class PrintCommonServiceImpl implements PrintCommonService {
             attachmentPath.put(SystemConstant.PATH, jsonArray);
             examDetail.setAttachmentPath(attachmentPath.toJSONString());
             for (int i = 0; i < printCount; i++) {
-                pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), 2));
+                pdfList.add(new PdfDto(pdfFileTemp.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), 2));
             }
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);
@@ -1429,32 +1371,21 @@ public class PrintCommonServiceImpl implements PrintCommonService {
     @Override
     public JSONObject uploadPdfFile(File pdfFile, ClassifyEnum classifyEnum) {
         try {
+            StringJoiner pdfStringJoiner = SystemConstant.getDirName(UploadFileEnum.PDF, true);
+            pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
+            String pdfDirName = pdfStringJoiner.toString();
+            pdfDirName = SystemConstant.getPdfFilePath(pdfDirName, pdfFile);
             boolean oss = dictionaryConfig.sysDomain().isOss();
-            String pdfDirName = pdfFile.getPath();
-
-            String pdfDirNameStr = null;
-            if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                String sysConfigPath = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator;
-                pdfDirNameStr = pdfDirName.replace("\\", "/").replaceAll(sysConfigPath.replace("\\", "/"), "");
-            } else {
-                pdfDirNameStr = pdfDirName;
-            }
 
             byte[] data = ByteArray.fromFile(pdfFile).value();
             String md5 = ByteArray.md5(data).toHexString();
-            InputStream ins = new ByteArrayInputStream(data);
-            if (oss) {//上传至oss
-                fileStoreUtil.ossUpload(pdfDirNameStr, ins, md5, UploadFileEnum.PDF.getFssType());
-                ins.close();
-            }
             JSONObject object = new JSONObject();
             object.put("printType", classifyEnum);
             object.put("pdfMd5", md5);
+            object.put(SystemConstant.PATH, pdfDirName);
             if (!oss) {
-                object.put(SystemConstant.PATH, pdfDirName);
                 object.put(SystemConstant.TYPE, SystemConstant.LOCAL);
             } else {
-                object.put(SystemConstant.PATH, pdfDirNameStr);
                 object.put(SystemConstant.TYPE, SystemConstant.OSS);
             }
             object.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.PDF);

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

@@ -546,7 +546,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             SysUser sysUser = (SysUser) map.get(SystemConstant.USER);
             Long examDetailId = tbTask.getEntityId();
             List<Long> examDetailIds = Objects.nonNull(map.get("examDetailIds")) ? (List<Long>) map.get("examDetailIds") : null;
-            Long schoolId = tbTask.getSchoolId();
+//            Long schoolId = tbTask.getSchoolId();
             List<Long> examDetailCourseIds = Objects.nonNull(map.get("examDetailCourseIds")) ? (List<Long>) map.get("examDetailCourseIds") : null;
             String paperTypeParam = Objects.nonNull(map.get("paperType")) ? (String) map.get("paperType") : null;
 
@@ -576,7 +576,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 throw ExceptionResultEnum.EXAM_DETAIL_IS_NULL.exception();
             }
             Set<Long> attachmentIds = new HashSet<>();
-            Set<File> ftlList = new HashSet<>();
+//            Set<File> ftlList = new HashSet<>();
             for (ExamDetail examDetail : examDetailList) {
                 tbTask.setObjName(examDetail.getExamRoom() + "-" + examDetail.getExamPlace());
                 //查询examDetailCourse

+ 47 - 48
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/CreatePdfUtil.java

@@ -119,11 +119,8 @@ public class CreatePdfUtil {
      * @param printCount
      */
     public void createCheckIn(ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount) throws Exception {
-//        BasicSchool basicSchool = basicSchoolService.getById(examDetail.getSchoolId());
-//        Map<String, List<String>> flowSchoolCodeMap = SystemConstant.getFlowSchoolCodeMap();
-        if (Objects.isNull(basicAttachment)) {
-            throw ExceptionResultEnum.ATTACHMENT_IS_NULL.exception();
-        }
+        Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception());
+
         String type = basicAttachment.getType();
         JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
 
@@ -936,9 +933,8 @@ public class CreatePdfUtil {
             if (Objects.nonNull(object.get("attachmentId")) && !Objects.equals("", object.get("attachmentId"))) {
                 Long attachmentId = Long.parseLong((String) (object.get("attachmentId")));
                 BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-                if (Objects.isNull(basicAttachment)) {
-                    throw ExceptionResultEnum.ATTACHMENT_IS_NULL.exception();
-                }
+                Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception("所在路径不存在"));
+
                 String name = (String) object.get("name");
                 JSONObject jsonObject = (JSONObject) object.get("original");
                 // 返回抽中的试卷
@@ -1119,32 +1115,36 @@ public class CreatePdfUtil {
                                           BasicExam basicExam,
                                           List<PdfDto> mergePdfDeleteList) throws Exception {
         boolean oss = dictionaryConfig.sysDomain().isOss();
-        StringJoiner stringJoiner = new StringJoiner("");
-        if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-            stringJoiner = stringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
-        } else {
-            stringJoiner = stringJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-        }
-        // oss上只认"/",windows生成的路径分隔符全部替换为"/"
+//        StringJoiner stringJoiner = new StringJoiner("");
+//        if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+//            stringJoiner = stringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
+//        } else {
+//            stringJoiner = stringJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
+//        }
+//        // oss上只认"/",windows生成的路径分隔符全部替换为"/"
         File localA4PdfFile = null, localPaperPdfFile = null, localA3PdfCardFile = null;
         BasicAttachment basicAttachment = null;
-        if (Objects.nonNull(dirNameA4)) {
-            dirNameA4 = dirNameA4.replaceAll("\\\\", "/");
-            localA4PdfFile = new File(stringJoiner.toString() + File.separator + dirNameA4);
-        }
+//        if (Objects.nonNull(dirNameA4)) {
+//            dirNameA4 = dirNameA4.replaceAll("\\\\", "/");
+//            localA4PdfFile = new File(stringJoiner.toString() + File.separator + dirNameA4);
+//        }
         // 试卷+题卡
         if (Objects.nonNull(dirNamePaper)) {
             dirNamePaper = dirNamePaper.replaceAll("\\\\", "/");
-            localPaperPdfFile = new File(stringJoiner.toString() + File.separator + dirNamePaper);
+            localPaperPdfFile = new File(dirNamePaper);
             basicAttachment = basicAttachmentService.saveAttachmentPdf(dirNamePaper, userId);
-            tbTask.setImportFileName(basicAttachment.getName());
-            tbTask.setImportFilePath(basicAttachment.getPath());
+            if (StringUtils.isBlank(tbTask.getImportFileName())) {
+                tbTask.setImportFileName(basicAttachment.getName());
+            }
+            if (StringUtils.isBlank(tbTask.getImportFilePath())) {
+                tbTask.setImportFilePath(basicAttachment.getPath());
+            }
             examDetail.setAttachmentId(basicAttachment.getId());
         }
         // 题卡
         if (Objects.nonNull(dirNameCardA3)) {
             dirNameCardA3 = dirNameCardA3.replaceAll("\\\\", "/");
-            localA3PdfCardFile = new File(stringJoiner.toString() + File.separator + dirNameCardA3);
+            localA3PdfCardFile = new File(dirNameCardA3);
             basicAttachment = basicAttachmentService.saveAttachmentPdf(dirNameCardA3, userId);
             if (StringUtils.isBlank(tbTask.getImportFileName())) {
                 tbTask.setImportFileName(basicAttachment.getName());
@@ -1163,36 +1163,36 @@ public class CreatePdfUtil {
             examDetail.setStatus(ExamDetailStatusEnum.READY);
         }
         detailService.saveOrUpdate(examDetail);
-        if (Objects.nonNull(localPaperPdfFile) && oss) {
-//            ossUtil.ossUpload(dirNameA3, localA3PdfFile, DigestUtils.md5Hex(new FileInputStream(localA3PdfFile)));
-            fileStoreUtil.ossUpload(dirNamePaper, localPaperPdfFile, DigestUtils.md5Hex(new FileInputStream(localPaperPdfFile)), fileStoreUtil.getUploadEnumByPath(dirNamePaper).getFssType());
-            localPaperPdfFile.delete();
-        }
-        if (Objects.nonNull(localA3PdfCardFile) && oss) {
-            fileStoreUtil.ossUpload(dirNameCardA3, localA3PdfCardFile, DigestUtils.md5Hex(new FileInputStream(localA3PdfCardFile)), fileStoreUtil.getUploadEnumByPath(dirNameCardA3).getFssType());
-            localA3PdfCardFile.delete();
-        }
-        if (Objects.nonNull(localA4PdfFile) && oss) {
-            localA4PdfFile.delete();
-        }
-        //删除题卡临时文件
-//        if (oss) {
-        for (PdfDto pdfDto : mergePdfDeleteList) {
-            if (Objects.nonNull(pdfDto)) {
-                new File(pdfDto.getPath()).delete();
-            }
-        }
+//        if (Objects.nonNull(localPaperPdfFile) && oss) {
+////            ossUtil.ossUpload(dirNameA3, localA3PdfFile, DigestUtils.md5Hex(new FileInputStream(localA3PdfFile)));
+//            fileStoreUtil.ossUpload(dirNamePaper, localPaperPdfFile, DigestUtils.md5Hex(new FileInputStream(localPaperPdfFile)), fileStoreUtil.getUploadEnumByPath(dirNamePaper).getFssType());
+//            localPaperPdfFile.delete();
 //        }
-//        for (PdfDto pdfDto : mergePdfA3List) {
-//            if (Objects.nonNull(pdfDto)) {
-//                new File(pdfDto.getPath()).delete();
-//            }
+//        if (Objects.nonNull(localA3PdfCardFile) && oss) {
+//            fileStoreUtil.ossUpload(dirNameCardA3, localA3PdfCardFile, DigestUtils.md5Hex(new FileInputStream(localA3PdfCardFile)), fileStoreUtil.getUploadEnumByPath(dirNameCardA3).getFssType());
+//            localA3PdfCardFile.delete();
 //        }
-//        for (PdfDto pdfDto : mergePdfA4List) {
+//        if (Objects.nonNull(localA4PdfFile) && oss) {
+//            localA4PdfFile.delete();
+//        }
+        //删除题卡临时文件
+////        if (oss) {
+//        for (PdfDto pdfDto : mergePdfDeleteList) {
 //            if (Objects.nonNull(pdfDto)) {
 //                new File(pdfDto.getPath()).delete();
 //            }
 //        }
+////        }
+////        for (PdfDto pdfDto : mergePdfA3List) {
+////            if (Objects.nonNull(pdfDto)) {
+////                new File(pdfDto.getPath()).delete();
+////            }
+////        }
+////        for (PdfDto pdfDto : mergePdfA4List) {
+////            if (Objects.nonNull(pdfDto)) {
+////                new File(pdfDto.getPath()).delete();
+////            }
+////        }
         return basicAttachment;
     }
 
@@ -1437,7 +1437,6 @@ public class CreatePdfUtil {
             cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-A4 page-box-0 page-box-less\">", packageCodeDiv);
         }
 
-
         BasicAttachment cardAttachment = printCommonService.saveAttachmentHtml(examDetail.getSchoolId() + "|" + examDetailCourse.getCourseCode(), cardTemp, userId, cardPdfList);
         JSONObject object = new JSONObject();
         object.put("name", paperType);

+ 5 - 17
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -16,7 +16,6 @@ import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.enums.PageSizeEnum;
 import com.qmth.teachcloud.common.enums.UploadFileEnum;
 import com.qmth.teachcloud.common.service.CommonCacheService;
-import com.qmth.teachcloud.common.util.JacksonUtil;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -45,28 +44,17 @@ public class PdfUtil {
      * @param outputPath
      */
     public static String mergePdf(String[] files, String outputPath) {
-        if (Objects.nonNull(files)) {
-            log.info("files:{},outputPath:{}", JacksonUtil.parseJson(files), outputPath);
-        } else {
-            log.info("files is null");
-        }
         Document document = null;
         PdfCopy copy = null;
-        StringJoiner dirName = null;
+        StringJoiner dirName = new StringJoiner("");
+        File pdfFileTemp = null;
         try {
             if (Objects.nonNull(files) && files.length > 0) {
                 document = new Document(new PdfReader(ByteArray.fromFile(new File(files[0])).value()).getPageSize(1));
                 if (Objects.isNull(outputPath)) {
-                    DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
-                    StringJoiner stringJoiner = new StringJoiner("");
-                    if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                        stringJoiner = stringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
-                    } else {
-                        stringJoiner = stringJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-                    }
-                    dirName = SystemConstant.getDirName(UploadFileEnum.PDF, true);
-                    dirName.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
-                    outputPath = stringJoiner.toString() + dirName.toString();
+                    pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+                    dirName.add(pdfFileTemp.getPath());
+                    outputPath = dirName.toString();
                 }
                 copy = new PdfSmartCopy(document, new FileOutputStream(outputPath));
                 document.open();

+ 69 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -4,6 +4,7 @@ import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
 import com.qmth.boot.core.uid.service.UidService;
 import com.qmth.teachcloud.common.annotation.DBVerify;
 import com.qmth.teachcloud.common.bean.result.DBVerifyResult;
+import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.entity.BasicSchool;
 import com.qmth.teachcloud.common.entity.SysUser;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
@@ -11,8 +12,12 @@ import com.qmth.teachcloud.common.enums.TFCustomTypeEnum;
 import com.qmth.teachcloud.common.enums.UploadFileEnum;
 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 org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.cglib.beans.BeanMap;
 
 import java.io.*;
@@ -901,4 +906,68 @@ public class SystemConstant {
         }
         return stringJoiner;
     }
+
+    /**
+     * 获取pdf文件路径
+     *
+     * @param pdfDirName
+     * @param file
+     * @return
+     * @throws Exception
+     */
+    public static String getPdfFilePath(String pdfDirName, File file) throws Exception {
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
+        boolean oss = dictionaryConfig.sysDomain().isOss();
+        if (oss) {
+            fileStoreUtil.ossUpload(pdfDirName, file, DigestUtils.md5Hex(new FileInputStream(file)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
+            return pdfDirName;
+        } else {
+            if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                String destUrl = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator + pdfDirName;
+                File newFile = new File(destUrl);
+                if (!newFile.exists()) {
+                    newFile.getParentFile().mkdirs();
+                    newFile.createNewFile();
+                }
+                FileUtils.copyFile(file, newFile);
+                return destUrl;
+            } else {
+                fileStoreUtil.ossUpload(pdfDirName, file, DigestUtils.md5Hex(new FileInputStream(file)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
+                return pdfDirName;
+            }
+        }
+    }
+
+    /**
+     * 获取普通文件路径
+     *
+     * @param fileDirName
+     * @param file
+     * @return
+     * @throws Exception
+     */
+    public static String getFilePath(String fileDirName, File file) throws Exception {
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
+        boolean oss = dictionaryConfig.sysDomain().isOss();
+        if (oss) {
+            fileStoreUtil.ossUpload(fileDirName, file, DigestUtils.md5Hex(new FileInputStream(file)), fileStoreUtil.getUploadEnumByPath(fileDirName).getFssType());
+            return fileDirName;
+        } else {
+            if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig())) {
+                String destUrl = dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileDirName;
+                File newFile = new File(destUrl);
+                if (!newFile.exists()) {
+                    newFile.getParentFile().mkdirs();
+                    newFile.createNewFile();
+                }
+                FileUtils.copyFile(file, newFile);
+                return destUrl;
+            } else {
+                fileStoreUtil.ossUpload(fileDirName, file, DigestUtils.md5Hex(new FileInputStream(file)), fileStoreUtil.getUploadEnumByPath(fileDirName).getFssType());
+                return fileDirName;
+            }
+        }
+    }
 }

+ 14 - 14
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/BasicAttachmentServiceImpl.java

@@ -68,28 +68,28 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
     public BasicAttachment saveAttachmentPdf(String dirName, Long userId) throws IOException {
         BasicAttachment basicAttachment = null;
         try {
-            StringJoiner stringJoiner = new StringJoiner("");
-            if (Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
-                stringJoiner = stringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
-            } else {
-                stringJoiner = stringJoiner.add(dictionaryConfig.fssPublicDomain().getConfig()).add(File.separator);
-            }
-            File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
-            if (!localPdfFile.getParentFile().exists()) {
-                localPdfFile.getParentFile().mkdirs();
-                localPdfFile.createNewFile();
-            }
+            File pdfFile = new File(dirName);
             boolean oss = dictionaryConfig.sysDomain().isOss();
+            StringJoiner pdfStringJoiner = new StringJoiner("");
+            if (!oss && Objects.nonNull(dictionaryConfig.fssLocalPdfDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalPdfDomain().getConfig())) {
+                pdfStringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator);
+            }
+            pdfStringJoiner = SystemConstant.getDirName(pdfStringJoiner, UploadFileEnum.PDF, true);
+            pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
+
+            String pdfDirName = pdfStringJoiner.toString().replaceAll("\\\\", "/");
+            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
+
             JSONObject jsonPdf = new JSONObject();
+            jsonPdf.put(SystemConstant.PATH, pdfDirName);
             if (oss) {
                 jsonPdf.put(SystemConstant.TYPE, SystemConstant.OSS);
-                jsonPdf.put(SystemConstant.PATH, dirName);
+                fileStoreUtil.ossUpload(pdfDirName, pdfFile, pdfFileMd5, fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
             } else {
                 jsonPdf.put(SystemConstant.TYPE, SystemConstant.LOCAL);
-                jsonPdf.put(SystemConstant.PATH, localPdfFile);
             }
             jsonPdf.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.PDF);
-            basicAttachment = new BasicAttachment(jsonPdf.toJSONString(), localPdfFile.getName(), SystemConstant.PDF_PREFIX, new BigDecimal(localPdfFile.length()), DigestUtils.md5Hex(new FileInputStream(localPdfFile)), userId);
+            basicAttachment = new BasicAttachment(jsonPdf.toJSONString(), pdfFile.getName(), SystemConstant.PDF_PREFIX, new BigDecimal(pdfFile.length()), pdfFileMd5, userId);
             save(basicAttachment);
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);

+ 9 - 3
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/TeachcloudCommonServiceImpl.java

@@ -601,14 +601,17 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
         JSONObject jsonObject = JSONObject.parseObject(path);
         String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
         String filePath = pdf ? (String) jsonObject.get(SystemConstant.PDF_PATH) : (String) jsonObject.get(SystemConstant.PATH);
-        UploadFileEnum uploadType = Enum.valueOf(UploadFileEnum.class, (String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
+        Optional.ofNullable(filePath).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception("附件路径不存在"));
 
+        UploadFileEnum uploadType = Enum.valueOf(UploadFileEnum.class, (String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
         StringJoiner localPath = new StringJoiner("");
         if (Objects.equals(attachmentType, SystemConstant.OSS)) {
             if (Objects.nonNull(dictionaryConfig.fssLocalFileDomain()) && !StringUtils.isBlank(dictionaryConfig.fssLocalFileDomain().getConfig()) && !filePath.contains(dictionaryConfig.fssLocalFileDomain().getConfig())) {
                 localPath = localPath.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator).add(filePath);
             } else {
-                localPath = localPath.add(filePath);
+                String suffix = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
+                File fileTemp = SystemConstant.getFileTempVar(suffix);
+                localPath = localPath.add(fileTemp.getPath());
             }
             file = fileStoreUtil.ossDownload(filePath, localPath.toString(), uploadType.getFssType());
         } else {
@@ -616,7 +619,10 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
                 localPath = localPath.add(dictionaryConfig.fssLocalFileDomain().getConfig()).add(File.separator).add(filePath);
                 file = new File(localPath.toString());
             } else {
-                file = new File(dictionaryConfig.fssPublicDomain().getConfig(), filePath);
+                String suffix = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
+                File fileTemp = SystemConstant.getFileTempVar(suffix);
+                localPath = localPath.add(fileTemp.getPath());
+                file = fileStoreUtil.ossDownload(filePath, localPath.toString(), uploadType.getFssType());
             }
         }
         return file;