9
9
import lombok .extern .slf4j .Slf4j ;
10
10
import org .layer .domain .external .ncp .dto .NcpResponse ;
11
11
import org .layer .domain .external .ncp .enums .ImageDomain ;
12
+ import org .layer .domain .external .ncp .exception .ExternalException ;
12
13
import org .springframework .beans .factory .annotation .Value ;
13
14
import org .springframework .stereotype .Service ;
14
15
15
16
import java .util .Date ;
16
17
import java .util .UUID ;
17
18
19
+ import static org .layer .common .exception .ExternalExceptionType .OBJECT_INVALID_ERROR ;
20
+
18
21
@ Service
19
22
@ Slf4j
20
23
@ RequiredArgsConstructor
@@ -37,6 +40,15 @@ public NcpResponse.PresignedResult getPreSignedUrl(Long memberId, ImageDomain im
37
40
);
38
41
}
39
42
43
+ public void checkObjectExistOrThrow (String url ) {
44
+
45
+ String objectKey = extractObjectKey (url );
46
+ boolean isExist = amazonS3Client .doesObjectExist (bucket , objectKey );
47
+ if (!isExist ) {
48
+ throw new ExternalException (OBJECT_INVALID_ERROR );
49
+ }
50
+ }
51
+
40
52
private GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest (String fileName ) {
41
53
GeneratePresignedUrlRequest generatePresignedUrlRequest =
42
54
new GeneratePresignedUrlRequest (bucket , fileName )
@@ -57,4 +69,13 @@ private Date getPreSignedUrlExpiration() {
57
69
expiration .setTime (expTimeMillis );
58
70
return expiration ;
59
71
}
72
+
73
+ private String extractObjectKey (String url ) {
74
+ String expectedPrefix = "https://layer-bucket.kr.object.ncloudstorage.com" ;
75
+
76
+ if (!url .startsWith (expectedPrefix )) {
77
+ throw new ExternalException (OBJECT_INVALID_ERROR );
78
+ }
79
+ return url .substring (expectedPrefix .length () + 1 ); // "/" 이후부터 추출
80
+ }
60
81
}
0 commit comments