添加文件服务aop,优化代码生成工具
This commit is contained in:
@@ -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;
|
||||
|
||||
/**
|
||||
* <p>Title : StorageServiceAop</p>
|
||||
* <p>Description : StorageServiceAop</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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("文件大小超限");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user