66
77import com ._oormthon .seasonthon .domain .member .dto .res .UserResponse ;
88import com ._oormthon .seasonthon .domain .member .service .UserService ;
9+ import com ._oormthon .seasonthon .domain .s3 .dto .PresignedUrlResponse ;
910
1011import software .amazon .awssdk .auth .credentials .DefaultCredentialsProvider ;
1112import software .amazon .awssdk .regions .Region ;
@@ -30,42 +31,34 @@ public class S3Service {
3031 /**
3132 * S3 Presigned URL (PUT) 발급
3233 */
33- public String generateUploadPresignedUrl (Long userId ) {
34+ public PresignedUrlResponse generateUploadPresignedUrl (Long userId , String fileName , String fileType ) {
3435 UserResponse user = userService .getUserById (userId );
3536
36- // URL에서 파일명만 추출
37- String profileImageName = extractFileNameFromUrl (user .profileImage ());
38-
3937 // 고유한 S3 키 생성 (닉네임/UUID-파일명)
40- String key = user .nickname () + "/" + UUID .randomUUID () + "-" + profileImageName ;
41-
42- // 확장자 기반 Content-Type 결정
43- String contentType = determineContentTypeFromKey (profileImageName );
38+ String key = user .nickname () + "/" + UUID .randomUUID () + "-" + fileName ;
4439
4540 // Presigned URL 발급용 요청 생성
4641 PutObjectRequest objectRequest = PutObjectRequest .builder ()
4742 .bucket (bucket )
4843 .key (key )
49- .contentType (contentType )
44+ .contentType (fileType )
5045 .build ();
5146
5247 // Presigned URL 생성
5348 PresignedPutObjectRequest presignedRequest = s3Presigner .presignPutObject (
5449 r -> r .putObjectRequest (objectRequest )
5550 .signatureDuration (Duration .ofMinutes (10 )));
5651
57- return presignedRequest .url ().toString ();
52+ // JSON 형태로 리턴
53+ return new PresignedUrlResponse (presignedRequest .url ().toString (), key );
5854 }
5955
6056 /**
6157 * S3 객체 삭제
6258 */
63- public void deleteFile (Long userId ) {
64- UserResponse user = userService .getUserById (userId );
59+ public void deleteFile (Long userId , String fileName ) {
6560
66- // URL에서 파일명만 추출
67- String profileImageName = extractFileNameFromUrl (user .profileImage ());
68- String key = user .nickname () + "/" + profileImageName ;
61+ String key = userService .getUserById (userId ).nickname () + "/" + fileName ;
6962
7063 S3Client s3 = S3Client .builder ()
7164 .region (Region .AP_NORTHEAST_2 )
@@ -81,37 +74,37 @@ public void deleteFile(Long userId) {
8174 /**
8275 * 파일명에서 Content-Type 추론
8376 */
84- private String determineContentTypeFromKey (String key ) {
85- if (key == null )
86- return "application/octet-stream" ; // 기본값
87-
88- String lowerKey = key .toLowerCase ();
89-
90- if (lowerKey .endsWith (".jpg" ) || lowerKey .endsWith (".jpeg" ))
91- return "image/jpeg" ;
92- if (lowerKey .endsWith (".png" ))
93- return "image/png" ;
94- if (lowerKey .endsWith (".gif" ))
95- return "image/gif" ;
96- if (lowerKey .endsWith (".webp" ))
97- return "image/webp" ;
98- if (lowerKey .endsWith (".svg" ))
99- return "image/svg+xml" ;
100-
101- return "application/octet-stream" ; // 모르는 확장자일 경우
102- }
77+ // private String determineContentTypeFromKey(String key) {
78+ // if (key == null)
79+ // return "application/octet-stream"; // 기본값
80+
81+ // String lowerKey = key.toLowerCase();
82+
83+ // if (lowerKey.endsWith(".jpg") || lowerKey.endsWith(".jpeg"))
84+ // return "image/jpeg";
85+ // if (lowerKey.endsWith(".png"))
86+ // return "image/png";
87+ // if (lowerKey.endsWith(".gif"))
88+ // return "image/gif";
89+ // if (lowerKey.endsWith(".webp"))
90+ // return "image/webp";
91+ // if (lowerKey.endsWith(".svg"))
92+ // return "image/svg+xml";
93+
94+ // return "application/octet-stream"; // 모르는 확장자일 경우
95+ // }
10396
10497 /**
10598 * URL에서 파일명만 추출
10699 * 예: https://t1.kakaocdn.net/account_images/default_profile.jpeg →
107100 * default_profile.jpeg
108101 */
109- private String extractFileNameFromUrl (String url ) {
110- if (url == null )
111- return "unknown" ;
112- int lastSlash = url .lastIndexOf ('/' );
113- if (lastSlash == -1 || lastSlash == url .length () - 1 )
114- return "unknown" ;
115- return url .substring (lastSlash + 1 );
116- }
102+ // private String extractFileNameFromUrl(String url) {
103+ // if (url == null)
104+ // return "unknown";
105+ // int lastSlash = url.lastIndexOf('/');
106+ // if (lastSlash == -1 || lastSlash == url.length() - 1)
107+ // return "unknown";
108+ // return url.substring(lastSlash + 1);
109+ // }
117110}
0 commit comments