增加品牌方案管理
This commit is contained in:
+41
-4
@@ -4,6 +4,7 @@ import cn.novalon.gym.manage.file.core.domain.SysFile;
|
||||
import cn.novalon.gym.manage.file.core.service.ISysFileService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.codec.multipart.FilePart;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -21,9 +22,12 @@ import java.nio.file.Paths;
|
||||
public class SysFileHandler {
|
||||
|
||||
private final ISysFileService fileService;
|
||||
private final String uploadDir;
|
||||
|
||||
public SysFileHandler(ISysFileService fileService) {
|
||||
public SysFileHandler(ISysFileService fileService,
|
||||
@Value("${file.upload.dir:/tmp/uploads}") String uploadDir) {
|
||||
this.fileService = fileService;
|
||||
this.uploadDir = uploadDir;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有文件", description = "获取系统中所有文件列表")
|
||||
@@ -88,8 +92,13 @@ public class SysFileHandler {
|
||||
@Operation(summary = "根据文件名下载", description = "根据文件名下载文件")
|
||||
public Mono<ServerResponse> downloadFileByName(ServerRequest request) {
|
||||
String fileName = request.pathVariable("fileName");
|
||||
// 支持多段路径(如 brand/logo/xxx.png),提取最后一段作为文件名
|
||||
if (fileName != null && fileName.contains("/")) {
|
||||
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
|
||||
}
|
||||
final String finalFileName = fileName;
|
||||
return fileService.getAllFiles()
|
||||
.filter(file -> file.getFileName().equals(fileName))
|
||||
.filter(file -> file.getFileName().equals(finalFileName))
|
||||
.next()
|
||||
.flatMap(file -> {
|
||||
try {
|
||||
@@ -127,8 +136,15 @@ public class SysFileHandler {
|
||||
@Operation(summary = "根据文件名预览", description = "根据文件名预览文件")
|
||||
public Mono<ServerResponse> previewFileByName(ServerRequest request) {
|
||||
String fileName = request.pathVariable("fileName");
|
||||
// 保存原始路径,用于磁盘文件回退(如 brand/logo/xxx.png)
|
||||
final String originalPath = fileName;
|
||||
// 支持多段路径(如 brand/logo/xxx.png),提取最后一段作为文件名
|
||||
if (fileName != null && fileName.contains("/")) {
|
||||
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
|
||||
}
|
||||
final String finalFileName = fileName;
|
||||
return fileService.getAllFiles()
|
||||
.filter(file -> file.getFileName().equals(fileName))
|
||||
.filter(file -> file.getFileName().equals(finalFileName))
|
||||
.next()
|
||||
.flatMap(file -> {
|
||||
try {
|
||||
@@ -141,7 +157,28 @@ public class SysFileHandler {
|
||||
return ServerResponse.notFound().build();
|
||||
}
|
||||
})
|
||||
.switchIfEmpty(ServerResponse.notFound().build());
|
||||
.switchIfEmpty(Mono.defer(() -> {
|
||||
// 回退:从上传目录直接读取文件(用于品牌图片等未注册 sys_file 的文件)
|
||||
try {
|
||||
Path diskPath = Paths.get(uploadDir, originalPath);
|
||||
if (Files.exists(diskPath) && !Files.isDirectory(diskPath)) {
|
||||
byte[] fileContent = Files.readAllBytes(diskPath);
|
||||
String contentType = "image/" + getFileExtension(originalPath);
|
||||
return ServerResponse.ok()
|
||||
.header("Content-Type", contentType)
|
||||
.bodyValue(fileContent);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return ServerResponse.notFound().build();
|
||||
}));
|
||||
}
|
||||
|
||||
private String getFileExtension(String filename) {
|
||||
if (filename == null || !filename.contains(".")) return "jpeg";
|
||||
String ext = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
|
||||
if ("jpg".equals(ext)) return "jpeg";
|
||||
return ext;
|
||||
}
|
||||
|
||||
@Operation(summary = "删除文件", description = "删除指定文件")
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ class SysFileHandlerTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
fileHandler = new SysFileHandler(fileService);
|
||||
fileHandler = new SysFileHandler(fileService, "/tmp/uploads");
|
||||
testFile = new SysFile();
|
||||
testFile.setId(1L);
|
||||
testFile.setFileName("test.txt");
|
||||
|
||||
Reference in New Issue
Block a user