调了上传文件记录的一个demo
This commit is contained in:
parent
5e9caede22
commit
46d798a019
|
|
@ -1,9 +1,11 @@
|
|||
package com.electromagnetic.industry.software.manage.common;
|
||||
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import com.electromagnetic.industry.software.common.exception.BizException;
|
||||
import com.electromagnetic.industry.software.common.exception.PermissionDeniedException;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
|
|
@ -22,6 +24,8 @@ public class GlobalExceptionHandler {
|
|||
@ResponseBody
|
||||
public ElectromagneticResult<?> runTimeError(Throwable e) {
|
||||
log.error(e.getMessage(), e);
|
||||
UserThreadLocal.setExceptionDetail(ExceptionUtil.stacktraceToOneLineString(e));
|
||||
UserThreadLocal.setRes(new ElectromagneticResult<>(false, "-1", e.getMessage(), ""));
|
||||
return ElectromagneticResultUtil.fail("-1", e.getMessage());
|
||||
}
|
||||
|
||||
|
|
@ -30,6 +34,8 @@ public class GlobalExceptionHandler {
|
|||
@ResponseBody
|
||||
public ElectromagneticResult<?> bizError(BizException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
UserThreadLocal.setExceptionDetail(ExceptionUtil.stacktraceToOneLineString(e));
|
||||
UserThreadLocal.setRes(new ElectromagneticResult<>(false, "-1", e.getMessage(), ""));
|
||||
return ElectromagneticResultUtil.fail("-1", e.getMessage());
|
||||
}
|
||||
|
||||
|
|
@ -38,6 +44,18 @@ public class GlobalExceptionHandler {
|
|||
@ResponseBody
|
||||
public ElectromagneticResult<?> accessDeniedError(PermissionDeniedException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
UserThreadLocal.setExceptionDetail(ExceptionUtil.stacktraceToOneLineString(e));
|
||||
UserThreadLocal.setRes(new ElectromagneticResult<>(false, "-1", e.getMessage(), ""));
|
||||
return ElectromagneticResultUtil.fail("-1", e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(Throwable.class)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ResponseBody
|
||||
public ElectromagneticResult<?> unknown(Throwable e) {
|
||||
log.error(e.getMessage(), e);
|
||||
UserThreadLocal.setExceptionDetail(ExceptionUtil.stacktraceToOneLineString(e));
|
||||
UserThreadLocal.setRes(new ElectromagneticResult<>(false, "-1", e.getMessage(), ""));
|
||||
return ElectromagneticResultUtil.fail("-1", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.electromagnetic.industry.software.manage.config;
|
|||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.SystemClock;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.electromagnetic.industry.software.common.annotations.UserOperation;
|
||||
import com.electromagnetic.industry.software.common.cons.UserConstants;
|
||||
import com.electromagnetic.industry.software.common.enums.AdminTypeEnum;
|
||||
|
|
@ -121,13 +122,15 @@ public class LoginInterceptor implements HandlerInterceptor {
|
|||
.accessSuccess(true)
|
||||
.createTime(new Date())
|
||||
.operationModule(userOperation.modelName().key)
|
||||
.operationMsg(UserThreadLocal.getUser().getSuccessMsg())
|
||||
.build();
|
||||
|
||||
ElectromagneticResult<?> result = UserThreadLocal.getResult();
|
||||
if (result != null) {
|
||||
if (!result.getSuccess()) {
|
||||
userAccessLog.setAccessSuccess(false);
|
||||
userAccessLog.setFailureReason(result.getErrorMessage());
|
||||
userAccessLog.setOperationMsg(result.getErrorMessage());
|
||||
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
|
||||
}
|
||||
} else { // 返回为ResponseEntity,且状态为失败。
|
||||
userAccessLog.setAccessSuccess(false);
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public class EdFileInfoController {
|
|||
@UserOperation(value = "查看了工程树", modelName = UserOperationModuleEnum.DATABASE)
|
||||
@RequestMapping("tree")
|
||||
public ElectromagneticResult<?> tree() {
|
||||
return edFileInfoService.tree(DataOwnEnum.COMMON.code);
|
||||
return edFileInfoService.tree(DataOwnEnum.SYS_PRJ.code);
|
||||
}
|
||||
|
||||
@UserOperation(value = "创建了文件夹", modelName = UserOperationModuleEnum.DATABASE)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ public class ProjectController {
|
|||
@Resource
|
||||
private EdPrjService edPrjService;
|
||||
|
||||
@UserOperation(value = "创建了工程", modelName = UserOperationModuleEnum.PRJ_SETTING)
|
||||
@UserOperation(value = "创建工程", modelName = UserOperationModuleEnum.PRJ_SETTING)
|
||||
@RequestMapping("create")
|
||||
public ElectromagneticResult<?> create(@RequestParam String prjName) {
|
||||
return edPrjService.createNewPrj(prjName, DataOwnEnum.SYS_PRJ.code);
|
||||
|
|
|
|||
|
|
@ -13,27 +13,43 @@ public class UserAccessLog {
|
|||
|
||||
private String id;
|
||||
|
||||
// 操作人的id
|
||||
private String userId;
|
||||
|
||||
// 请求开始时间
|
||||
private Date accessStartTime;
|
||||
|
||||
// 请求结束时间
|
||||
private Date accessEndTime;
|
||||
|
||||
// 请求耗时
|
||||
private Long accessDuration;
|
||||
|
||||
// 进行的操作
|
||||
private String action;
|
||||
|
||||
// 请求的url
|
||||
private String requestUrl;
|
||||
|
||||
// 请求的参数
|
||||
private String reqArgs;
|
||||
|
||||
// 请求远端地址
|
||||
private String remoteAddr;
|
||||
|
||||
// 请求是否成功
|
||||
private Boolean accessSuccess;
|
||||
|
||||
private String failureReason;
|
||||
// 操作详情
|
||||
private String operationMsg;
|
||||
|
||||
// 异常信息 供开发者异常查看
|
||||
private String exceptionDetail;
|
||||
|
||||
// 创建时间,对应操作时间
|
||||
private Date createTime;
|
||||
|
||||
// 操作对象
|
||||
private String operationModule;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,6 +120,11 @@ public class CommonService {
|
|||
return getDbPath(paths);
|
||||
}
|
||||
|
||||
public String getDbPathById(String dirId) {
|
||||
EdFileInfo fileInfo = edFileInfoMapper.selectById(dirId);
|
||||
return getDbPath(fileInfo.getFilePath());
|
||||
}
|
||||
|
||||
private String getDbPath(List<String> ids) {
|
||||
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
|
||||
.select(EdFileInfo::getId, EdFileInfo::getFileName, EdFileInfo::getDataType, EdFileInfo::getFileType, EdFileInfo::getFileCode)
|
||||
|
|
|
|||
|
|
@ -106,6 +106,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
|||
.eq(EdFileInfo::getSaveStatus, EleDataSaveStatusEnum.SUCCESS.code)
|
||||
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
||||
.eq(EdFileInfo::getParentId, pars.getParentId())
|
||||
.eq(EdFileInfo::getDataOwn, dataOwnCode)
|
||||
|
||||
.eq(ObjUtil.equals(pars.getDataStatus(), EleDataStatusEnum.NOT_PUBLISHED.code), EdFileInfo::getDataStatus, EleDataStatusEnum.NOT_PUBLISHED.code)
|
||||
.eq(ObjUtil.equals(pars.getDataStatus(), EleDataStatusEnum.PUBLISHED.code), EdFileInfo::getDataStatus, EleDataStatusEnum.PUBLISHED.code)
|
||||
|
|
@ -719,16 +720,19 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
|||
@Transactional(rollbackFor = Exception.class)
|
||||
public ElectromagneticResult<?> upload(String parentId, MultipartFile file, Integer strategy, int dataOwnCode) {
|
||||
|
||||
Assert.isTrue(!file.isEmpty(), "禁止上传空文件");
|
||||
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), NAME_VALID_MSG);
|
||||
String destPath = commonService.getDbPathById(parentId);
|
||||
String fileName = file.getOriginalFilename();
|
||||
String strategyStr = FileRepeatEnum.getDesc(strategy);
|
||||
Assert.isTrue(!file.isEmpty(), StrFormatter.format("文件 {} 为空,文件上传到 {} 失败,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr));
|
||||
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), StrFormatter.format("文件 {} {},上传到 {} 失败,同名同后缀的处理方式为 {}", fileName, NAME_VALID_MSG, destPath, strategyStr));
|
||||
// 查找下一层,看是否存在顶级定义相关文件,如果存在,则该层属于管理员层级定义的,不允许上传文件
|
||||
long dirCount = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||
.eq(EdFileInfo::getParentId, parentId)
|
||||
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
||||
.eq(EdFileInfo::getDataOwn, dataOwnCode)
|
||||
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code));
|
||||
Assert.isTrue(dirCount == 0, "层级目录不允许上传文件");
|
||||
String fileName = file.getOriginalFilename();
|
||||
Assert.isTrue(dirCount == 0, "文件 {} 上传到 {} 失败,层级结构不允许上传文件,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr);
|
||||
|
||||
String mainName = FileUtil.mainName(fileName);
|
||||
String suffix = FileUtil.getSuffix(fileName);
|
||||
EdFileInfo newEdFileInfo = new EdFileInfo();
|
||||
|
|
@ -771,6 +775,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
|||
// fileSystemService.save(FileUtil.getInputStream(fileDestPath), fileDestPath); // 这里会导致文件大小为0,考虑到当前的系统为OS文件系统,暂不处理
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String info = StrFormatter.format("文件 {} 为上传到 {} 失败,原因 {},同名同后缀的处理方式为 {}", fileName, destPath, e.getMessage(), strategyStr);
|
||||
newEdFileInfo.setParentId(parentId)
|
||||
.setFileName(mainName)
|
||||
.setFileType(suffix)
|
||||
|
|
@ -781,10 +786,10 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
|||
.setSaveStatus(EleDataSaveStatusEnum.FAIL.code)
|
||||
.setDataOwn(dataOwnCode);
|
||||
this.saveOrUpdate(newEdFileInfo);
|
||||
String info = "上传文件失败";
|
||||
log.error(info, e);
|
||||
throw new BizException(info);
|
||||
}
|
||||
UserThreadLocal.setSuccessMsg(StrFormatter.format("文件 {} 为上传到 {} 成功,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr));
|
||||
return ElectromagneticResultUtil.success(true);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,13 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
|
|||
public ElectromagneticResult<?> createNewPrj(String prjName, int dataOwnCode) {
|
||||
|
||||
Assert.isTrue(EleCommonUtil.isFileNameValid(prjName), NAME_VALID_MSG);
|
||||
|
||||
if (!EleCommonUtil.isFileNameValid(prjName)) {
|
||||
String info = StrFormatter.format("{} {}", prjName, NAME_VALID_MSG);
|
||||
log.info(info);
|
||||
return ElectromagneticResultUtil.fail("-1", info);
|
||||
}
|
||||
|
||||
// 首先检查工程是否存在
|
||||
Long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||
.eq(EdFileInfo::getParentId, PRJ_PARENT_ID)
|
||||
|
|
@ -103,9 +110,9 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
|
|||
// 保存到文件系统
|
||||
fileSystemService.createDirectory(commonService.getEleDataPath(dataOwnCode) + File.separator + prjName);
|
||||
} catch (Exception e) {
|
||||
String info = StrFormatter.format("文件创建失败,具体为--->{}", e.getMessage());
|
||||
String info = StrFormatter.format("工程创建失败,具体为--->{}", e.getMessage());
|
||||
log.error(info, e);
|
||||
throw new BizException(info);
|
||||
return ElectromagneticResultUtil.fail("-1", info);
|
||||
}
|
||||
return ElectromagneticResultUtil.success(true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ public interface ElectromagneticConstants {
|
|||
|
||||
String UPLOAD_FILE_CHUNK_SUFFIX = ".part";
|
||||
|
||||
String NAME_VALID_MSG = "文件名不符合规范,只能包含中文字符、下划线、连字符、加号、数字和英文字符且长度小于32。";
|
||||
String NAME_VALID_MSG = "不符合规范,只能包含中文字符、下划线、连字符、加号、数字和英文字符且长度小于32。";
|
||||
|
||||
String FILE_SEC_PASSWD = "adknfhkj87654knd";
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package com.electromagnetic.industry.software.common.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public enum FileRepeatEnum {
|
||||
|
||||
IGNORE(1, "跳过所有冲突文件"),
|
||||
VERSION(2, "所有冲突文件版本更新"),
|
||||
NEW(3, "重命名所有冲突文件, 文件后加“_1”");
|
||||
public int code;
|
||||
public String desc;
|
||||
|
||||
public static String getDesc(int code) {
|
||||
for (FileRepeatEnum e : FileRepeatEnum.values()) {
|
||||
if (e.code == code) {
|
||||
return e.desc;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -45,4 +45,9 @@ public class UserLoginInfo {
|
|||
* 访问请求的参数
|
||||
*/
|
||||
private String reqArgs;
|
||||
|
||||
private String successMsg;
|
||||
|
||||
private String exceptionDetail;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,4 +28,7 @@ public class ElectromagneticResultUtil {
|
|||
electromagneticResult.setErrorMessage(errorEnum.getErrorMessage());
|
||||
return electromagneticResult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue