|
|
@@ -5,9 +5,11 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.lowagie.text.*;
|
|
|
-import com.lowagie.text.pdf.*;
|
|
|
+import com.lowagie.text.pdf.BaseFont;
|
|
|
+import com.lowagie.text.pdf.PdfPCell;
|
|
|
+import com.lowagie.text.pdf.PdfPTable;
|
|
|
+import com.lowagie.text.pdf.PdfWriter;
|
|
|
import com.xunmei.common.core.constant.CacheConstants;
|
|
|
import com.xunmei.common.core.domain.IdName;
|
|
|
import com.xunmei.common.core.domain.registerbook.dto.CoreRegisterBookPdfExportDto;
|
|
|
@@ -28,7 +30,9 @@ import com.xunmei.file.vo.PdfFilePathVo;
|
|
|
import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
|
|
|
import com.xunmei.system.api.vo.SysOrgVO;
|
|
|
import io.netty.util.internal.StringUtil;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.text.StringEscapeUtils;
|
|
|
import org.apache.tools.zip.ZipEntry;
|
|
|
import org.apache.tools.zip.ZipOutputStream;
|
|
|
import org.slf4j.Logger;
|
|
|
@@ -37,10 +41,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
@@ -53,7 +54,7 @@ import java.net.URLDecoder;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
-import java.time.Duration;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.util.List;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
@@ -61,6 +62,8 @@ import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import static java.util.regex.Pattern.compile;
|
|
|
+
|
|
|
/**
|
|
|
* 本地文件存储
|
|
|
*
|
|
|
@@ -72,6 +75,7 @@ import java.util.stream.Collectors;
|
|
|
public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(LocalSysFileServiceImpl.class);
|
|
|
+
|
|
|
public static final String TEMP_DIR_NAME = "registerBookPdfBatchExportTempDir";
|
|
|
|
|
|
@Value("${file.path}")
|
|
|
@@ -85,20 +89,36 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
- private static PdfFilePathVo getLocalFilePath(String localFilePath, String businessType, String fileName) {
|
|
|
- fileName = filterPath(fileName);
|
|
|
- businessType = filterPath(businessType);
|
|
|
- final String path = File.separator + businessType + File.separator + DateUtil.format(new Date(), "yyyy" + File.separator + "MM" + File.separator + "dd" + File.separator);
|
|
|
- final File file = new File(localFilePath + path);
|
|
|
+ private PdfFilePathVo getLocalFilePath(String businessType, String fileName) {
|
|
|
+ // 验证输入
|
|
|
+ if (!isValidFileName(fileName)) {
|
|
|
+ throw new IllegalArgumentException("Invalid file name");
|
|
|
+ }
|
|
|
+ final String path = File.separator + businessType +
|
|
|
+ File.separator +
|
|
|
+ DateUtil.format(new Date(), "yyyy" + File.separator + "MM" + File.separator + "dd" +
|
|
|
+ File.separator);
|
|
|
+ String filePath = localFilePath + path;
|
|
|
+ // 规范化路径
|
|
|
+ String absolutePath = Paths.get(filePath).toAbsolutePath().normalize().toString();
|
|
|
+
|
|
|
+
|
|
|
+ final File file = FileUtils.getFile(absolutePath);
|
|
|
if (!file.exists()) {
|
|
|
file.mkdirs();
|
|
|
}
|
|
|
+
|
|
|
PdfFilePathVo pathVo = new PdfFilePathVo();
|
|
|
pathVo.setAbsolutePath(localFilePath + path + fileName);
|
|
|
pathVo.setRelativePath(path + fileName);
|
|
|
return pathVo;
|
|
|
}
|
|
|
|
|
|
+ private boolean isValidFileName(String fileName) {
|
|
|
+ // 使用正则表达式检查文件名是否合法
|
|
|
+ return fileName.matches("[a-zA-Z0-9._\\-]+");
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 修复路径操纵bug
|
|
|
*
|
|
|
@@ -106,18 +126,12 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
* @return
|
|
|
*/
|
|
|
private static String filterPath(String param) {
|
|
|
- Pattern pattern = Pattern.compile("[/\\:*?<>|]");
|
|
|
+ Pattern pattern = compile("[/\\:*?<>|]");
|
|
|
Matcher matcher = pattern.matcher(param);
|
|
|
param = matcher.replaceAll("");
|
|
|
return param;
|
|
|
}
|
|
|
|
|
|
- private static String filterHeader(String param) {
|
|
|
- Pattern pattern = Pattern.compile("[/\\:*?<>|=\\r\\n]");
|
|
|
- Matcher matcher = pattern.matcher(param);
|
|
|
- param = matcher.replaceAll("");
|
|
|
- return param;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 本地文件上传接口
|
|
|
@@ -129,14 +143,12 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
@Override
|
|
|
public String uploadFile(MultipartFile file) throws Exception {
|
|
|
String name = FileUploadUtils.upload(localFilePath, file);
|
|
|
-// String url = domain + localFilePrefix + name;
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String uploadFile(MultipartFile file, String busType) throws Exception {
|
|
|
String name = FileUploadUtils.upload(localFilePath, file, busType);
|
|
|
-// String url = domain + localFilePrefix + name;
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
@@ -179,40 +191,6 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void getFileStream(String path, HttpServletResponse response) {
|
|
|
- if (ObjectUtil.isEmpty(path)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (!path.startsWith(this.localFilePath)) {
|
|
|
- path = this.localFilePath + path;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- File file = new File(path);
|
|
|
- FileInputStream inputStream = new FileInputStream(file);
|
|
|
- int i = path.lastIndexOf(File.separator);
|
|
|
- String fileName = path.substring(i + 1);
|
|
|
- fileName = filterHeader(fileName);
|
|
|
- // 设置响应头
|
|
|
- response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
|
|
|
- response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
-
|
|
|
- // 将文件流写入响应输出流
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int bytesRead;
|
|
|
- while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
- response.getOutputStream().write(buffer, 0, bytesRead);
|
|
|
- }
|
|
|
- inputStream.close();
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取文件内容失败!");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public String getRelativePath(String path) {
|
|
|
if (ObjectUtil.isEmpty(path)) {
|
|
|
@@ -242,8 +220,11 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
|
|
|
@Override
|
|
|
public String generateEduTrainingPdf(Map<String, Object> data) throws Exception {
|
|
|
- PdfFilePathVo pathVo = getLocalFilePath(localFilePath, "edu", data.get("fileName").toString());
|
|
|
- log.info("开始生成教育培训登记簿,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ String fileName = filterPath(data.get("fileName").toString());
|
|
|
+
|
|
|
+ PdfFilePathVo pathVo = getLocalFilePath("edu", fileName);
|
|
|
+ String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
|
|
|
+ log.info("开始生成教育培训登记簿,当前绝对地址为:{}", afterStr);
|
|
|
final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 6, 10);
|
|
|
final Document document = pdfTableVo.getDocument();
|
|
|
final PdfWriter writer = pdfTableVo.getWriter();
|
|
|
@@ -254,15 +235,16 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
PdfUtil.dealEduBody(document, table, tableFont, data);
|
|
|
document.close();
|
|
|
writer.close();
|
|
|
- log.info("教育培训登记簿生成结束,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ log.info("教育培训登记簿生成结束,当前绝对地址为:{}", afterStr);
|
|
|
//此处返回 /statics/edu/xxx.pdf
|
|
|
return this.prefix + pathVo.getRelativePath();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String generateResumptionPdf(Map<String, Object> data) throws Exception {
|
|
|
- PdfFilePathVo pathVo = getLocalFilePath(localFilePath, "resumption", data.get("fileName").toString());
|
|
|
- log.info("开始生成履职登记簿,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ PdfFilePathVo pathVo = getLocalFilePath("resumption", data.get("fileName").toString());
|
|
|
+ String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
|
|
|
+ log.info("开始生成履职登记簿,当前绝对地址为:{}", afterStr);
|
|
|
Document document = new Document();
|
|
|
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pathVo.getAbsolutePath()));
|
|
|
document.open();
|
|
|
@@ -277,15 +259,16 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
PdfUtil.dealResumptionBody(document, table, tableFont, data);
|
|
|
document.close();
|
|
|
writer.close();
|
|
|
- log.info("履职登记簿生成结束,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ log.info("履职登记簿生成结束,当前绝对地址为:{}", afterStr);
|
|
|
//此处返回 /statics/edu/xxx.pdf
|
|
|
return this.prefix + pathVo.getRelativePath();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String generateSafeCheckPdf(SafeCheckTaskRegisterBookVo data) throws Exception {
|
|
|
- PdfFilePathVo pathVo = getLocalFilePath(localFilePath, "safeCheck", data.getDest());
|
|
|
- log.info("开始生成安全检查登记簿,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ PdfFilePathVo pathVo = getLocalFilePath("safeCheck", data.getDest());
|
|
|
+ String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
|
|
|
+ log.info("开始生成安全检查登记簿,当前绝对地址为:{}", afterStr);
|
|
|
final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 46, 7);
|
|
|
final Document document = pdfTableVo.getDocument();
|
|
|
final PdfWriter writer = pdfTableVo.getWriter();
|
|
|
@@ -324,15 +307,16 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
|
|
|
document.close();
|
|
|
writer.close();
|
|
|
- log.info("安全检查登记簿生成结束,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ log.info("安全检查登记簿生成结束,当前绝对地址为:{}", afterStr);
|
|
|
//此处返回 /statics/edu/xxx.pdf
|
|
|
return this.prefix + pathVo.getRelativePath();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String generateDrillPdf(Map<String, Object> data) throws Exception {
|
|
|
- PdfFilePathVo pathVo = getLocalFilePath(localFilePath, "drill", data.get("fileName").toString());
|
|
|
- log.info("开始生成预案演练登记簿,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ PdfFilePathVo pathVo = getLocalFilePath("drill", data.get("fileName").toString());
|
|
|
+ String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
|
|
|
+ log.info("开始生成预案演练登记簿,当前绝对地址为:{}", afterStr);
|
|
|
final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 6, 10);
|
|
|
final Document document = pdfTableVo.getDocument();
|
|
|
final PdfWriter writer = pdfTableVo.getWriter();
|
|
|
@@ -344,7 +328,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
PdfUtil.dealDrillBody(document, table, tableFont, data);
|
|
|
document.close();
|
|
|
writer.close();
|
|
|
- log.info("预案演练登记簿生成结束,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ log.info("预案演练登记簿生成结束,当前绝对地址为:{}", afterStr);
|
|
|
//此处返回 /statics/edu/xxx.pdf
|
|
|
return this.prefix + pathVo.getRelativePath();
|
|
|
|
|
|
@@ -352,8 +336,9 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
|
|
|
@Override
|
|
|
public String generateOutInPdf(Map<String, Object> data) throws Exception {
|
|
|
- PdfFilePathVo pathVo = getLocalFilePath(localFilePath, "visit", data.get("fileName").toString());
|
|
|
- log.info("开始生成来访管理登记簿,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ PdfFilePathVo pathVo = getLocalFilePath("visit", data.get("fileName").toString());
|
|
|
+ String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
|
|
|
+ log.info("开始生成来访管理登记簿,当前绝对地址为:{}", afterStr);
|
|
|
final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 6, 10);
|
|
|
final Document document = pdfTableVo.getDocument();
|
|
|
final PdfWriter writer = pdfTableVo.getWriter();
|
|
|
@@ -365,7 +350,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
PdfUtil.dealOutInBody(document, table, tableFont, data);
|
|
|
document.close();
|
|
|
writer.close();
|
|
|
- log.info("来访管理登记簿生成结束,当前绝对地址为:{}", pathVo.getAbsolutePath());
|
|
|
+ log.info("来访管理登记簿生成结束,当前绝对地址为:{}", afterStr);
|
|
|
//此处返回 /statics/edu/xxx.pdf
|
|
|
return this.prefix + pathVo.getRelativePath();
|
|
|
}
|
|
|
@@ -646,7 +631,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
@Override
|
|
|
public InputStream getFileStream(String path) throws IOException {
|
|
|
String absolutePath = this.absolutePath(path);
|
|
|
- File file = new File(absolutePath);
|
|
|
+ File file = FileUtils.getFile(absolutePath);
|
|
|
return Files.newInputStream(file.toPath());
|
|
|
}
|
|
|
|
|
|
@@ -700,13 +685,13 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
inputStream = getFileStream(pdf.getFileUrl());
|
|
|
if (ObjectUtil.isEmpty(inputStream)) {
|
|
|
log.error("登记簿导出失败,文件不存在,文件名:{}", pdf.getFileUrl());
|
|
|
- return null;
|
|
|
+ throw new RuntimeException("登记簿导出失败,文件不存在");
|
|
|
}
|
|
|
final String pdfFileName = pdf.getFileName();
|
|
|
//pdfFileName==null的时候在下面会报错此处加个判断,要处理问题还需要在问题源头除处理
|
|
|
// registerBookPdfBatchExportTempDir (Is a directory)
|
|
|
if (StringUtils.isEmpty(pdfFileName)) {
|
|
|
- return null;
|
|
|
+ throw new RuntimeException("登记簿导出失败,文件不存在");
|
|
|
}
|
|
|
final PdfToZipTempVo tempVo = new PdfToZipTempVo();
|
|
|
//tempVo.setBytes(bytes);
|
|
|
@@ -800,9 +785,13 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
.collect(Collectors.toList());
|
|
|
pdfToZipTempVoList.removeIf(pdfToZipTempVo -> !FileUtil.exist(pdfToZipTempVo.getFile()));
|
|
|
log.info("登记簿全部下载完成,开始压缩文件,数量:{}", pdfToZipTempVoList.size());
|
|
|
+
|
|
|
+
|
|
|
String encodedFileName = URLEncoder.encode(CacheConstants.REGISTER_PDF_FILE_KEY + DateHelper.getDateString(date) + str + ".zip", "UTF-8");
|
|
|
+
|
|
|
String filePath = this.localFilePath + File.separator + encodedFileName;
|
|
|
- FileOutputStream fos = new FileOutputStream(filePath);
|
|
|
+ File file = FileUtils.getFile(filePath);
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
ZipOutputStream zos = new ZipOutputStream(fos);
|
|
|
long fileSize = 0L;
|
|
|
for (PdfToZipTempVo tempVo : pdfToZipTempVoList) {
|
|
|
@@ -911,7 +900,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|
|
|
|
|
@Override
|
|
|
public void deletedZipFile() {
|
|
|
- final File dir = new File(this.localFilePath);
|
|
|
+ final File dir = FileUtils.getFile(this.localFilePath);
|
|
|
final File[] files = dir.listFiles();
|
|
|
if (ObjectUtil.isNull(files) || ObjectUtil.isEmpty(files)) {
|
|
|
return;
|