fix:设置固定的附件文件夹,用于存储、删除附件
This commit is contained in:
@@ -70,7 +70,13 @@ public enum DirTypeEnum {
|
||||
TOLERANCE_ANALYSIS_DIR("toleranceAnalysis", 11, "公差分析库"),
|
||||
|
||||
@Schema(description = "异常库", example = "12")
|
||||
EXCEPTION_DIR("exception", 12, "异常库");
|
||||
EXCEPTION_DIR("exception", 12, "异常库"),
|
||||
|
||||
/**
|
||||
* 附件库
|
||||
*/
|
||||
@Schema(description = "附件库文件夹", example = "13")
|
||||
ATTACHMENT_DIR("attachment", 13, "附件库");
|
||||
|
||||
/** 目录名称(英文,用于MinIO路径) */
|
||||
String dirName;
|
||||
|
||||
@@ -484,10 +484,10 @@ public class DataFileController implements IDataFeignClient {
|
||||
@PostMapping(value = "/uploadScriptFile")
|
||||
@Operation(summary = "上传脚本文件", description = "上传脚本文件")
|
||||
public SdmResponse uploadScriptFile(@RequestParam("script") MultipartFile script) {
|
||||
return IDataFileService.uploadScriptAndReportFile(script, DirTypeEnum.SCRIPT_DIR.getDirName());
|
||||
return IDataFileService.uploadFileToFixedDir(script, DirTypeEnum.SCRIPT_DIR.getDirName());
|
||||
}
|
||||
|
||||
// 根基脚本文件ID更新当前脚本文件
|
||||
// 根据本文件ID更新当前脚本文件
|
||||
@PostMapping(value = "/updateScriptFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "更新脚本文件", description = "更新脚本文件")
|
||||
public SdmResponse updateScriptFile(UpdateScriptAndReportReq req) {
|
||||
@@ -498,10 +498,10 @@ public class DataFileController implements IDataFeignClient {
|
||||
@PostMapping(value = "/uploadReportTemplateFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "上传报告模板文件", description = "上传报告模板文件")
|
||||
public SdmResponse<Long> uploadReportTemplateFile(UploadFilesReq req) {
|
||||
return IDataFileService.uploadScriptAndReportFile(req.getFile(), DirTypeEnum.REPORT_TEMPLATE_DIR.getDirName());
|
||||
return IDataFileService.uploadFileToFixedDir(req.getFile(), DirTypeEnum.REPORT_TEMPLATE_DIR.getDirName());
|
||||
}
|
||||
|
||||
// 根基脚本文件ID更新当前脚本文件
|
||||
// 根据脚本文件ID更新当前脚本文件
|
||||
@PostMapping(value = "/updateReportTemplateFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "更新报告模板文件", description = "更新报告模板文件")
|
||||
public SdmResponse<Long> updateReportTemplateFile(UpdateScriptAndReportReq req) {
|
||||
@@ -510,6 +510,19 @@ public class DataFileController implements IDataFeignClient {
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "/uploadAttachment")
|
||||
@Operation(summary = "上传附件", description = "上传附件文件")
|
||||
public SdmResponse<Long> uploadAttachment(@RequestParam("attachment") MultipartFile attachment) {
|
||||
return IDataFileService.uploadFileToFixedDir(attachment, DirTypeEnum.ATTACHMENT_DIR.getDirName());
|
||||
}
|
||||
|
||||
@GetMapping(value = "/deleteAttachment")
|
||||
@Operation(summary = "删除附件", description = "删除附件文件")
|
||||
public SdmResponse<Boolean> deleteAttachment(@RequestParam("fileId") Long fileId) {
|
||||
return IDataFileService.deleteFileFromFixedDir(fileId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 图片预览
|
||||
@GetMapping("/previewImage")
|
||||
|
||||
@@ -291,11 +291,12 @@ public interface IDataFileService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传脚本文件
|
||||
* 上传文件到指定固定目录(脚本、报告、附件等)
|
||||
* @param file 文件
|
||||
* @param dirName 目录名称(如script、reportTemplate、attachment等)
|
||||
* @return 上传文件id
|
||||
*/
|
||||
default SdmResponse<Long> uploadScriptAndReportFile(MultipartFile file, String dirName){
|
||||
default SdmResponse<Long> uploadFileToFixedDir(MultipartFile file, String dirName){
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -304,6 +305,16 @@ public interface IDataFileService {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除固定目录的文件(脚本、报告、附件等)
|
||||
* 直接物理删除,不经过权限校验和审批流程
|
||||
* @param fileId 文件ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
default SdmResponse<Boolean> deleteFileFromFixedDir(Long fileId){
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片预览
|
||||
*
|
||||
|
||||
@@ -3524,7 +3524,7 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SdmResponse<Long> uploadScriptAndReportFile(MultipartFile file, String dirName) {
|
||||
public SdmResponse<Long> uploadFileToFixedDir(MultipartFile file, String dirName) {
|
||||
// 先创建根目录
|
||||
String dirMinioObjectKey = getDirMinioObjectKey(dirName);
|
||||
Optional<FileMetadataInfo> fileMetadataInfoByObjectKey = getFileMetadataInfoByObjectKey(dirMinioObjectKey, ThreadLocalContext.getTenantId());
|
||||
@@ -3568,6 +3568,38 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SdmResponse<Boolean> deleteFileFromFixedDir(Long fileId) {
|
||||
try {
|
||||
// 校验文件是否存在
|
||||
FileMetadataInfo deleteFileMetadataInfo = fileMetadataInfoService.getById(fileId);
|
||||
if (deleteFileMetadataInfo == null) {
|
||||
return SdmResponse.failed("文件不存在");
|
||||
}
|
||||
|
||||
// 如果文件大小为 0,直接删除数据库记录
|
||||
if (deleteFileMetadataInfo.getFileSize() != null && deleteFileMetadataInfo.getFileSize().equals(0L)) {
|
||||
fileMetadataInfoService.removeById(fileId);
|
||||
return SdmResponse.success(true);
|
||||
}
|
||||
|
||||
// 删除 MinIO 中的文件
|
||||
String objectKey = deleteFileMetadataInfo.getObjectKey();
|
||||
String bucketName = deleteFileMetadataInfo.getBucketName();
|
||||
minioService.deleteFile(objectKey, bucketName);
|
||||
|
||||
// 删除数据库记录
|
||||
fileMetadataInfoService.removeById(fileId);
|
||||
|
||||
log.info("删除固定目录文件成功: fileId={}, fileName={}, objectKey={}", fileId, deleteFileMetadataInfo.getOriginalName(), objectKey);
|
||||
return SdmResponse.success(true);
|
||||
} catch (Exception e) {
|
||||
log.error("删除固定目录文件失败: fileId={}", fileId, e);
|
||||
return SdmResponse.failed("删除文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public SdmResponse<Long> updateScriptAndReportFile(UpdateScriptAndReportReq req) {
|
||||
// 先创建 script 目录
|
||||
|
||||
@@ -235,9 +235,10 @@ public class MinioService implements IMinioService {
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建删除对象列表
|
||||
// 构建删除对象列表(文件对象key,不能强制补"/")
|
||||
List<DeleteObject> deleteObjects = objectKeys.stream()
|
||||
.map(key -> new DeleteObject(dealDirPath(key)))
|
||||
.map(MinioService::dealFilePath)
|
||||
.map(DeleteObject::new)
|
||||
.toList();
|
||||
|
||||
// 批量删除
|
||||
@@ -268,6 +269,16 @@ public class MinioService implements IMinioService {
|
||||
return dirPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件对象路径:去除前导"/",保留原始后缀与末尾形态(不强制补"/")
|
||||
*/
|
||||
private static String dealFilePath(String filePath) {
|
||||
if (filePath.startsWith("/")) {
|
||||
filePath = filePath.substring(1);
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归删除指定目录下的所有对象。
|
||||
@@ -340,8 +351,8 @@ public class MinioService implements IMinioService {
|
||||
*/
|
||||
public void deleteFile(String minioObjectKey, String bucketName) {
|
||||
try {
|
||||
// 确保目录名以 "/" 结尾
|
||||
minioObjectKey = dealDirPath(minioObjectKey);
|
||||
// 文件对象key不能强制补"/",否则会导致删不到真实对象
|
||||
minioObjectKey = dealFilePath(minioObjectKey);
|
||||
minioClient.removeObject(
|
||||
RemoveObjectArgs.builder()
|
||||
.bucket(getBucketName(bucketName))
|
||||
|
||||
Reference in New Issue
Block a user