|
1 | 1 | package com.semosan.api.domain.demo.controller; |
2 | 2 |
|
3 | 3 | import com.semosan.api.common.config.DemoProperties; |
4 | | -import com.semosan.api.common.config.MinioProperties; |
5 | 4 | import com.semosan.api.common.response.ApiResponse; |
6 | 5 | import com.semosan.api.common.status.SuccessStatus; |
7 | 6 | import com.semosan.api.domain.demo.controller.docs.DemoControllerDocs; |
8 | | -import com.semosan.api.domain.tracking.entity.TrackingPhoto; |
9 | | -import com.semosan.api.domain.tracking.repository.TrackingPhotoRepository; |
10 | | -import io.minio.GetPresignedObjectUrlArgs; |
11 | | -import io.minio.MinioClient; |
12 | | -import io.minio.http.Method; |
| 7 | +import com.semosan.api.domain.demo.service.DemoService; |
13 | 8 | import lombok.RequiredArgsConstructor; |
14 | | -import lombok.extern.slf4j.Slf4j; |
15 | 9 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
16 | 10 | import org.springframework.http.ResponseEntity; |
17 | 11 | import org.springframework.web.bind.annotation.*; |
18 | 12 |
|
19 | | -import java.util.ArrayList; |
20 | | -import java.util.Collections; |
21 | 13 | import java.util.List; |
22 | | -import java.util.concurrent.TimeUnit; |
23 | 14 |
|
24 | | -@Slf4j |
25 | 15 | @RestController |
26 | 16 | @RequestMapping("/api/demo") |
27 | 17 | @RequiredArgsConstructor |
28 | 18 | @EnableConfigurationProperties(DemoProperties.class) |
29 | 19 | public class DemoController implements DemoControllerDocs { |
30 | 20 |
|
31 | | - private static final String BUCKET = "tracking-photos"; |
32 | | - |
33 | | - private final DemoProperties demoProperties; |
34 | | - private final MinioClient minioClient; |
35 | | - private final MinioProperties minioProperties; |
36 | | - private final TrackingPhotoRepository trackingPhotoRepository; |
| 21 | + private final DemoService demoService; |
37 | 22 |
|
38 | 23 | @GetMapping("/tracking/sessions/{sessionId}/photos") |
39 | 24 | @Override |
40 | 25 | public ResponseEntity<ApiResponse<List<String>>> getDemoPhotos( |
41 | 26 | @PathVariable Long sessionId, |
42 | 27 | @RequestParam(defaultValue = "3") int count |
43 | 28 | ) { |
44 | | - List<String> shuffled = new ArrayList<>(demoProperties.photoFilenames()); |
45 | | - Collections.shuffle(shuffled); |
46 | | - List<String> randomUrls = shuffled.subList(0, Math.min(count, shuffled.size())) |
47 | | - .stream() |
48 | | - .map(this::presignedGetUrl) |
49 | | - .toList(); |
50 | | - |
51 | | - List<String> uploadedUrls = trackingPhotoRepository |
52 | | - .findByTrackingSession_IdOrderByMilestoneIndexAsc(sessionId) |
53 | | - .stream() |
54 | | - .map(TrackingPhoto::getImageUrl) |
55 | | - .toList(); |
56 | | - |
57 | | - List<String> combined = new ArrayList<>(randomUrls); |
58 | | - combined.addAll(uploadedUrls); |
59 | | - |
| 29 | + List<String> combined = demoService.getDemoPhotos(sessionId, count); |
60 | 30 | return ApiResponse.success(SuccessStatus.TRACKING_PHOTO_LIST_SUCCESS, combined); |
61 | 31 | } |
62 | | - |
63 | | - private String presignedGetUrl(String objectKey) { |
64 | | - try { |
65 | | - String url = minioClient.getPresignedObjectUrl( |
66 | | - GetPresignedObjectUrlArgs.builder() |
67 | | - .method(Method.GET) |
68 | | - .bucket(BUCKET) |
69 | | - .object(objectKey) |
70 | | - .expiry(1, TimeUnit.HOURS) |
71 | | - .build() |
72 | | - ); |
73 | | - return url.replace(minioProperties.endpoint(), minioProperties.publicUrl()); |
74 | | - } catch (Exception e) { |
75 | | - log.warn("Failed to generate presigned URL for {}", objectKey, e); |
76 | | - return minioProperties.publicUrl() + "/" + BUCKET + "/" + objectKey; |
77 | | - } |
78 | | - } |
79 | 32 | } |
0 commit comments