From 0b39edb68acc67ed01fbfe5d31bfa776a1b17de1 Mon Sep 17 00:00:00 2001
From: zxl <763096477@qq.com>
Date: 星期三, 25 三月 2026 09:14:53 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/show-demo' into show_demo

---
 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