mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
1、文件服务新增feign接口上传文件返回id;2、文件服务新增根据id获取文件接口;
This commit is contained in:
+14
@@ -75,4 +75,18 @@ public interface FileApi {
|
||||
@Parameter(name = "path", description = "文件地址", example = "url", required = true)
|
||||
CommonResult<Boolean> deleteFileByPath(String path);
|
||||
|
||||
@PostMapping(PREFIX + "/create/id")
|
||||
@Operation(summary = "保存文件,并返回文件id")
|
||||
CommonResult<Long> createFileAndReturnId(@Valid @RequestBody FileCreateReqDTO createReqDTO);
|
||||
|
||||
/**
|
||||
* 保存文件,并返回文件id
|
||||
*
|
||||
* @param content 文件内容
|
||||
* @param fileName 文件名称
|
||||
* @return 文件路径
|
||||
*/
|
||||
default Long createFileAndReturnId(byte[] content, String fileName) {
|
||||
return createFileAndReturnId(new FileCreateReqDTO().setName(fileName).setPath(null).setContent(content)).getCheckedData();
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -28,4 +28,9 @@ public class FileApiImpl implements FileApi {
|
||||
return success(fileService.deleteFileByPath(path));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Long> createFileAndReturnId(FileCreateReqDTO createReqDTO) {
|
||||
return success(fileService.createFileDO(createReqDTO.getName(), createReqDTO.getPath(),
|
||||
createReqDTO.getContent()));
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -85,6 +85,22 @@ public class FileController {
|
||||
ServletUtils.writeAttachment(response, path, content);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "根据文件编号下载文件")
|
||||
@Parameter(name = "id", description = "文件编号", required = true)
|
||||
@OperateLog(enable = false)
|
||||
public void getFileContentById(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
@PathVariable("id") Long id) throws Exception {
|
||||
byte[] content = fileService.getFileById(id);
|
||||
if (content == null) {
|
||||
log.warn("[getFileContent][id({}) 文件不存在]", id);
|
||||
response.setStatus(HttpStatus.NOT_FOUND.value());
|
||||
return;
|
||||
}
|
||||
ServletUtils.writeAttachment(response, String.valueOf(id), content);
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得文件分页")
|
||||
@PreAuthorize("@ss.hasPermission('infra:file:query')")
|
||||
|
||||
+19
@@ -29,6 +29,16 @@ public interface FileService {
|
||||
*/
|
||||
String createFile(String name, String path, byte[] content);
|
||||
|
||||
/**
|
||||
* 保存文件,并返回文件id
|
||||
*
|
||||
* @param name 文件名称
|
||||
* @param path 文件路径
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
Long createFileDO(String name, String path, byte[] content);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
@@ -45,6 +55,15 @@ public interface FileService {
|
||||
*/
|
||||
byte[] getFileContent(Long configId, String path) throws Exception;
|
||||
|
||||
/**
|
||||
* 通过文件id获取文件内容
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
byte[] getFileById(Long id) throws Exception;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param path 文件地址
|
||||
|
||||
+43
@@ -3,6 +3,7 @@ package com.cf.imes.module.infra.service.file;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import com.cf.imes.framework.common.pojo.PageResult;
|
||||
import com.cf.imes.framework.common.util.Assert.AssertUtils;
|
||||
import com.cf.imes.framework.common.util.io.FileUtils;
|
||||
import com.cf.imes.module.infra.framework.file.core.client.FileClient;
|
||||
import com.cf.imes.module.infra.framework.file.core.utils.FileTypeUtils;
|
||||
@@ -72,6 +73,36 @@ public class FileServiceImpl implements FileService {
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Long createFileDO(String name, String path, byte[] content) {
|
||||
// 计算默认的 path 名
|
||||
String type = FileTypeUtils.getMineType(content, name);
|
||||
if (CharSequenceUtil.isEmpty(path)) {
|
||||
path = FileUtils.generatePath(content, name);
|
||||
}
|
||||
// 如果 name 为空,则使用 path 填充
|
||||
if (CharSequenceUtil.isEmpty(name)) {
|
||||
name = path;
|
||||
}
|
||||
|
||||
// 上传到文件存储器
|
||||
FileClient client = fileConfigService.getMasterFileClient();
|
||||
Assert.notNull(client, "客户端(master) 不能为空");
|
||||
String url = client.upload(content, path, type);
|
||||
|
||||
// 保存到数据库
|
||||
FileDO file = new FileDO();
|
||||
file.setConfigId(client.getId());
|
||||
file.setName(name);
|
||||
file.setPath(path);
|
||||
file.setUrl(url);
|
||||
file.setType(type);
|
||||
file.setSize(content.length);
|
||||
fileMapper.insert(file);
|
||||
return file.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(Long id) throws Exception {
|
||||
// 校验存在
|
||||
@@ -101,6 +132,18 @@ public class FileServiceImpl implements FileService {
|
||||
return client.getContent(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getFileById(Long id) throws Exception {
|
||||
FileDO fileDO = fileMapper.selectById(id);
|
||||
AssertUtils.notEmpty(fileDO, FILE_NOT_EXISTS);
|
||||
|
||||
Long configId = fileDO.getConfigId();
|
||||
FileClient client = fileConfigService.getFileClient(configId);
|
||||
Assert.notNull(client, CLIENT_ERR_NOTIFICATION, configId);
|
||||
|
||||
return client.getContent(fileDO.getPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteFileByPath(String path) {
|
||||
// 校验存在
|
||||
|
||||
Reference in New Issue
Block a user