浏览代码

题库预览处理

wangliang 2 年之前
父节点
当前提交
95b79a7003

+ 71 - 24
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicTemplateServiceImpl.java

@@ -27,7 +27,9 @@ 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;
@@ -36,6 +38,7 @@ 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;
@@ -71,6 +74,9 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
     @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());
@@ -211,34 +217,75 @@ public class BasicTemplateServiceImpl extends ServiceImpl<BasicTemplateMapper, B
         pdfStringJoiner.add(SystemConstant.getNanoId()).add(SystemConstant.PDF_PREFIX);
         String pdfDirName = pdfStringJoiner.toString();
 
-        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 file = null;
+        try {
+            file = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+            String destUrl = file.getPath();
+            // 签到表
+            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);
+                }
+                return basicTemplate.getPreviewPath() == null ? null : teachcloudCommonService.filePreview(basicTemplate.getPreviewPath());
+            } 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);
+                }
+                return basicTemplate.getPreviewPath() == null ? null : teachcloudCommonService.filePreview(basicTemplate.getPreviewPath());
+            } else if (ClassifyEnum.CHECK_IN.equals(classifyEnum)) {
+                BasicAttachment attachment = basicAttachmentService.getById(basicTemplate.getAttachmentId());
+                return attachment == null ? null : teachcloudCommonService.filePreview(attachment.getPath());
+            } else {
+                throw ExceptionResultEnum.ERROR.exception("不支持当前模板预览");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(file)) {
+                file.delete();
+            }
         }
+        return null;
+    }
 
-        // 签到表
-        if (ClassifyEnum.SIGN.equals(classifyEnum)) {
-            if (StringUtils.isBlank(basicTemplate.getPreviewPath())) {
-                JSONObject json = PdfFillUtils.parseSignTempData(basicTemplate.getDisplayRange(), destUrl);
-                basicTemplate.setPreviewPath(JSON.toJSONString(json));
-                basicTemplateMapper.updateById(basicTemplate);
-            }
-            return basicTemplate.getPreviewPath() == null ? null : teachcloudCommonService.filePreview(basicTemplate.getPreviewPath());
-        } else if (ClassifyEnum.PACKAGE.equals(classifyEnum)) {
-            if (StringUtils.isBlank(basicTemplate.getPreviewPath())) {
-                JSONObject json = PdfFillUtils.packageTempData(basicTemplate.getDisplayRange(), destUrl);
-                basicTemplate.setPreviewPath(JSON.toJSONString(json));
-                basicTemplateMapper.updateById(basicTemplate);
+    /**
+     * 获取预览路径
+     *
+     * @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 basicTemplate.getPreviewPath() == null ? null : teachcloudCommonService.filePreview(basicTemplate.getPreviewPath());
-        } else if (ClassifyEnum.CHECK_IN.equals(classifyEnum)) {
-            BasicAttachment attachment = basicAttachmentService.getById(basicTemplate.getAttachmentId());
-            return attachment == null ? null : teachcloudCommonService.filePreview(attachment.getPath());
-        } else {
-            throw ExceptionResultEnum.ERROR.exception("不支持当前模板预览");
         }
+        return json;
     }
 
     @Override

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

@@ -189,10 +189,10 @@ public class AuthInfoServiceImpl implements AuthInfoService {
                     File fileTemp = null;
                     String dirName = null;
                     try {
-//                    File fileNew = new File("/Volumes/extend/图库/spring.jpg");
-//                    fileTemp = File.createTempFile("temp", ".jpg");
+//                        File fileNew = new File("/Volumes/extend/图库/spring.jpg");
+                        fileTemp = File.createTempFile("temp", ".jpg");
                         SystemConstant.base64ToImage(o.getLogo(), fileTemp.getPath());
-//                    FileUtils.copyInputStreamToFile(new FileInputStream(fileNew), fileTemp);
+//                        FileUtils.copyInputStreamToFile(new FileInputStream(fileNew), fileTemp);
 
                         dirName = stringJoiner.toString() + SystemConstant.getNanoId() + "." + FilenameUtils.getExtension(fileTemp.getPath());
                         fileStoreUtil.ossUpload(dirName, fileTemp, DigestUtils.md5Hex(new FileInputStream(fileTemp)), UploadFileEnum.FILE.getFssType());