feat:(unify): 读取系统的目录和文件信息

This commit is contained in:
opensnail 2024-12-01 12:15:27 +08:00
parent 821e7fb659
commit 7ff278d04d
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package com.aizuda.snailjob.server.web.controller;
import com.aizuda.snailjob.server.common.exception.SnailJobServerException;
import com.aizuda.snailjob.server.web.model.response.ViewFileResponseVO;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileViewerController {
// 显示文件列表
@GetMapping("/files")
public List<String> listFiles(@RequestParam("rootPath") String rootPath) {
try {
// 读取目录下的文件列表
return Files.list(Paths.get(rootPath))
.map(path -> path.getFileName().toString())
.toList();
} catch (IOException e) {
throw new SnailJobServerException("获取文件列表失败", e);
}
}
// 显示文件内容
@GetMapping("/file")
public ViewFileResponseVO viewFile(@RequestParam("fileName") String fileName, @RequestParam("fullPath") String fullPath) {
try {
// 构建文件路径
String filePath = Paths.get(fullPath, fileName).toString();
// 读取文件内容
String s = Files.readString(Paths.get(filePath));
return ViewFileResponseVO.builder().content(s).build();
} catch (IOException e) {
throw new SnailJobServerException("读取文件失败", e);
}
}
}

View File

@ -0,0 +1,14 @@
package com.aizuda.snailjob.server.web.model.response;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class ViewFileResponseVO {
private String fileName;
private String filePath;
private String fileSize;
private String content;
}