Compare commits

..

4 Commits

Author SHA1 Message Date
chenxudong 95a82bb69c 删除上传的工程文件。 2025-05-15 14:21:35 +08:00
chenxudong c6dcd99afb clean code 2025-05-15 13:47:38 +08:00
chenxudong 82ef0063f7 优化代码 2025-05-15 12:11:02 +08:00
chenxudong ce50a07e93 优化代码 2025-05-15 10:52:08 +08:00
6 changed files with 82 additions and 211 deletions

View File

@ -18,7 +18,6 @@ import com.electromagnetic.industry.software.common.enums.*;
import com.electromagnetic.industry.software.common.exception.BizException; import com.electromagnetic.industry.software.common.exception.BizException;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult; import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.EleCommonUtil; import com.electromagnetic.industry.software.common.util.EleCommonUtil;
import com.electromagnetic.industry.software.common.util.EleLog;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil; import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal; import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.config.ElePropertyConfig; import com.electromagnetic.industry.software.manage.config.ElePropertyConfig;
@ -31,6 +30,7 @@ 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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -43,12 +43,12 @@ import java.util.stream.Collectors;
import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.*; import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.*;
import static com.electromagnetic.industry.software.common.enums.FileRepeatEnum.*; import static com.electromagnetic.industry.software.common.enums.FileRepeatEnum.*;
@Slf4j
@Component @Component
public class CommonService { public class CommonService {
private static final Map<Integer, String> PATH_MAP = new HashMap<>(); private static final Map<Integer, String> PATH_MAP = new HashMap<>();
private final EleLog log = new EleLog(CommonService.class);
@Resource @Resource
private ElePropertyConfig elePropertyConfig; private ElePropertyConfig elePropertyConfig;

View File

@ -7,10 +7,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrFormatter; import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.*;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
@ -42,6 +39,7 @@ import com.electromagnetic.industry.software.manage.pojo.resp.UploadRecordVO;
import com.electromagnetic.industry.software.manage.service.*; import com.electromagnetic.industry.software.manage.service.*;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -64,10 +62,10 @@ import static com.electromagnetic.industry.software.common.cons.ElectromagneticC
import static com.electromagnetic.industry.software.common.enums.DataOwnEnum.USER_FILE; import static com.electromagnetic.industry.software.common.enums.DataOwnEnum.USER_FILE;
import static com.electromagnetic.industry.software.common.enums.FileRepeatEnum.*; import static com.electromagnetic.industry.software.common.enums.FileRepeatEnum.*;
@Slf4j
@Service @Service
public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo> implements EdFileInfoService { public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo> implements EdFileInfoService {
private final EleLog log = new EleLog(EdFileInfoServiceImpl.class);
@Resource @Resource
private CommonService commonService; private CommonService commonService;
@Resource @Resource
@ -1312,11 +1310,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} }
String mimeType = Files.probeContentType(Paths.get(fileSaveTmpPath)); String mimeType = Files.probeContentType(Paths.get(fileSaveTmpPath));
if (mimeType == null) { mimeType = EleCommonUtil.simpleTrueFalse(ObjectUtil.isNull(mimeType), "application/octet-stream", mimeType);
// 如果无法探测到MIME类型则使用默认值 application/octet-stream
mimeType = "application/octet-stream";
}
FileSystemResource fileSystemResource = new FileSystemResource(fileSaveTmpPath); FileSystemResource fileSystemResource = new FileSystemResource(fileSaveTmpPath);
String fileName = fileSystemResource.getFilename(); String fileName = fileSystemResource.getFilename();
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -1571,12 +1565,15 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} }
private void updateImportPrj2FileSystem(MultipartFile file, String prjId) throws IOException { private void updateImportPrj2FileSystem(MultipartFile file, String prjId) throws IOException {
String tmpZipFile = null;
String destDir = null;
try {
String orgName = file.getOriginalFilename(); String orgName = file.getOriginalFilename();
String tmpZipFile = elePropertyConfig.getEleTmpPath() + File.separator + orgName; tmpZipFile = elePropertyConfig.getEleTmpPath() + File.separator + orgName;
String mainName = FileUtil.mainName(orgName); String mainName = FileUtil.mainName(orgName);
FileUtil.del(tmpZipFile); FileUtil.del(tmpZipFile);
FileUtil.writeFromStream(file.getInputStream(), tmpZipFile); FileUtil.writeFromStream(file.getInputStream(), tmpZipFile);
String destDir = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID(); destDir = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID();
try { try {
ZipUtil.unzip(tmpZipFile, destDir, StandardCharsets.UTF_8); ZipUtil.unzip(tmpZipFile, destDir, StandardCharsets.UTF_8);
} catch (Exception e) { } catch (Exception e) {
@ -1635,6 +1632,13 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
EleCommonUtil.encryptFile(destPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes())); EleCommonUtil.encryptFile(destPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
UserThreadLocal.setSuccessInfo(newEdFileInfo.getParentId(), newEdFileInfo.getId(), "解析导入的工程文件成功,导入的工程名为 {},文件名为 {}", orgName, importFile.getName()); UserThreadLocal.setSuccessInfo(newEdFileInfo.getParentId(), newEdFileInfo.getId(), "解析导入的工程文件成功,导入的工程名为 {},文件名为 {}", orgName, importFile.getName());
} }
} catch (Exception e) {
String info = "上传失败,原因 " + e.getMessage();
throw new BizException(info, e);
} finally {
FileUtil.del(destDir);
FileUtil.del(tmpZipFile);
}
} }
private String updateImportPrj2Db(MultipartFile file) { private String updateImportPrj2Db(MultipartFile file) {

View File

@ -13,7 +13,6 @@ import com.electromagnetic.industry.software.common.enums.*;
import com.electromagnetic.industry.software.common.exception.BizException; import com.electromagnetic.industry.software.common.exception.BizException;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult; import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.EleCommonUtil; import com.electromagnetic.industry.software.common.util.EleCommonUtil;
import com.electromagnetic.industry.software.common.util.EleLog;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil; import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal; import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.config.ElePropertyConfig; import com.electromagnetic.industry.software.manage.config.ElePropertyConfig;
@ -25,6 +24,7 @@ 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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -33,11 +33,10 @@ import java.util.stream.Collectors;
import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.*; import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.*;
@Slf4j
@Service @Service
public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo> implements EdPrjService { public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo> implements EdPrjService {
private final EleLog log = new EleLog(EdPrjServiceImpl.class);
@Resource @Resource
private FileSystemService fileSystemService; private FileSystemService fileSystemService;

View File

@ -6,10 +6,8 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*; import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security; import java.security.Security;
/** /**
@ -21,56 +19,6 @@ import java.security.Security;
*/ */
public class AESUtils { public class AESUtils {
/**
* <p>Discription:[加密]</p>
* Created on 2022/07/06 10:52
*
* @param content 明文 用JSON.toJSONString(Map<String, String> map)转换的json字符串
* @param key 加解密规则 访客系统提供key
* @return String 密文
*/
public static String ecodes(String content, String key) {
if (content == null || content.length() < 1) {
return null;
}
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] byteRresult = cipher.doFinal(byteContent);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteRresult.length; i++) {
String hex = Integer.toHexString(byteRresult[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/** /**
* 前端使用ECB后端解密方法 * 前端使用ECB后端解密方法
* *

View File

@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
import com.electromagnetic.industry.software.common.exception.BizException; import com.electromagnetic.industry.software.common.exception.BizException;
import com.electromagnetic.industry.software.common.parse.*; import com.electromagnetic.industry.software.common.parse.*;
import lombok.extern.slf4j.Slf4j;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
@ -17,6 +18,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@Slf4j
public final class EleCommonUtil { public final class EleCommonUtil {
private static final Map<String, FileParse> PARSE_MAP = new HashMap<>(); private static final Map<String, FileParse> PARSE_MAP = new HashMap<>();
@ -24,8 +26,7 @@ public final class EleCommonUtil {
private static final String PATTERN = "^[\\u4e00-\\u9fa5a-zA-Z0-9._\\-+]+$"; private static final String PATTERN = "^[\\u4e00-\\u9fa5a-zA-Z0-9._\\-+]+$";
private static final String TIME_FORMAT1 = "yyMMddHHmmssSSS"; private static final String TIME_FORMAT1 = "yyMMddHHmmssSSS";
// 编译正则表达式 // 编译正则表达式
private static final Pattern pattern = Pattern.compile(PATTERN); private static final Pattern NAME_PATTERN = Pattern.compile(PATTERN);
private static final EleLog log = new EleLog(EleCommonUtil.class);
static { static {
PARSE_MAP.put("doc", new WordParse()); PARSE_MAP.put("doc", new WordParse());
@ -51,7 +52,7 @@ public final class EleCommonUtil {
if (StrUtil.isEmpty(fileFullName) || fileFullName.length() > 32) { if (StrUtil.isEmpty(fileFullName) || fileFullName.length() > 32) {
return false; return false;
} }
return pattern.matcher(fileFullName).matches(); return NAME_PATTERN.matcher(fileFullName).matches();
} }
public static String getNowTimeStr() { public static String getNowTimeStr() {

View File

@ -1,81 +0,0 @@
package com.electromagnetic.industry.software.common.util;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EleLog {
private static final String PLACE_HOLDER = "{}";
private Logger logger;
public EleLog(Class clazz) {
this.logger = LoggerFactory.getLogger(clazz);
}
private static String format(String strPattern, Object... argArray) {
if (!StrUtil.isBlank(strPattern) && !StrUtil.isBlank(PLACE_HOLDER) && !ArrayUtil.isEmpty(argArray)) {
int strPatternLength = strPattern.length();
int placeHolderLength = PLACE_HOLDER.length();
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
int handledPosition = 0;
for (int argIndex = 0; argIndex < argArray.length; ++argIndex) {
int delimIndex = strPattern.indexOf(PLACE_HOLDER, handledPosition);
if (delimIndex == -1) {
if (handledPosition == 0) {
return strPattern;
}
sbuf.append(strPattern, handledPosition, strPatternLength);
return sbuf.toString();
}
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == '\\') {
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == '\\') {
sbuf.append(strPattern, handledPosition, delimIndex - 1);
sbuf.append(StrUtil.utf8Str(argArray[argIndex]));
handledPosition = delimIndex + placeHolderLength;
} else {
--argIndex;
sbuf.append(strPattern, handledPosition, delimIndex - 1);
sbuf.append(PLACE_HOLDER.charAt(0));
handledPosition = delimIndex + 1;
}
} else {
sbuf.append(strPattern, handledPosition, delimIndex);
sbuf.append(StrUtil.utf8Str(argArray[argIndex]));
handledPosition = delimIndex + placeHolderLength;
}
}
sbuf.append(strPattern, handledPosition, strPatternLength);
return sbuf.toString();
} else {
return strPattern;
}
}
public void info(String msg, Object... args) {
logger.info(format(msg, args));
}
public void debug(String msg, Object... args) {
logger.debug(format(msg, args));
}
public void warn(String msg, Object... args) {
logger.warn(format(msg, args));
}
public void error(String msg, Object... args) {
logger.error(format(msg, args));
}
public void error(String msg, Throwable e, Object... args) {
String info = format(msg, args);
logger.error(info, e);
}
}