mirror of
https://gitee.com/xiaonuobase/snowy
synced 2025-12-16 11:13:59 +08:00
【升级】文件管理增加物理删除方法,加入一个字段
This commit is contained in:
@@ -41,6 +41,10 @@ public class DevFile extends CommonEntity {
|
||||
@Schema(description = "存储桶")
|
||||
private String bucket;
|
||||
|
||||
/** 文件Key */
|
||||
@Schema(description = "文件Key")
|
||||
private String fileKey;
|
||||
|
||||
/** 文件名称 */
|
||||
@Schema(description = "文件名称")
|
||||
private String name;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*/
|
||||
package vip.xiaonuo.dev.modular.file.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import vip.xiaonuo.common.mapper.CommonDeleteAbsoluteMapper;
|
||||
import vip.xiaonuo.dev.modular.file.entity.DevFile;
|
||||
|
||||
/**
|
||||
@@ -21,5 +21,5 @@ import vip.xiaonuo.dev.modular.file.entity.DevFile;
|
||||
* @author xuyuxiang
|
||||
* @date 2022/2/23 18:40
|
||||
**/
|
||||
public interface DevFileMapper extends BaseMapper<DevFile> {
|
||||
public interface DevFileMapper extends CommonDeleteAbsoluteMapper<DevFile> {
|
||||
}
|
||||
|
||||
@@ -17,8 +17,10 @@ import cn.hutool.json.JSONUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import vip.xiaonuo.dev.api.DevConfigApi;
|
||||
import vip.xiaonuo.dev.api.DevFileApi;
|
||||
import vip.xiaonuo.dev.modular.file.enums.DevFileEngineTypeEnum;
|
||||
import vip.xiaonuo.dev.modular.file.param.DevFileIdParam;
|
||||
import vip.xiaonuo.dev.modular.file.service.DevFileService;
|
||||
|
||||
import java.util.Optional;
|
||||
@@ -32,9 +34,20 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class DevFileApiProvider implements DevFileApi {
|
||||
|
||||
/** 默认文件引擎 */
|
||||
private static final String SNOWY_SYS_DEFAULT_FILE_ENGINE_KEY = "SNOWY_SYS_DEFAULT_FILE_ENGINE";
|
||||
|
||||
@Resource
|
||||
private DevFileService devFileService;
|
||||
|
||||
@Resource
|
||||
private DevConfigApi devConfigApi;
|
||||
|
||||
@Override
|
||||
public String uploadDynamicReturnId(MultipartFile file) {
|
||||
return devFileService.uploadReturnId(devConfigApi.getValueByKey(SNOWY_SYS_DEFAULT_FILE_ENGINE_KEY), file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String storageFileWithReturnUrlLocal(MultipartFile file) {
|
||||
return devFileService.uploadReturnUrl(DevFileEngineTypeEnum.LOCAL.getValue(), file);
|
||||
@@ -81,4 +94,11 @@ public class DevFileApiProvider implements DevFileApi {
|
||||
.map(JSONUtil::parseObj)
|
||||
.orElse(new JSONObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAbsoluteById(String id) {
|
||||
DevFileIdParam devFileIdParam = new DevFileIdParam();
|
||||
devFileIdParam.setId(id);
|
||||
devFileService.deleteAbsolute(devFileIdParam);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +81,14 @@ public interface DevFileService extends IService<DevFile> {
|
||||
**/
|
||||
void delete(List<DevFileIdParam> devFileIdParamList);
|
||||
|
||||
/**
|
||||
* 物理删除文件
|
||||
*
|
||||
* @author xuyuxiang
|
||||
* @date 2022/8/4 10:36
|
||||
**/
|
||||
void deleteAbsolute(DevFileIdParam devFileIdParam);
|
||||
|
||||
/**
|
||||
* 获取文件详情
|
||||
*
|
||||
|
||||
@@ -28,7 +28,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import vip.xiaonuo.common.exception.CommonException;
|
||||
import vip.xiaonuo.common.page.CommonPageRequest;
|
||||
@@ -60,6 +62,7 @@ import java.util.List;
|
||||
* @author xuyuxiang
|
||||
* @date 2022/2/23 18:43
|
||||
**/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DevFileServiceImpl extends ServiceImpl<DevFileMapper, DevFile> implements DevFileService {
|
||||
|
||||
@@ -122,10 +125,40 @@ public class DevFileServiceImpl extends ServiceImpl<DevFileMapper, DevFile> impl
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<DevFileIdParam> devFileIdParamList) {
|
||||
this.removeByIds(CollStreamUtil.toList(devFileIdParamList, DevFileIdParam::getId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAbsolute(DevFileIdParam devFileIdParam) {
|
||||
DevFile devFile = this.queryEntity(devFileIdParam.getId());
|
||||
try {
|
||||
// 存储引擎
|
||||
String engine = devFile.getEngine();
|
||||
// 存储桶名称
|
||||
String bucketName = devFile.getBucket();
|
||||
// 文件key
|
||||
String fileKey = devFile.getFileKey();
|
||||
// 根据存储引擎删除文件
|
||||
if (DevFileEngineTypeEnum.LOCAL.getValue().equals(engine)) {
|
||||
DevFileLocalUtil.deleteFile(bucketName, fileKey);
|
||||
} else if (DevFileEngineTypeEnum.ALIYUN.getValue().equals(engine)) {
|
||||
DevFileAliyunUtil.deleteFile(bucketName, fileKey);
|
||||
} else if (DevFileEngineTypeEnum.TENCENT.getValue().equals(engine)) {
|
||||
DevFileTencentUtil.deleteFile(bucketName, fileKey);
|
||||
} else if (DevFileEngineTypeEnum.MINIO.getValue().equals(engine)) {
|
||||
DevFileMinIoUtil.deleteFile(bucketName, fileKey);
|
||||
} else {
|
||||
log.error("未知存储引擎:{}", engine);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("文件删除失败:{},路径:{}", devFile.getName(), devFile.getStoragePath(), e);
|
||||
throw new CommonException("文件删除失败"); // 触发事务回滚
|
||||
}
|
||||
this.baseMapper.deleteAbsoluteById(devFile.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储文件
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user