11package com .example .backend .dashboard .service ;
22
3+ import com .amazonaws .HttpMethod ;
4+ import com .amazonaws .services .s3 .AmazonS3 ;
5+ import com .amazonaws .services .s3 .model .GeneratePresignedUrlRequest ;
36import com .example .backend .common .domain .CaseEntity ;
47import com .example .backend .common .domain .CctvEntity ;
58import com .example .backend .common .domain .OfficeEntity ;
912import com .example .backend .search .repository .CctvRepository ;
1013import jakarta .persistence .EntityNotFoundException ;
1114import lombok .RequiredArgsConstructor ;
15+ import org .springframework .beans .factory .annotation .Value ;
1216import org .springframework .stereotype .Service ;
1317import org .springframework .transaction .annotation .Transactional ;
1418
1519import java .time .LocalDateTime ;
20+ import java .util .Date ;
21+ import java .util .concurrent .TimeUnit ;
1622
1723@ Service
1824@ Transactional
@@ -22,6 +28,10 @@ public class CaseDetectService {
2228 private final DashboardRepository caseRepository ;
2329 private final CctvRepository cctvRepository ; // CCTV 정보(주소) 조회용
2430
31+ @ Value ("${cloud.aws.bucket}" )
32+ private String bucket ;
33+ private final AmazonS3 s3Client ;
34+
2535 public CaseDetectResponse saveCase (CaseDetectRequest request ) {
2636 // 1) CCTV 정보 조회 (주소 포함)
2737 CctvEntity cctvEntity = cctvRepository .findById (request .getCctvId ())
@@ -48,7 +58,23 @@ public CaseDetectResponse saveCase(CaseDetectRequest request) {
4858
4959 CaseEntity saved = caseRepository .save (caseEntity );
5060
61+ CaseDetectResponse detect_case = CaseDetectResponse .fromEntity (saved );
62+
63+ // Presigned URL 유효기간 설정 (30분)
64+ Date expiration = new Date ();
65+ long expTime = expiration .getTime ();
66+ expTime += TimeUnit .MINUTES .toMillis (30 ); // 30 minute
67+ expiration .setTime (expTime );
68+
69+ GeneratePresignedUrlRequest presignRequest =
70+ new GeneratePresignedUrlRequest (bucket , detect_case .getVideo ())
71+ .withMethod (HttpMethod .GET )
72+ .withExpiration (expiration );
73+
74+ String url = s3Client .generatePresignedUrl (presignRequest ).toString ();
75+ detect_case .setVideo (url );
76+
5177 // 3) DTO 변환: 기존에 정의한 fromEntity 메서드를 사용하여 응답 객체 생성
52- return CaseDetectResponse . fromEntity ( saved ) ;
78+ return detect_case ;
5379 }
5480}
0 commit comments