1. 升级 3.0.39
新增 FileApi 获取到完整 fileDO 信息接口
This commit is contained in:
@@ -3,6 +3,7 @@ package com.zt.plat.module.infra.api.file;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.infra.api.file.dto.FileCreateReqDTO;
|
||||
import com.zt.plat.module.infra.api.file.dto.FileRespDTO;
|
||||
import com.zt.plat.module.infra.controller.admin.file.vo.file.FileRespVO;
|
||||
import com.zt.plat.module.infra.dal.dataobject.file.FileDO;
|
||||
import com.zt.plat.module.infra.service.file.FileService;
|
||||
import jakarta.annotation.Resource;
|
||||
@@ -24,6 +25,32 @@ public class FileApiImpl implements FileApi {
|
||||
createReqDTO.getDirectory(), createReqDTO.getType(), false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<FileRespDTO> createFileWithReturn(FileCreateReqDTO createReqDTO) {
|
||||
try {
|
||||
FileRespVO fileRespVO = fileService.createFileWhitReturn(
|
||||
createReqDTO.getContent(),
|
||||
createReqDTO.getName(),
|
||||
createReqDTO.getDirectory(),
|
||||
createReqDTO.getType(),
|
||||
false
|
||||
);
|
||||
|
||||
if (fileRespVO == null) {
|
||||
return CommonResult.error(500, "文件创建失败");
|
||||
}
|
||||
|
||||
// 将 FileRespVO 转换为 FileRespDTO
|
||||
FileRespDTO respDTO = convertToRespDTO(fileRespVO);
|
||||
// 设置文件内容
|
||||
respDTO.setContent(createReqDTO.getContent());
|
||||
|
||||
return success(respDTO);
|
||||
} catch (Exception e) {
|
||||
return CommonResult.error(500, "创建文件失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<FileRespDTO> getFile(Long fileId) {
|
||||
try {
|
||||
@@ -40,19 +67,8 @@ public class FileApiImpl implements FileApi {
|
||||
}
|
||||
|
||||
// 构建响应对象
|
||||
FileRespDTO respDTO = new FileRespDTO();
|
||||
respDTO.setId(fileDO.getId());
|
||||
respDTO.setName(fileDO.getName());
|
||||
respDTO.setType(fileDO.getType());
|
||||
respDTO.setSize(fileDO.getSize());
|
||||
FileRespDTO respDTO = convertToRespDTO(fileDO);
|
||||
respDTO.setContent(content);
|
||||
|
||||
// 从文件路径或URL中提取目录信息
|
||||
String path = fileDO.getPath();
|
||||
if (path != null && path.contains("/")) {
|
||||
String directory = path.substring(0, path.lastIndexOf("/"));
|
||||
respDTO.setDirectory(directory);
|
||||
}
|
||||
|
||||
return success(respDTO);
|
||||
} catch (Exception e) {
|
||||
@@ -60,4 +76,61 @@ public class FileApiImpl implements FileApi {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> deleteFile(Long fileId) {
|
||||
try {
|
||||
// 检查文件是否存在
|
||||
FileDO fileDO = fileService.getActiveFileById(fileId);
|
||||
if (fileDO == null) {
|
||||
return CommonResult.error(404, "文件不存在");
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
fileService.deleteFile(fileId);
|
||||
return success(true);
|
||||
} catch (Exception e) {
|
||||
return CommonResult.error(500, "删除文件失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 FileDO 转换为 FileRespDTO
|
||||
*/
|
||||
private FileRespDTO convertToRespDTO(FileDO fileDO) {
|
||||
FileRespDTO respDTO = new FileRespDTO();
|
||||
respDTO.setId(fileDO.getId());
|
||||
respDTO.setName(fileDO.getName());
|
||||
respDTO.setType(fileDO.getType());
|
||||
respDTO.setSize(fileDO.getSize());
|
||||
|
||||
// 从文件路径中提取目录信息
|
||||
String path = fileDO.getPath();
|
||||
if (path != null && path.contains("/")) {
|
||||
String directory = path.substring(0, path.lastIndexOf("/"));
|
||||
respDTO.setDirectory(directory);
|
||||
}
|
||||
|
||||
return respDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 FileRespVO 转换为 FileRespDTO
|
||||
*/
|
||||
private FileRespDTO convertToRespDTO(FileRespVO fileRespVO) {
|
||||
FileRespDTO respDTO = new FileRespDTO();
|
||||
respDTO.setId(fileRespVO.getId());
|
||||
respDTO.setName(fileRespVO.getName());
|
||||
respDTO.setType(fileRespVO.getType());
|
||||
respDTO.setSize(fileRespVO.getSize());
|
||||
|
||||
// 从文件路径中提取目录信息
|
||||
String path = fileRespVO.getPath();
|
||||
if (path != null && path.contains("/")) {
|
||||
String directory = path.substring(0, path.lastIndexOf("/"));
|
||||
respDTO.setDirectory(directory);
|
||||
}
|
||||
|
||||
return respDTO;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.zt.plat.framework.common.pojo.PageResult;
|
||||
import com.zt.plat.framework.common.util.object.BeanUtils;
|
||||
import com.zt.plat.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
|
||||
@@ -18,7 +19,6 @@ import com.zt.plat.module.infra.framework.file.core.client.FileClient;
|
||||
import com.zt.plat.module.infra.framework.file.core.client.s3.FilePresignedUrlRespDTO;
|
||||
import com.zt.plat.module.infra.framework.file.core.utils.FileTypeUtils;
|
||||
import com.zt.plat.module.infra.util.VerificationCodeUtil;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -122,7 +122,8 @@ public class FileServiceImpl implements FileService {
|
||||
@SneakyThrows
|
||||
public String createFile(byte[] content, String name, String directory, String type, Boolean encrypt) {
|
||||
FileDO entity = uploadFile(content, name, directory, type, encrypt);
|
||||
return entity.getUrl();
|
||||
FileRespVO fileRespVO = BeanUtils.toBean(entity, FileRespVO.class);
|
||||
return fileRespVO.getUrl();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
|
||||
Reference in New Issue
Block a user