feat: make upload path and base-url configurable via properties

This commit is contained in:
MaDaLei 2026-04-13 14:29:35 +08:00
parent be63a3a377
commit 3c04019af4
2 changed files with 15 additions and 13 deletions

View File

@ -26,15 +26,14 @@ import java.util.UUID;
@CrossOrigin
public class FileController {
private static final String UPLOAD_BASE = "/Users/wac/Desktop/www/_src/petstore/backend/uploads/";
@Value("${upload.path:uploads}")
@Value("${upload.path:uploads/}")
private String uploadPath;
@GetMapping("/image/**")
public ResponseEntity<Resource> getImage(HttpServletRequest request) throws IOException {
String path = request.getRequestURI().replace("/api/upload/image", "");
File file = new File(UPLOAD_BASE + path);
String basePath = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/";
File file = new File(basePath + path);
if (!file.exists()) {
return ResponseEntity.notFound().build();
}
@ -49,7 +48,8 @@ public class FileController {
@GetMapping("/legacy/**")
public ResponseEntity<Resource> getLegacyImage(HttpServletRequest request) throws IOException {
String path = request.getRequestURI().replace("/api/upload/legacy", "");
File file = new File(UPLOAD_BASE + path);
String basePath = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/";
File file = new File(basePath + path);
if (!file.exists()) {
return ResponseEntity.notFound().build();
}
@ -78,9 +78,9 @@ public class FileController {
}
try {
// 创建上传目录使用绝对路径
// 创建上传目录
String datePath = LocalDate.now().toString().replace("-", "/");
String dirPath = UPLOAD_BASE + datePath; // /.../uploads/2026/04/01
String dirPath = uploadPath.endsWith("/") ? uploadPath + datePath : uploadPath + "/" + datePath;
File dir = new File(dirPath);
if (!dir.exists()) dir.mkdirs();

View File

@ -6,6 +6,7 @@ import com.petstore.service.ReportService;
import com.petstore.service.StoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import java.util.HashMap;
import java.util.List;
@ -20,12 +21,13 @@ public class ReportController {
private final ReportService reportService;
private final StoreService storeService;
private static final String BASE_URL = "http://localhost:8080";
@Value("${app.base-url:http://localhost:8080}")
private String baseUrl;
private String fullUrl(String path) {
if (path == null || path.isEmpty()) return path;
if (path.startsWith("http")) return path;
return BASE_URL + path;
return baseUrl + path;
}
@PostMapping("/create")