From 0de0d3a13412bd3dedf63856a68f3f5eb43b035c Mon Sep 17 00:00:00 2001 From: xujun Date: Tue, 19 May 2026 09:37:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1aop=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/gen/service/impl/GenServiceImpl.java | 24 ++--- .../app/sys/file/aop/StorageServiceAop.java | 97 +++++++++++++++++++ .../service/impl/SysFileOptServiceImpl.java | 49 ++++++---- 3 files changed, 139 insertions(+), 31 deletions(-) create mode 100644 xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/aop/StorageServiceAop.java diff --git a/xtools-app-gen/xtools-app-gen-biz/src/main/java/xtools/app/gen/service/impl/GenServiceImpl.java b/xtools-app-gen/xtools-app-gen-biz/src/main/java/xtools/app/gen/service/impl/GenServiceImpl.java index d71d579..6878159 100644 --- a/xtools-app-gen/xtools-app-gen-biz/src/main/java/xtools/app/gen/service/impl/GenServiceImpl.java +++ b/xtools-app-gen/xtools-app-gen-biz/src/main/java/xtools/app/gen/service/impl/GenServiceImpl.java @@ -333,18 +333,18 @@ public class GenServiceImpl implements GenService, BaseParams { String subModuleName = datasource.getSubModuleName(); String tableName = table.getTableName(); // 获取uri - int indexOf = tableName.indexOf("_"); + int indexOf = tableName.indexOf(CP_UNDERLINE); String uri; if (indexOf > CP_NEGATIVE1) { - uri = tableName.substring(CP_NUM0, indexOf) + CP_SLASH + tableName.substring(indexOf + CP_NUM1).replaceAll("_", CP_LINE); + uri = tableName.substring(CP_NUM0, indexOf) + CP_SLASH + tableName.substring(indexOf + CP_NUM1).replace(CP_UNDERLINE, CP_LINE); } else { uri = tableName; } // 获取按钮权限前缀 - String btnPermPrefix = uri.replaceAll(CP_SLASH, CP_COLON); + String btnPermPrefix = uri.replace(CP_SLASH, CP_COLON); - // 获取dbid - List dbIdList = getDbIdList(CP_NUM20); + // 获取dbId + List dbIdList = getDbIdList(); // 获取父级菜单ID和树路径 String parentMenuId = table.getParentMenuId(); String treePath; @@ -356,7 +356,7 @@ public class GenServiceImpl implements GenService, BaseParams { parentId = "parentId"; treePath = "treePath"; } - String component = subModuleName + CP_SLASH + tableName.replace("_", "-") + CP_SLASH + "index"; + String component = subModuleName + CP_SLASH + tableName.replace(CP_UNDERLINE, CP_LINE) + CP_SLASH + "index"; // 根据配置文件生成模板 Map templateConfigs = genProperties.getTemplateConfigs(); @@ -429,7 +429,7 @@ public class GenServiceImpl implements GenService, BaseParams { map.put("subPackageName", value.getSubPackageName()); map.put("uri", uri); map.put("btnPermPrefix", btnPermPrefix); - map.put("apiPath", tableName.replace("_", "-") + "-api"); + map.put("apiPath", tableName.replace(CP_UNDERLINE, CP_LINE) + "-api"); map.put("dbIds", dbIdList); map.put("parentId", parentId); @@ -455,10 +455,10 @@ public class GenServiceImpl implements GenService, BaseParams { /** * 获取数据库 ID 列表 * - * @param count 获取个数 * @return 数据库 ID 列表 */ - private List getDbIdList(int count) { + private List getDbIdList() { + int count = CP_NUM20; InetAddress localHost; try { localHost = InetAddress.getLocalHost(); @@ -487,7 +487,7 @@ public class GenServiceImpl implements GenService, BaseParams { return switch (type) { case "Entity", "Sql" -> entityName + extension; - case "TS_API" -> tableName.replace("_", "-") + "-api" + extension; + case "TS_API" -> tableName.replace(CP_UNDERLINE, CP_LINE) + "-api" + extension; case "VUE_VIEW" -> "index" + extension; default -> entityName + type + extension; }; @@ -511,7 +511,7 @@ public class GenServiceImpl implements GenService, BaseParams { case ".java" -> { joiner.add(genProperties.getBackendAppName()); joiner.add(moduleName); - joiner.add(moduleName + "-" + projectModule); + joiner.add(moduleName + CP_LINE + projectModule); joiner.add("src"); joiner.add("main"); joiner.add("java"); @@ -529,7 +529,7 @@ public class GenServiceImpl implements GenService, BaseParams { joiner.add("src"); joiner.add(subPackageName.replace(CP_DOT, CP_SLASH)); joiner.add(subModuleName); - joiner.add(tableName.replace("_", "-")); + joiner.add(tableName.replace(CP_UNDERLINE, CP_LINE)); } case ".sql" -> joiner.add("sql"); default -> joiner.add(CP_EMPTY); diff --git a/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/aop/StorageServiceAop.java b/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/aop/StorageServiceAop.java new file mode 100644 index 0000000..dc6eaa4 --- /dev/null +++ b/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/aop/StorageServiceAop.java @@ -0,0 +1,97 @@ +package xtools.app.sys.file.aop; + +import com.alibaba.fastjson2.JSONObject; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; +import xtools.base.config.BaseParams; +import xtools.boot.api.model.dto.log.LogTrack; +import xtools.boot.log.LogBus; +import xtools.boot.log.enums.LogBusBaseType; +import xtools.boot.log.holder.LogTrackHolder; +import xtools.core.enums.LogLevel; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Objects; + +/** + *

Title : StorageServiceAop

+ *

Description : StorageServiceAop

+ *

DevelopTools : Idea_x64_v2026.1

+ *

DevelopSystem : macOS Sequoia 15.7.5

+ *

Company : org.xujun

+ * + * @author : XuJun + * @version : 1.0.0 + * @date : 2026/2/11 14:07 + */ +@Aspect +@Component +public class StorageServiceAop implements BaseParams { + + /** + * 方法切面 + */ + @Pointcut("execution(* xtools.boot.storage.base.service.StorageService.*(..))") + public void methods() { + } + + /** + * 调用记录 + * + * @param joinPoint 切点 + * @return 调用结果 + * @throws Throwable 异常信息 + */ + @Around("methods()") + public Object logMethodCall(ProceedingJoinPoint joinPoint) throws Throwable { + LogTrack logTrack = LogTrackHolder.getDefNull(); + if (Objects.isNull(logTrack)) { + return joinPoint.proceed(); + } + long startTime = System.currentTimeMillis(); + String method = joinPoint.getSignature().getName(); + Object[] args = joinPoint.getArgs(); + Exception err = null; + Object result = null; + try { + result = joinPoint.proceed(); + return result; + } catch (Exception e) { + err = e; + throw e; + } finally { + long endTime = System.currentTimeMillis(); + JSONObject logData = JSONObject.of("method", method, "args", filterStreamArgs(args), "result", result, "execTime", endTime - startTime); + LogBus.init(err == null ? LogLevel.INFO : LogLevel.ERROR, LogBusBaseType.STORAGE, logTrack) + .data(logData) + .error(err) + .save(); + } + } + + /** + * 过滤掉 InputStream 和 OutputStream 参数 + * + * @param args 参数 + * @return 过滤后的参数 + */ + private Object[] filterStreamArgs(Object[] args) { + if (args == null || args.length == 0) { + return args; + } + Object[] filtered = new Object[args.length]; + for (int i = CP_NUM0; i < args.length; i++) { + if (args[i] instanceof InputStream || args[i] instanceof OutputStream) { + filtered[i] = "<" + args[i].getClass().getSimpleName() + ">"; + } else { + filtered[i] = args[i]; + } + } + return filtered; + } + +} diff --git a/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/service/impl/SysFileOptServiceImpl.java b/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/service/impl/SysFileOptServiceImpl.java index e3e6ba7..2c071b1 100644 --- a/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/service/impl/SysFileOptServiceImpl.java +++ b/xtools-app-sys/xtools-app-sys-file/src/main/java/xtools/app/sys/file/service/impl/SysFileOptServiceImpl.java @@ -119,25 +119,10 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams { ) { // 上传时间 Instant uploadTime = Instant.now(); - - // 文件名校验 - if (StringUtils.isBlank(fileName)) { - throw new BizError("文件名称不能为空"); - } - // 文件格式校验 + // 获取文件扩展名 String suffix = FileUtils.getSuffix(fileName); - String[] extArr = bizType.extArr(); - if (ArrUtils.isNotEmpty(extArr)) { - if (!CheckUtils.suffix(suffix, extArr)) { - throw new BizError("文件格式错误"); - } - } - // 文件大小限制 - long sizeLimit = bizType.sizeLimit(); - if (sizeLimit > CP_NEGATIVE1 && contentLength > sizeLimit) { - throw new BizError("文件大小超限"); - } - + // 检查文件 + checkFile(fileName, contentLength, suffix, bizType); // 计算文件码 String md5 = null; String sm3 = null; @@ -163,7 +148,6 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams { throw new BizError("计算文件码失败"); } } - // 文件存储器的文件名 String filePath = UuidUtils.get() + CP_LINE + fileName; // 获取存储桶 @@ -203,6 +187,33 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams { throw new BizError("文件上传失败"); } + /** + * 校验文件 + * + * @param fileName 文件名 + * @param contentLength 文件长度 + * @param suffix 文件扩展名 + * @param bizType 业务类型 + */ + private void checkFile(String fileName, long contentLength, String suffix, FileBizBaseEnum bizType) { + // 文件名校验 + if (StringUtils.isBlank(fileName)) { + throw new BizError("文件名称不能为空"); + } + // 文件扩展名校验 + String[] extArr = bizType.extArr(); + if (ArrUtils.isNotEmpty(extArr)) { + if (!CheckUtils.suffix(suffix, extArr)) { + throw new BizError("文件格式错误"); + } + } + // 文件长度校验 + long sizeLimit = bizType.sizeLimit(); + if (sizeLimit > CP_NEGATIVE1 && contentLength > sizeLimit) { + throw new BizError("文件大小超限"); + } + } + /** * 文件下载 *