This commit is contained in:
2025-12-18 16:48:45 +08:00
parent b97f0da746
commit 41cbe2bd54
80 changed files with 5434 additions and 351 deletions

View File

@@ -84,4 +84,14 @@ public interface FileMapper extends BaseMapper<TbSysFileDTO> {
*/
@Select("SELECT * FROM file.tb_sys_file WHERE bucket_name = #{bucketName} AND object_name = #{objectName} AND deleted = 0")
TbSysFileDTO selectByMinioObject(@Param("bucketName") String bucketName, @Param("objectName") String objectName);
/**
* @description 根据文件根ID查询最大版本号
* @param fileRootId 文件根ID
* @return Integer 最大版本号
* @author yslg
* @since 2025-12-18
*/
@Select("SELECT MAX(version) FROM file.tb_sys_file WHERE file_root_id = #{fileRootId} AND deleted = 0")
Integer selectMaxVersionByFileRootId(@Param("fileRootId") String fileRootId);
}

View File

@@ -157,6 +157,13 @@ public class FileServiceImpl implements FileService {
}
}
/**
* @description 软删除文件只标记数据库记录为已删除不删除minio文件
* @param fileId 文件ID
* @return ResultDomain<Boolean> 删除结果
* @author yslg
* @since 2025-12-18
*/
@Override
public ResultDomain<Boolean> deleteFile(String fileId) {
try {
@@ -167,13 +174,13 @@ public class FileServiceImpl implements FileService {
}
// 删除MinIO中的文件
minioUtil.deleteFile(sysFile.getBucketName(), sysFile.getObjectName());
// minioUtil.deleteFile(sysFile.getBucketName(), sysFile.getObjectName());
// 逻辑删除数据库记录
int result = fileMapper.deleteByFileId(fileId, "system");
if (result > 0) {
logger.info("文件删除成功: {}", fileId);
logger.info("文件删除成功: {}", fileId);
return ResultDomain.success("文件删除成功", true);
} else {
return ResultDomain.failure("文件删除失败");
@@ -276,6 +283,97 @@ public class FileServiceImpl implements FileService {
return ResultDomain.failure("临时文件保存失败: " + e.getMessage());
}
}
/**
* @description 上传新版本文件用于文件更新fileRootId保持一致version递增
* @param file 文件对象
* @param module 所属模块
* @param businessId 业务ID
* @param fileRootId 文件根ID多版本一致
* @return ResultDomain<TbSysFileDTO> 上传结果,包含新版本文件信息
* @author yslg
* @since 2025-12-18
*/
@Override
public ResultDomain<TbSysFileDTO> uploadFileVersion(MultipartFile file, String module, String businessId, String fileRootId) {
try {
if (file == null || file.isEmpty()) {
return ResultDomain.failure("文件不能为空");
}
if (fileRootId == null || fileRootId.isEmpty()) {
return ResultDomain.failure("文件根ID不能为空");
}
// 1. 获取当前最大版本号
Integer maxVersion = fileMapper.selectMaxVersionByFileRootId(fileRootId);
int newVersion = (maxVersion != null ? maxVersion : 0) + 1;
// 2. 生成文件信息
String originalFilename = file.getOriginalFilename();
String extension = getFileExtension(originalFilename);
String contentType = file.getContentType();
long size = file.getSize();
// 3. 生成唯一的对象名称
String objectName = generateObjectName(originalFilename, module);
// 4. 计算文件MD5
String md5Hash = calculateMD5(file.getBytes());
// 5. 上传到MinIO
String bucketName = minioConfig.getBucketName();
boolean uploadSuccess = minioUtil.uploadFile(
bucketName,
objectName,
file.getInputStream(),
size,
contentType
);
if (!uploadSuccess) {
return ResultDomain.failure("文件上传到MinIO失败");
}
// 6. 构建文件访问URL
String fileUrl = minioConfig.buildFileUrl(objectName);
// 7. 保存到数据库(新版本记录)
TbSysFileDTO fileDTO = new TbSysFileDTO();
fileDTO.setOptsn(UUID.randomUUID().toString());
fileDTO.setFileId(UUID.randomUUID().toString());
fileDTO.setFileRootId(fileRootId);
fileDTO.setVersion(newVersion);
fileDTO.setName(originalFilename);
fileDTO.setPath(objectName);
fileDTO.setSize(size);
fileDTO.setType(extension);
fileDTO.setStorageType("MINIO");
fileDTO.setMimeType(contentType);
fileDTO.setUrl(fileUrl);
fileDTO.setStatus("NORMAL");
fileDTO.setModule(module);
fileDTO.setBusinessId(businessId);
fileDTO.setObjectName(objectName);
fileDTO.setBucketName(bucketName);
fileDTO.setMd5Hash(md5Hash);
fileDTO.setExtension(extension);
fileDTO.setCreateTime(new java.util.Date());
int result = fileMapper.insertFile(fileDTO);
if (result <= 0) {
// 如果数据库保存失败删除MinIO中的文件
minioUtil.deleteFile(bucketName, objectName);
return ResultDomain.failure("文件信息保存失败");
}
logger.info("新版本文件上传成功: {}, version: {}, fileRootId: {}", originalFilename, newVersion, fileRootId);
return ResultDomain.success("文件上传成功", fileDTO);
} catch (Exception e) {
logger.error("新版本文件上传失败", e);
return ResultDomain.failure("文件上传失败: " + e.getMessage());
}
}
/**
* 生成唯一的对象名称