|
1 | 1 | package com.example.backend.dashboard.service; |
2 | 2 |
|
| 3 | +import com.amazonaws.HttpMethod; |
| 4 | +import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; |
3 | 5 | import com.example.backend.common.domain.CaseEntity; |
4 | 6 | import com.example.backend.common.domain.PoliceEntity; |
5 | 7 | import com.example.backend.dashboard.dto.DashboardResponse; |
|
10 | 12 | import jakarta.persistence.EntityNotFoundException; |
11 | 13 | import jakarta.servlet.http.HttpSession; |
12 | 14 | import lombok.RequiredArgsConstructor; |
| 15 | +import org.springframework.beans.factory.annotation.Value; |
13 | 16 | import org.springframework.stereotype.Service; |
14 | 17 | import org.springframework.transaction.annotation.Transactional; |
15 | 18 |
|
16 | 19 | import java.time.LocalDateTime; |
17 | 20 | import java.time.format.DateTimeFormatter; |
18 | 21 | import java.util.*; |
| 22 | +import java.util.concurrent.TimeUnit; |
19 | 23 | import java.util.stream.Collectors; |
20 | 24 |
|
| 25 | + |
| 26 | +import com.amazonaws.services.s3.AmazonS3; |
| 27 | + |
| 28 | + |
21 | 29 | @Service |
22 | 30 | @Transactional |
23 | 31 | @RequiredArgsConstructor |
24 | 32 | public class DashboardService { |
25 | 33 |
|
| 34 | + @Value("${cloud.aws.bucket}") |
| 35 | + private String bucket; |
| 36 | + private final AmazonS3 s3Client; |
26 | 37 | private final DashboardRepository dashboardRepository; |
27 | 38 |
|
| 39 | + |
| 40 | + public Map<String, String> getCaseVideo(int id, HttpSession session) { |
| 41 | + CaseEntity caseEntity = getAuthorizedCase(id, session); |
| 42 | + |
| 43 | + String videoKey = caseEntity.getVideo(); |
| 44 | + if (videoKey == null || videoKey.trim().isEmpty()) { |
| 45 | + throw new EntityNotFoundException("해당 사건에 대한 영상이 없습니다."); |
| 46 | + } |
| 47 | + |
| 48 | + if (caseEntity.getState() == CaseEntity.CaseState.미확인) { |
| 49 | + caseEntity.setState(CaseEntity.CaseState.확인); |
| 50 | + dashboardRepository.save(caseEntity); |
| 51 | + } |
| 52 | + |
| 53 | + // Presigned URL 유효기간 설정 (30분) |
| 54 | + Date expiration = new Date(); |
| 55 | + long expTime = expiration.getTime(); |
| 56 | + expTime += TimeUnit.MINUTES.toMillis(30); // 30 minute |
| 57 | + expiration.setTime(expTime); |
| 58 | + |
| 59 | + GeneratePresignedUrlRequest presignRequest = |
| 60 | + new GeneratePresignedUrlRequest(bucket, videoKey) |
| 61 | + .withMethod(HttpMethod.GET) |
| 62 | + .withExpiration(expiration); |
| 63 | + |
| 64 | + String url = s3Client.generatePresignedUrl(presignRequest).toString(); |
| 65 | + return Collections.singletonMap("video", url); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
28 | 72 | // 세션에서 officeId 추출 |
29 | 73 | private int getAuthenticatedOfficeId(HttpSession session) { |
30 | 74 | UserResponseDto user = (UserResponseDto) session.getAttribute("user"); |
@@ -80,22 +124,54 @@ public List<DashboardResponse> getCases(HttpSession session) { |
80 | 124 | .collect(Collectors.toList()); |
81 | 125 | } |
82 | 126 |
|
83 | | - // id별 사건 영상 확인 |
84 | | - public Map<String, String> getCaseVideo(int id, HttpSession session) { |
85 | | - CaseEntity caseEntity = getAuthorizedCase(id, session); |
86 | 127 |
|
87 | | - String videoUrl = caseEntity.getVideo(); |
88 | | - if (videoUrl == null || videoUrl.trim().isEmpty()) { |
89 | | - throw new EntityNotFoundException("해당 사건에 대한 영상이 없습니다."); |
90 | | - } |
91 | | - |
92 | | - if (caseEntity.getState() == CaseEntity.CaseState.미확인) { |
93 | | - caseEntity.setState(CaseEntity.CaseState.확인); |
94 | | - dashboardRepository.save(caseEntity); |
95 | | - } |
96 | | - |
97 | | - return Collections.singletonMap("video", videoUrl); |
98 | | - } |
| 128 | +// public Map<String, String> getCaseVideo(int id, HttpSession session) { |
| 129 | +// CaseEntity caseEntity = getAuthorizedCase(id, session); |
| 130 | +// |
| 131 | +// String videoUrl = caseEntity.getVideo(); |
| 132 | +// if (videoUrl == null || videoUrl.trim().isEmpty()) { |
| 133 | +// throw new EntityNotFoundException("해당 사건에 대한 영상이 없습니다."); |
| 134 | +// } |
| 135 | +// |
| 136 | +// if (caseEntity.getState() == CaseEntity.CaseState.미확인) { |
| 137 | +// caseEntity.setState(CaseEntity.CaseState.확인); |
| 138 | +// dashboardRepository.save(caseEntity); |
| 139 | +// } |
| 140 | +// String filename = videoUrl; |
| 141 | +// |
| 142 | +// |
| 143 | +// Date expiration = new Date(); |
| 144 | +// long expTime = expiration.getTime(); |
| 145 | +// expTime += TimeUnit.MINUTES.toMillis(30); // 30 minute |
| 146 | +// expiration.setTime(expTime); |
| 147 | +// |
| 148 | +// GetPresignedUrlRequest generatePresignedUrlRequest = new GetPresignedUrlRequest(bucket, fileName) |
| 149 | +// .withMethod(HttpMethod.GET) |
| 150 | +// .withExpiration(expiration); |
| 151 | +// |
| 152 | +// |
| 153 | +// return Collections.singletonMap("video", AWSS3ResDto.builder() |
| 154 | +// .url(s3Presigner.getPresignedUrl(generatePresignedUrlRequest).toString())); |
| 155 | +// } |
| 156 | +// |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +// public String getPresignedUrl(String originUrl) { |
| 161 | + // Presigned URL 생성 |
| 162 | + |
| 163 | + |
| 164 | +// GetPresignedUrlRequest getPresignedUrlRequest = s3Presigner.presignGetObject( |
| 165 | +// req -> req.signatureDuration(Duration.ofMinutes(15)) // 15분 유효기간 |
| 166 | +// .getObjectRequest( |
| 167 | +// GetObjectRequest.builder() |
| 168 | +// .bucket(bucketName) |
| 169 | +// .key(key) |
| 170 | +// .build() |
| 171 | +// ) |
| 172 | +// ) |
| 173 | + |
| 174 | +// } |
99 | 175 |
|
100 | 176 | // 출동, 미출동 상태 변경 |
101 | 177 | public Map<String, String> updateCaseState(int id, StateRequest request, HttpSession session) { |
|
0 commit comments