fix:调整删除文件逻辑
This commit is contained in:
parent
250ea16455
commit
2b3b6f85bd
@ -23,8 +23,15 @@ public abstract class CommonConstants {
|
||||
* 是否删除
|
||||
*/
|
||||
public interface TableDelete {
|
||||
Integer UN_DELETE = 0;
|
||||
Integer DELETE = 1;
|
||||
|
||||
/**
|
||||
* 未删除
|
||||
*/
|
||||
Integer UN_DELETED = 0;
|
||||
/**
|
||||
* 已删除
|
||||
*/
|
||||
Integer DELETED = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -15,8 +15,9 @@ public interface FileDao extends IService<File> {
|
||||
|
||||
/**
|
||||
* 根据url md5获取文件信息
|
||||
* @param appCode
|
||||
* @param urlMd5
|
||||
* @return
|
||||
*/
|
||||
File getFileByUrlMd5(String urlMd5);
|
||||
File getByAppCodeAndUrlMd5(String appCode, String urlMd5);
|
||||
}
|
||||
|
||||
@ -20,8 +20,9 @@ import org.springframework.stereotype.Repository;
|
||||
public class FileDaoImpl extends ServiceImpl<FileMapper, File> implements FileDao {
|
||||
|
||||
@Override
|
||||
public File getFileByUrlMd5(String urlMd5) {
|
||||
return lambdaQuery().eq(File::getUrlMd5, urlMd5).eq(File::getStatus, FileStatus.SUCCESS).eq(File::getIsDelete, TableDelete.UN_DELETE)
|
||||
public File getByAppCodeAndUrlMd5(String appCode, String urlMd5) {
|
||||
return lambdaQuery().eq(File::getAppCode, appCode).eq(File::getUrlMd5, urlMd5)
|
||||
.eq(File::getStatus, FileStatus.SUCCESS).eq(File::getIsDelete, TableDelete.UN_DELETED)
|
||||
.last("limit 1").one();
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,4 +20,7 @@ public class ServerFileDeleteRequest {
|
||||
@NotBlank(message = "url must not be null")
|
||||
private String url;
|
||||
|
||||
@NotBlank(message = "operator must not be null")
|
||||
private String operator;
|
||||
|
||||
}
|
||||
|
||||
@ -8,6 +8,12 @@ package cn.axzo.oss.manager.api;
|
||||
**/
|
||||
public interface FileManager {
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param bucketName 桶名称
|
||||
* @param tgtFileKey 目标文件key
|
||||
* @return
|
||||
*/
|
||||
boolean delete(String bucketName, String tgtFileKey);
|
||||
|
||||
/**
|
||||
|
||||
@ -21,6 +21,12 @@ public class FileManagerImpl implements FileManager {
|
||||
@Autowired
|
||||
private AliOssService aliOssService;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param bucketName 桶名称
|
||||
* @param tgtFileKey 目标文件key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean delete(String bucketName, String tgtFileKey) {
|
||||
return aliOssService.delFile(bucketName, tgtFileKey);
|
||||
|
||||
@ -69,15 +69,13 @@ public class FileServiceImpl implements FileService {
|
||||
@Override
|
||||
public void delete(ServerFileDeleteDto dto) {
|
||||
log.info("delete dto = {}", JsonUtil.obj2Str(dto));
|
||||
// 检查app code
|
||||
checkAppCode(dto.getAppCode());
|
||||
|
||||
if (Utility.isBlank(dto.getUrl())) {
|
||||
log.warn("delete url is null");
|
||||
return;
|
||||
}
|
||||
String urlMd5 = Utility.getMd5(dto.getUrl());
|
||||
log.info("delete urlMd5 = {}", urlMd5);
|
||||
File file = fileDao.getFileByUrlMd5(urlMd5);
|
||||
|
||||
File file = fileDao.getByAppCodeAndUrlMd5(dto.getAppCode(), urlMd5);
|
||||
if (Utility.objIsNull(file)) {
|
||||
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
||||
return;
|
||||
@ -88,11 +86,11 @@ public class FileServiceImpl implements FileService {
|
||||
log.info("delete deleteFlag = {}", deleteFlag);
|
||||
if (deleteFlag) {
|
||||
File updateFile = new File();
|
||||
updateFile.setIsDelete(TableDelete.DELETE);
|
||||
updateFile.setIsDelete(TableDelete.DELETED);
|
||||
updateFile.setStatus(FileStatus.DELETED);
|
||||
updateFile.setUpdateBy(dto.getOperator());
|
||||
updateFile.setId(file.getId());
|
||||
fileDao.updateById(file);
|
||||
fileDao.updateById(updateFile);
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,12 +114,8 @@ public class FileServiceImpl implements FileService {
|
||||
public ServerFileUploadResponse upload(ServerFileUploadDto dto) {
|
||||
log.info("update fileName:{},appCode:{},bizScene:{}",
|
||||
dto.getFileName(), dto.getAppCode(), dto.getBizScene());
|
||||
|
||||
FileApp fileAppByAppCode = fileAppDao.getByAppCode(dto.getAppCode());
|
||||
if (fileAppByAppCode == null) {
|
||||
log.error("upload 未找到对应编码 appCode:{}", dto.getAppCode());
|
||||
throw new BizException(CodeEnum.APP_CODE_NOT_FOUND);
|
||||
}
|
||||
// 检查appCode
|
||||
checkAppCode(dto.getAppCode());
|
||||
|
||||
List<AppChannelBucket> appChannelBuckets = appChannelBucketDao
|
||||
.getByAppCode(dto.getAppCode());
|
||||
@ -207,16 +201,12 @@ public class FileServiceImpl implements FileService {
|
||||
|
||||
String uuid = Utility.getUUID();
|
||||
|
||||
// 上传文件
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(fileUploadConfig.getDirectory());
|
||||
builder.append("/");
|
||||
builder.append(uuid);
|
||||
builder.append(".");
|
||||
builder.append(fileFormats);
|
||||
|
||||
// 生成上传文件的唯一key
|
||||
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileFormats);
|
||||
|
||||
String fileUrl = fileManager.uploadByStream(
|
||||
fileUploadConfig.getBucketName(), builder.toString(), dto.getFileContent());
|
||||
fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileContent());
|
||||
if (Utility.isBlank(fileUrl)) {
|
||||
ossFile.setStatus(FileStatusEnum.STATUS_UPLOAD_FAIL.getCode());
|
||||
fileDao.save(ossFile);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user