【8.3.0】【file】更新文件上传增加md5值的计算和保存

master
stylefeng 2024-09-18 18:54:43 +08:00
parent f0fb8b7608
commit dd5957cc75
2 changed files with 14 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import cn.stylefeng.roses.kernel.file.modular.service.MultipleFileService;
import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
@ -28,12 +29,11 @@ public class MultipleFileServiceImpl implements MultipleFileService {
private SysFileInfoService sysFileInfoService;
@Override
@Transactional(rollbackFor = Exception.class)
public List<SysFileInfoResponse> batchUploadFile(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest) {
// 1. 创建文件基本信息
Map<MultipartFile, SysFileInfo> multipartFileSysFileInfoMap = FileInfoFactory.batchCreateFileInfo(fileList, sysFileInfoRequest);
Collection<SysFileInfo> sysFileInfos = multipartFileSysFileInfoMap.values();
this.sysFileInfoService.saveBatch(sysFileInfos);
// 2. 进行批量保存文件
for (Map.Entry<MultipartFile, SysFileInfo> entry : multipartFileSysFileInfoMap.entrySet()) {
@ -42,6 +42,10 @@ public class MultipleFileServiceImpl implements MultipleFileService {
this.sysFileInfoService.storageFile(file, fileInfo);
}
// 2.1 批量保存文件信息
Collection<SysFileInfo> sysFileInfos = multipartFileSysFileInfoMap.values();
this.sysFileInfoService.saveBatch(sysFileInfos);
// 3. 获取文件信息
List<SysFileInfoResponse> fileInfoResponses = new ArrayList<>();
for (SysFileInfo sysFileInfo : sysFileInfos) {

View File

@ -31,6 +31,7 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
@ -142,12 +143,12 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
// 文件请求转换存入数据库的附件信息
SysFileInfo sysFileInfo = FileInfoFactory.createSysFileInfo(file, sysFileInfoRequest);
// 保存文件信息
this.save(sysFileInfo);
// 存储文件到云或者本地
this.storageFile(file, sysFileInfo);
// 保存文件信息
this.save(sysFileInfo);
// 返回文件信息体
return getFileInfoResponse(sysFileInfo);
}
@ -611,6 +612,10 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
} else {
fileOperatorApi.storageFile(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(), bytes);
}
// 【2024年9月18日新增】将文件信息进行md5加密
sysFileInfo.setFileMd5(new MD5().digestHex(bytes));
} catch (IOException e) {
throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
}