优化上传文件。

This commit is contained in:
chenxudong 2025-04-21 14:07:42 +08:00
parent 344666b50b
commit 284dea6a98
1 changed files with 18 additions and 10 deletions

View File

@ -594,13 +594,13 @@ public class CommonService {
public ElectromagneticResult<?> upload(String parentId, MultipartFile file, Integer strategy, int dataOwnCode) {
Assert.isTrue(FileRepeatEnum.contains(strategy), "解决重名文件参数错误");
Assert.isTrue(contains(strategy), "解决重名文件参数错误");
String fileName = file.getOriginalFilename();
String mainName = FileUtil.mainName(fileName);
String suffix = FileUtil.getSuffix(fileName);
Assert.isTrue(StrUtil.isNotEmpty(suffix), "文件类型不能为空");
String destPath = getDbPathById(parentId);
String strategyStr = FileRepeatEnum.getDesc(strategy);
String strategyStr = 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));
// 查找下一层看是否存在顶级定义相关文件如果存在则该层属于管理员层级定义的不允许上传文件
@ -620,7 +620,7 @@ public class CommonService {
Assert.isTrue(dirCount == 0, "文件 {} 上传到 {} 失败,层级结构不允许上传文件,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr);
EdFileInfo newEdFileInfo = new EdFileInfo();
newEdFileInfo.newInit();
EdFileInfo finalEdFileInfo;
EdFileInfo finalEdFileInfo = null;
// 首先检查是否是同名文件
try {
Assert.isTrue(EleCommonUtil.isFileNameValid(fileName), NAME_VALID_MSG);
@ -640,7 +640,7 @@ public class CommonService {
String fileCode = createFileCode(codePathByDbPath, suffix, FILE_START_VERSION, newEdFileInfo.getFileTime());
newEdFileInfo.setParentId(parentId)
.setFileCode(fileCode)
.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code)
.setSaveStatus(EleDataSaveStatusEnum.UPLOADING.code)
.setDataOwn(dataOwnCode)
.setFileName(mainName)
.setFileContent(EleCommonUtil.parse(file.getInputStream(), suffix))
@ -656,10 +656,16 @@ public class CommonService {
FileUtil.writeFromStream(file.getInputStream(), fileDestPath);
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
finalEdFileInfo = newEdFileInfo;
newEdFileInfo.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code);
edFileInfoMapper.insertOrUpdate(newEdFileInfo);
}
} catch (Exception e) {
String info = StrFormatter.format("文件 {} 为上传到 {} 失败,原因 {},同名同后缀的处理方式为 {}", fileName, destPath, e.getMessage(), strategyStr);
newEdFileInfo.setParentId(parentId)
if (Objects.isNull(finalEdFileInfo)) {
log.error(info, e);
throw new BizException(info);
}
finalEdFileInfo.setParentId(parentId)
.setFileName(mainName)
.setSaveStatus(EleDataSaveStatusEnum.FAIL.code)
.setFileType(suffix)
@ -670,18 +676,18 @@ public class CommonService {
.setDataType(EleDataTypeEnum.FILE.code)
.setDataStatus(PublishEnum.PUBLISHED.getCode())
.setEffectFlag(EffectFlagEnum.NOT_EFFECTIVE.code);
edFileInfoMapper.insertOrUpdate(newEdFileInfo);
edFileInfoMapper.insertOrUpdate(finalEdFileInfo);
log.error(info, e);
throw new BizException(info);
}
UserThreadLocal.setSuccessInfo(Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getParentId).orElse(""),
UserThreadLocal.setSuccessInfo(parentId,
Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getId).orElse(""),
"文件 {} 为上传到 {} 成功,同名同后缀的处理方式为 {},存入的文件名为 {}",
fileName,
destPath,
strategyStr,
Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getFileName).orElse(fileName) + "." + Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getFileType).orElse(suffix));
return ElectromagneticResultUtil.success(newEdFileInfo.getId());
fileName + "." + suffix);
return ElectromagneticResultUtil.success(Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getId).orElse(""));
}
@Transactional(rollbackFor = Exception.class)
@ -699,7 +705,7 @@ public class CommonService {
.setFileSize(file.getSize())
.setDataType(EleDataTypeEnum.FILE.code)
.setDataStatus(PublishEnum.PUBLISHED.getCode())
.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code)
.setSaveStatus(EleDataSaveStatusEnum.UPLOADING.code)
.setDataOwn(dataOwnCode)
.setEffectFlag(EffectFlagEnum.EFFECT.code);
if (strategy == REVERSION.code) {
@ -752,6 +758,8 @@ public class CommonService {
String fileDestPath = getFileSysPath(newEdFileInfo.getId());
fileSystemService.save(file.getInputStream(), fileDestPath);
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
newEdFileInfo.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code);
edFileInfoMapper.insertOrUpdate(newEdFileInfo);
return newEdFileInfo;
}