clean code
This commit is contained in:
parent
1be0488bd6
commit
fd81961ddb
|
|
@ -4,7 +4,13 @@ import java.util.concurrent.*;
|
||||||
|
|
||||||
public final class ThreadUtil {
|
public final class ThreadUtil {
|
||||||
|
|
||||||
private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(1, 1, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
|
private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(1,
|
||||||
|
1,
|
||||||
|
3,
|
||||||
|
TimeUnit.SECONDS,
|
||||||
|
new LinkedBlockingDeque<>(100),
|
||||||
|
Executors.defaultThreadFactory(),
|
||||||
|
new ThreadPoolExecutor.DiscardOldestPolicy());
|
||||||
|
|
||||||
public static ExecutorService getThreadPool() {
|
public static ExecutorService getThreadPool() {
|
||||||
return THREAD_POOL;
|
return THREAD_POOL;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package com.electromagnetic.industry.software.manage.aop;
|
package com.electromagnetic.industry.software.manage.aop;
|
||||||
|
|
||||||
import com.electromagnetic.industry.software.common.annotations.RequiredPermission;
|
import com.electromagnetic.industry.software.common.annotations.RequiredPermission;
|
||||||
import com.electromagnetic.industry.software.common.cons.ElectromagneticConstants;
|
|
||||||
import com.electromagnetic.industry.software.common.enums.FilePermission;
|
import com.electromagnetic.industry.software.common.enums.FilePermission;
|
||||||
import com.electromagnetic.industry.software.common.exception.PermissionDeniedException;
|
import com.electromagnetic.industry.software.common.exception.PermissionDeniedException;
|
||||||
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||||
|
|
|
||||||
|
|
@ -136,52 +136,23 @@ public class LoginInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
long accessStartTime = (long) request.getSession().getAttribute("accessStartTime");
|
|
||||||
long accessEndTime = System.currentTimeMillis();
|
|
||||||
UserOperation userOperation = ((HandlerMethod) handler).getMethod().getAnnotation(UserOperation.class);
|
|
||||||
|
|
||||||
if (ObjectUtil.isEmpty(userOperation)) {
|
try {
|
||||||
return;
|
long accessStartTime = (long) request.getSession().getAttribute("accessStartTime");
|
||||||
}
|
long accessEndTime = System.currentTimeMillis();
|
||||||
|
UserOperation userOperation = ((HandlerMethod) handler).getMethod().getAnnotation(UserOperation.class);
|
||||||
|
|
||||||
String reqArgs = UserThreadLocal.getReqArgs();
|
if (ObjectUtil.isEmpty(userOperation)) {
|
||||||
|
return;
|
||||||
UserLoginInfo user = Optional.of(UserThreadLocal.getUser()).orElse(new UserLoginInfo());
|
|
||||||
List<AccessSuccessInfo> successInfoList = user.getSuccessInfoList();
|
|
||||||
|
|
||||||
if (CollUtil.isEmpty(successInfoList)) {
|
|
||||||
ElectromagneticResult res = user.getResult();
|
|
||||||
UserAccessLog userAccessLog = new UserAccessLog()
|
|
||||||
.setId(IdWorker.getSnowFlakeIdString())
|
|
||||||
.setUserId(user.getUserId())
|
|
||||||
.setAccessStartTime(DateUtil.date(accessStartTime))
|
|
||||||
.setAccessEndTime(DateUtil.date(accessEndTime))
|
|
||||||
.setAccessDuration(accessEndTime - accessStartTime)
|
|
||||||
.setAction(userOperation.value())
|
|
||||||
.setRequestUrl(request.getRequestURL().toString())
|
|
||||||
.setRequestIp(parseIpFromUrl(request.getRequestURL().toString()))
|
|
||||||
.setReqArgs(reqArgs)
|
|
||||||
.setRemoteAddr(getRealIp(request))
|
|
||||||
.setAccessSuccess(true)
|
|
||||||
.setCreateTime(new Date())
|
|
||||||
.setOperationModule(userOperation.modelName().key)
|
|
||||||
.setOperationMsg(res.getErrorMessage())
|
|
||||||
.setDataId("")
|
|
||||||
.setParentId("");
|
|
||||||
|
|
||||||
if (res != null) {
|
|
||||||
userAccessLog.setResponse(JSONUtil.toJsonStr(res));
|
|
||||||
if (!res.getSuccess()) {
|
|
||||||
userAccessLog.setAccessSuccess(false);
|
|
||||||
userAccessLog.setOperationMsg(res.getErrorMessage());
|
|
||||||
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
|
|
||||||
}
|
|
||||||
} else { // 返回为ResponseEntity,且状态为失败。
|
|
||||||
userAccessLog.setAccessSuccess(false);
|
|
||||||
}
|
}
|
||||||
userAccessLogMapper.insert(userAccessLog);
|
|
||||||
} else {
|
String reqArgs = UserThreadLocal.getReqArgs();
|
||||||
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {
|
|
||||||
|
UserLoginInfo user = Optional.of(UserThreadLocal.getUser()).orElse(new UserLoginInfo());
|
||||||
|
List<AccessSuccessInfo> successInfoList = user.getSuccessInfoList();
|
||||||
|
|
||||||
|
if (CollUtil.isEmpty(successInfoList)) {
|
||||||
|
ElectromagneticResult res = user.getResult();
|
||||||
UserAccessLog userAccessLog = new UserAccessLog()
|
UserAccessLog userAccessLog = new UserAccessLog()
|
||||||
.setId(IdWorker.getSnowFlakeIdString())
|
.setId(IdWorker.getSnowFlakeIdString())
|
||||||
.setUserId(user.getUserId())
|
.setUserId(user.getUserId())
|
||||||
|
|
@ -196,26 +167,60 @@ public class LoginInterceptor implements HandlerInterceptor {
|
||||||
.setAccessSuccess(true)
|
.setAccessSuccess(true)
|
||||||
.setCreateTime(new Date())
|
.setCreateTime(new Date())
|
||||||
.setOperationModule(userOperation.modelName().key)
|
.setOperationModule(userOperation.modelName().key)
|
||||||
.setOperationMsg(accessSuccessInfo.getSuccessMsg())
|
.setOperationMsg(res.getErrorMessage())
|
||||||
.setDataId(accessSuccessInfo.getDataId())
|
.setDataId("")
|
||||||
.setParentId(accessSuccessInfo.getParentId());
|
.setParentId("");
|
||||||
|
|
||||||
ElectromagneticResult<?> result = user.getResult();
|
if (res != null) {
|
||||||
if (result != null) {
|
userAccessLog.setResponse(JSONUtil.toJsonStr(res));
|
||||||
userAccessLog.setResponse(JSONUtil.toJsonStr(result));
|
if (!res.getSuccess()) {
|
||||||
if (!result.getSuccess()) {
|
|
||||||
userAccessLog.setAccessSuccess(false);
|
userAccessLog.setAccessSuccess(false);
|
||||||
userAccessLog.setOperationMsg(result.getErrorMessage());
|
userAccessLog.setOperationMsg(res.getErrorMessage());
|
||||||
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
|
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
|
||||||
}
|
}
|
||||||
} else { // 返回为ResponseEntity,且状态为失败。
|
} else { // 返回为ResponseEntity,且状态为失败。
|
||||||
userAccessLog.setAccessSuccess(false);
|
userAccessLog.setAccessSuccess(false);
|
||||||
}
|
}
|
||||||
userAccessLogMapper.insert(userAccessLog);
|
userAccessLogMapper.insert(userAccessLog);
|
||||||
}
|
} else {
|
||||||
}
|
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {
|
||||||
|
UserAccessLog userAccessLog = new UserAccessLog()
|
||||||
|
.setId(IdWorker.getSnowFlakeIdString())
|
||||||
|
.setUserId(user.getUserId())
|
||||||
|
.setAccessStartTime(DateUtil.date(accessStartTime))
|
||||||
|
.setAccessEndTime(DateUtil.date(accessEndTime))
|
||||||
|
.setAccessDuration(accessEndTime - accessStartTime)
|
||||||
|
.setAction(userOperation.value())
|
||||||
|
.setRequestUrl(request.getRequestURL().toString())
|
||||||
|
.setRequestIp(parseIpFromUrl(request.getRequestURL().toString()))
|
||||||
|
.setReqArgs(reqArgs)
|
||||||
|
.setRemoteAddr(getRealIp(request))
|
||||||
|
.setAccessSuccess(true)
|
||||||
|
.setCreateTime(new Date())
|
||||||
|
.setOperationModule(userOperation.modelName().key)
|
||||||
|
.setOperationMsg(accessSuccessInfo.getSuccessMsg())
|
||||||
|
.setDataId(accessSuccessInfo.getDataId())
|
||||||
|
.setParentId(accessSuccessInfo.getParentId());
|
||||||
|
|
||||||
UserThreadLocal.remove();
|
ElectromagneticResult<?> result = user.getResult();
|
||||||
|
if (result != null) {
|
||||||
|
userAccessLog.setResponse(JSONUtil.toJsonStr(result));
|
||||||
|
if (!result.getSuccess()) {
|
||||||
|
userAccessLog.setAccessSuccess(false);
|
||||||
|
userAccessLog.setOperationMsg(result.getErrorMessage());
|
||||||
|
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
|
||||||
|
}
|
||||||
|
} else { // 返回为ResponseEntity,且状态为失败。
|
||||||
|
userAccessLog.setAccessSuccess(false);
|
||||||
|
}
|
||||||
|
userAccessLogMapper.insert(userAccessLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("请求后置异常,原因 {}", e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
UserThreadLocal.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isTokenValid(String tokenStr) {
|
public Boolean isTokenValid(String tokenStr) {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.text.StrFormatter;
|
import cn.hutool.core.text.StrFormatter;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.crypto.digest.DigestUtil;
|
import cn.hutool.crypto.digest.DigestUtil;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
|
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
|
||||||
|
|
@ -112,9 +113,14 @@ public class ChatService {
|
||||||
documents = new TokenTextSplitter().apply(documentReader.get());
|
documents = new TokenTextSplitter().apply(documentReader.get());
|
||||||
Files.deleteIfExists(tempFile);
|
Files.deleteIfExists(tempFile);
|
||||||
}
|
}
|
||||||
case "xls" -> {
|
case "xls", "xlsx" -> {
|
||||||
String filePath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".xls";
|
String filePath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + "." + fileType;
|
||||||
List<PageFile> pageInfo = OfficeFileUtil.parseXlsByPage(filePath);
|
List<PageFile> pageInfo;
|
||||||
|
if (StrUtil.equals("xlsx", filePath)) {
|
||||||
|
pageInfo = OfficeFileUtil.parseXlsxByPage(filePath);
|
||||||
|
} else {
|
||||||
|
pageInfo = OfficeFileUtil.parseXlsByPage(filePath);
|
||||||
|
}
|
||||||
for (PageFile pageFile : pageInfo) {
|
for (PageFile pageFile : pageInfo) {
|
||||||
Map<String, Object> metaData = Map.of("page_number", pageFile.getPageNumber(), "file_name", file.getOriginalFilename());
|
Map<String, Object> metaData = Map.of("page_number", pageFile.getPageNumber(), "file_name", file.getOriginalFilename());
|
||||||
Document document = new Document(pageFile.getContent(), metaData);
|
Document document = new Document(pageFile.getContent(), metaData);
|
||||||
|
|
@ -122,20 +128,14 @@ public class ChatService {
|
||||||
}
|
}
|
||||||
FileUtil.del(filePath);
|
FileUtil.del(filePath);
|
||||||
}
|
}
|
||||||
case "xlsx" -> {
|
case "doc", "docx", "ppt", "pptx" -> {
|
||||||
String filePath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".xlsx";
|
|
||||||
List<PageFile> pageInfo = OfficeFileUtil.parseXlsxByPage(filePath);
|
|
||||||
for (PageFile pageFile : pageInfo) {
|
|
||||||
Map<String, Object> metaData = Map.of("page_number", pageFile.getPageNumber(), "file_name", file.getOriginalFilename());
|
|
||||||
Document document = new Document(pageFile.getContent(), metaData);
|
|
||||||
documents.add(document);
|
|
||||||
}
|
|
||||||
FileUtil.del(filePath);
|
|
||||||
}
|
|
||||||
case "doc" -> {
|
|
||||||
String wordPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".doc";
|
|
||||||
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
|
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
|
||||||
OfficeFileUtil.doc2pdf(wordPath, pdfPath);
|
String srcPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + "." + fileType;
|
||||||
|
if (StrUtil.equals(fileType, "doc") || StrUtil.equals(fileType, "docx")) {
|
||||||
|
OfficeFileUtil.doc2pdf(srcPath, pdfPath);
|
||||||
|
} else {
|
||||||
|
OfficeFileUtil.ppt2pdf(srcPath, pdfPath);
|
||||||
|
}
|
||||||
Path path = new File(pdfPath).toPath();
|
Path path = new File(pdfPath).toPath();
|
||||||
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
|
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
|
||||||
List<Document> tmp = reader.get();
|
List<Document> tmp = reader.get();
|
||||||
|
|
@ -146,55 +146,7 @@ public class ChatService {
|
||||||
documents.add(doc);
|
documents.add(doc);
|
||||||
}
|
}
|
||||||
Files.deleteIfExists(path);
|
Files.deleteIfExists(path);
|
||||||
FileUtil.del(wordPath);
|
FileUtil.del(srcPath);
|
||||||
}
|
|
||||||
case "docx" -> {
|
|
||||||
String wordPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".docx";
|
|
||||||
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
|
|
||||||
OfficeFileUtil.doc2pdf(wordPath, pdfPath);
|
|
||||||
Path path = new File(pdfPath).toPath();
|
|
||||||
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
|
|
||||||
List<Document> tmp = reader.get();
|
|
||||||
for (Document document : tmp) {
|
|
||||||
Map<String, Object> metadata = document.getMetadata();
|
|
||||||
Map<String, Object> metaData = Map.of("page_number", metadata.get("page_number"), "file_name", file.getOriginalFilename());
|
|
||||||
Document doc = new Document(Objects.requireNonNull(document.getText()), metaData);
|
|
||||||
documents.add(doc);
|
|
||||||
}
|
|
||||||
Files.deleteIfExists(path);
|
|
||||||
FileUtil.del(wordPath);
|
|
||||||
}
|
|
||||||
case "ppt" -> {
|
|
||||||
String pptPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".ppt";
|
|
||||||
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
|
|
||||||
OfficeFileUtil.ppt2pdf(pptPath, pdfPath);
|
|
||||||
Path path = new File(pdfPath).toPath();
|
|
||||||
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
|
|
||||||
List<Document> tmp = reader.get();
|
|
||||||
for (Document document : tmp) {
|
|
||||||
Map<String, Object> metadata = document.getMetadata();
|
|
||||||
Map<String, Object> metaData = Map.of("page_number", metadata.get("page_number"), "file_name", file.getOriginalFilename());
|
|
||||||
Document doc = new Document(Objects.requireNonNull(document.getText()), metaData);
|
|
||||||
documents.add(doc);
|
|
||||||
}
|
|
||||||
Files.deleteIfExists(path);
|
|
||||||
FileUtil.del(pptPath);
|
|
||||||
}
|
|
||||||
case "pptx" -> {
|
|
||||||
String pptxPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pptx";
|
|
||||||
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
|
|
||||||
OfficeFileUtil.ppt2pdf(pptxPath, pdfPath);
|
|
||||||
Path path = new File(pdfPath).toPath();
|
|
||||||
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
|
|
||||||
List<Document> tmp = reader.get();
|
|
||||||
for (Document document : tmp) {
|
|
||||||
Map<String, Object> metadata = document.getMetadata();
|
|
||||||
Map<String, Object> metaData = Map.of("page_number", metadata.get("page_number"), "file_name", file.getOriginalFilename());
|
|
||||||
Document doc = new Document(Objects.requireNonNull(document.getText()), metaData);
|
|
||||||
documents.add(doc);
|
|
||||||
}
|
|
||||||
Files.deleteIfExists(path);
|
|
||||||
FileUtil.del(pptxPath);
|
|
||||||
}
|
}
|
||||||
case "pdf" -> {
|
case "pdf" -> {
|
||||||
Path tempFile = saveUploadedFileToTemp(file);
|
Path tempFile = saveUploadedFileToTemp(file);
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@ import com.electromagnetic.industry.software.manage.service.FileFormatService;
|
||||||
import com.electromagnetic.industry.software.manage.service.FileSystemService;
|
import com.electromagnetic.industry.software.manage.service.FileSystemService;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ import com.electromagnetic.industry.software.manage.pojo.resp.ProjectVO;
|
||||||
import com.electromagnetic.industry.software.manage.service.EdPrjService;
|
import com.electromagnetic.industry.software.manage.service.EdPrjService;
|
||||||
import com.electromagnetic.industry.software.manage.service.FileSystemService;
|
import com.electromagnetic.industry.software.manage.service.FileSystemService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,6 @@ import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue