文件按时间管理功能

This commit is contained in:
XaoLi717 2024-10-23 10:23:36 +08:00
parent d32c1759af
commit 13967f2a1b
2 changed files with 21 additions and 7 deletions

View File

@ -66,23 +66,29 @@ public class FileController {
return success(true); return success(true);
} }
@GetMapping("/{configId}/get/**") @GetMapping("/{configId}/get/{fileDate}/**")
@PermitAll @PermitAll
@Operation(summary = "下载文件") @Operation(summary = "下载文件")
@Parameter(name = "configId", description = "配置编号", required = true) @Parameter(name = "configId", description = "配置编号", required = true)
public void getFileContent(HttpServletRequest request, public void getFileContent(HttpServletRequest request,
HttpServletResponse response, HttpServletResponse response,
@PathVariable("fileDate") String fileDate,
@PathVariable("configId") Long configId) throws Exception { @PathVariable("configId") Long configId) throws Exception {
// 获取请求的路径 // 获取请求的路径
String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false); String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false);
if (StrUtil.isEmpty(path)) { if (StrUtil.isEmpty(path)) {
throw new IllegalArgumentException("结尾的 path 路径必须传递"); throw new IllegalArgumentException("结尾的 path 路径必须传递");
} }
// 解码解决中文路径的问题 https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/807/
path = URLUtil.decode(path); path = URLUtil.decode(path);
//去除路径时间信息
String[] parts = path.split("/");
if (parts.length > 1) { path = parts[1]; }
//拼接时间路径
String filePath = fileDate+"\\"+path;
// 读取内容 // 读取内容
byte[] content = fileService.getFileContent(configId, path); byte[] content = fileService.getFileContent(configId, filePath);
if (content == null) { if (content == null) {
log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path); log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path);
response.setStatus(HttpStatus.NOT_FOUND.value()); response.setStatus(HttpStatus.NOT_FOUND.value());

View File

@ -4,6 +4,8 @@ import cn.hutool.core.io.FileUtil;
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient; import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
import java.io.File; import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/** /**
* 本地文件客户端 * 本地文件客户端
@ -26,11 +28,17 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
@Override @Override
public String upload(byte[] content, String path, String type) { public String upload(byte[] content, String path, String type) {
// 执行写入 //文件路径加上当前时间
String filePath = getFilePath(path); LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date = now.format(formatter); //当前时间
String fPath = date+"\\"+path; //文件路径
String uPath = date+"/"+path; //url路径
// 执行写入文件路径
String filePath = getFilePath(fPath);
FileUtil.writeBytes(content, filePath); FileUtil.writeBytes(content, filePath);
// 拼接返回路径 // 拼接url路径返回路径
return super.formatFileUrl(config.getDomain(), path); return super.formatFileUrl(config.getDomain(), uPath);
} }
@Override @Override