From aeb465194251550c9d3748c34910e435d7f760cb Mon Sep 17 00:00:00 2001
From: peng <peng.com>
Date: 星期五, 20 三月 2026 15:22:33 +0800
Subject: [PATCH] 首页视频调整
---
jyz-base-start/src/main/java/com/tievd/jyz/controller/VideoController.java | 73 ++++++++++++++++++++++++++++++++++++
1 files changed, 73 insertions(+), 0 deletions(-)
diff --git a/jyz-base-start/src/main/java/com/tievd/jyz/controller/VideoController.java b/jyz-base-start/src/main/java/com/tievd/jyz/controller/VideoController.java
index d3badba..16f91f9 100644
--- a/jyz-base-start/src/main/java/com/tievd/jyz/controller/VideoController.java
+++ b/jyz-base-start/src/main/java/com/tievd/jyz/controller/VideoController.java
@@ -40,6 +40,9 @@
@Value("${video.local.path:E:\\yclCode\\DEV_ZHJYZ\\DEV_ZHJYZ\\video-test}")
private String videoBasePath;
+
+ @Value("${image.local.path:E:\\yclCode\\DEV_ZHJYZ\\DEV_ZHJYZ\\images}")
+ private String imageBasePath;
private final Map<String, AtomicInteger> convertingTasks = new ConcurrentHashMap<>();
private final int MAX_CONCURRENT_CONVERSIONS = 2;
@@ -93,6 +96,68 @@
} catch (Exception e) {
log.error("鑾峰彇闅忔満瑙嗛澶辫触", e);
return Result.error("鑾峰彇闅忔満瑙嗛澶辫触: " + e.getMessage());
+ }
+ }
+
+ @GetMapping("/random-images")
+ @Operation(summary = "鑾峰彇闅忔満鍥剧墖鍦板潃鍒楄〃")
+ public Result<?> getRandomImages(@RequestParam(defaultValue = "20") Integer count, HttpServletRequest request) {
+ try {
+ int safeCount = Math.max(1, Math.min(count, 200));
+ File imageDir = new File(imageBasePath);
+ if (!imageDir.exists() || !imageDir.isDirectory()) {
+ return Result.error("鍥剧墖鐩綍涓嶅瓨鍦�: " + imageBasePath);
+ }
+ File[] imageFiles = imageDir.listFiles(file -> file.isFile() && isImageFile(file.getName()));
+ if (imageFiles == null || imageFiles.length == 0) {
+ return Result.error("鐩綍涓病鏈夊浘鐗囨枃浠�");
+ }
+ String baseUrl = request.getRequestURL().toString().replace(request.getRequestURI(), "");
+ Random random = new Random();
+ List<String> imageUrls = new ArrayList<>();
+ for (int i = 0; i < safeCount; i++) {
+ File selectedImage = imageFiles[random.nextInt(imageFiles.length)];
+ imageUrls.add(baseUrl + "/cube/jyz/video/image/" + selectedImage.getName());
+ }
+ return Result.ok(imageUrls);
+ } catch (Exception e) {
+ log.error("鑾峰彇闅忔満鍥剧墖澶辫触", e);
+ return Result.error("鑾峰彇闅忔満鍥剧墖澶辫触: " + e.getMessage());
+ }
+ }
+
+ @GetMapping("/image/{imageName:.+}")
+ @Operation(summary = "杩斿洖鏈湴鍥剧墖鏂囦欢")
+ public void streamImage(@PathVariable String imageName, HttpServletResponse response) {
+ try {
+ if (!imageName.matches("^[a-zA-Z0-9._-]+$")) {
+ response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+ File imageDir = new File(imageBasePath);
+ File imageFile = new File(imageDir, imageName);
+ String basePath = imageDir.getCanonicalPath() + File.separator;
+ String imagePath = imageFile.getCanonicalPath();
+ if (!imagePath.startsWith(basePath) || !imageFile.exists() || !imageFile.isFile()) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+ String contentType = Files.probeContentType(imageFile.toPath());
+ if (contentType == null || !contentType.startsWith("image/")) {
+ contentType = MediaType.IMAGE_JPEG_VALUE;
+ }
+ response.setHeader("Access-Control-Allow-Origin", "*");
+ response.setHeader("Cache-Control", "max-age=3600");
+ response.setContentType(contentType);
+ response.setHeader("Content-Length", String.valueOf(imageFile.length()));
+ Files.copy(imageFile.toPath(), response.getOutputStream());
+ response.getOutputStream().flush();
+ } catch (Exception e) {
+ log.error("鍥剧墖璇诲彇澶辫触: {}", imageName, e);
+ try {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ } catch (IOException ignored) {
+ }
}
}
@@ -188,6 +253,14 @@
return videoName != null && videoName.toLowerCase(Locale.ROOT).endsWith("_converted.mp4");
}
+ private boolean isImageFile(String fileName) {
+ if (fileName == null) {
+ return false;
+ }
+ String lowerName = fileName.toLowerCase(Locale.ROOT);
+ return lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") || lowerName.endsWith(".png") || lowerName.endsWith(".webp") || lowerName.endsWith(".gif") || lowerName.endsWith(".bmp");
+ }
+
private boolean waitForConvertedVideo(File outputFile, long waitMs) {
long deadline = System.currentTimeMillis() + waitMs;
while (System.currentTimeMillis() < deadline) {
--
Gitblit v1.8.0