Skip to content

Commit 55e4057

Browse files
committed
Merge branch 'dev' of https://github.com/CommitField/commitField into feat/#106
2 parents 7f424df + 6f99f4a commit 55e4057

File tree

14 files changed

+183
-55
lines changed

14 files changed

+183
-55
lines changed

โ€Ž.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
aws-region: ${{ secrets.AWS_REGION }}
7777
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
7878
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
79-
instance-ids: "i-0e27758b937bcc047"
79+
instance-ids: "i-050af13cf2c3bc98a"
8080
working-directory: /
8181
comment: Deploy
8282
command: |

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatMessage/controller/response/ChatMsgResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@AllArgsConstructor
1111
@Builder
1212
public class ChatMsgResponse {
13+
//์ฑ„ํŒ…๋ฐฉ ID
1314
private Long roomId;
1415
//์‚ฌ์šฉ์ž(user)
1516
private String from;

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatMessage/service/ChatMessageServiceImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class ChatMessageServiceImpl implements ChatMessageService {
3333
private final ChatMessageCustomRepository chatMessageCustomRepository;
3434
private final UserChatRoomRepository userChatRoomRepository;
3535

36+
// ์ปค๋ฐ‹์šฉ ์ฃผ์„
37+
3638
@Override
3739
@Transactional
3840
public ChatMsgResponse sendMessage(ChatMsgRequest message, Long userId, Long roomId) {
@@ -48,17 +50,23 @@ public ChatMsgResponse sendMessage(ChatMsgRequest message, Long userId, Long roo
4850
.user(findUser)
4951
.chatRoom(chatRoom)
5052
.build();
51-
// Response
53+
54+
// Response message
55+
// ์‘๋‹ต ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜
56+
5257
ChatMsgResponse response = ChatMsgResponse.builder()
58+
5359
.roomId(roomId)
5460
.from(findUser.getNickname())
5561
.message(message.getMessage())
5662
.sendAt(chatMsg.getCreatedAt())
5763
.build();
5864
chatMessageRepository.save(chatMsg);
5965
return response;
66+
6067
}
6168

69+
6270
@Transactional(readOnly = true)
6371
@Override
6472
public List<ChatMsgDto> getRoomChatMsgList(Long roomId, Long userId, Long lastId) {
@@ -86,4 +94,4 @@ public List<ChatMsgDto> getRoomChatMsgList(Long roomId, Long userId, Long lastId
8694
}
8795
return chatMsgDtos;
8896
}
89-
}
97+
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/controller/ChatRoomController.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cmf.commitField.domain.chat.chatRoom.controller;
22

33
import cmf.commitField.domain.File.service.FileService;
4+
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomJoinRequest;
45
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest;
56
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest;
67
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
78
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomUserDto;
89
import cmf.commitField.domain.chat.chatRoom.service.ChatRoomService;
910
import cmf.commitField.domain.user.entity.CustomOAuth2User;
11+
import cmf.commitField.global.aws.s3.S3Service;
1012
import cmf.commitField.global.error.ErrorCode;
1113
import cmf.commitField.global.globalDto.GlobalResponse;
1214
import cmf.commitField.global.security.LoginCheck;
@@ -28,25 +30,25 @@
2830
@RequestMapping("/chat")
2931
public class ChatRoomController {
3032
private final ChatRoomService chatRoomService;
31-
private final FileService fileService;
33+
private final S3Service s3Service; // S3 ํŒŒ์ผ ์ €์žฅ์„ ์œ„ํ•œ ์„œ๋น„์Šค
34+
private final FileService fileService; //local file ์ €์žฅ์„ ์œ„ํ•œ ์„œ๋น„์Šค
3235

3336
// ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ (ํŒŒ์ผ ์—…๋กœ๋“œ ํฌํ•จ)
3437
@PostMapping(value = "/room", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
3538
public GlobalResponse<Object> createRoom(
3639
@ModelAttribute @Valid ChatRoomRequest chatRoomRequest) throws IOException {
3740

38-
3941
// ์ธ์ฆ ํ™•์ธ
4042
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
4143

4244
if (authentication instanceof OAuth2AuthenticationToken) {
4345
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
44-
Long userId = principal.getId(); // getId()๋ฅผ ํ†ตํ•ด userId๋ฅผ ์ถ”์ถœ
46+
Long userId = principal.getId(); // getId()๋ฅผ ํ†ตํ•ด userId ์ถ”์ถœ
4547

4648
// ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ
4749
String imageUrl = null;
4850
if (chatRoomRequest.getFile() != null && !chatRoomRequest.getFile().isEmpty()) {
49-
imageUrl = fileService.saveFile(chatRoomRequest.getFile()); // ํŒŒ์ผ ์ €์žฅ
51+
imageUrl = s3Service.uploadFile(chatRoomRequest.getFile(), "chat-room"); // S3์— ์—…๋กœ๋“œ
5052
}
5153

5254
// ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ ์„œ๋น„์Šค ํ˜ธ์ถœ (์ด๋ฏธ์ง€ URL ํฌํ•จ)
@@ -58,15 +60,16 @@ public GlobalResponse<Object> createRoom(
5860
}
5961
}
6062

63+
6164
//์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ
6265
@PostMapping("/room/join/{roomId}")
63-
public GlobalResponse<Object> joinRoom(@PathVariable Long roomId, @RequestBody ChatRoomRequest chatRoomRequest) {
66+
public GlobalResponse<Object> joinRoom(@PathVariable Long roomId, @RequestBody ChatRoomJoinRequest chatRoomJoinRequest) {
6467
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
6568

6669
if (authentication instanceof OAuth2AuthenticationToken) {
6770
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
6871
Long userId = principal.getId(); // getId()๋ฅผ ํ†ตํ•ด userId๋ฅผ ์ถ”์ถœ
69-
chatRoomService.joinRoom(roomId, userId, chatRoomRequest); // userId๋ฅผ ์ „๋‹ฌ
72+
chatRoomService.joinRoom(roomId, userId, chatRoomJoinRequest); // userId๋ฅผ ์ „๋‹ฌ
7073
return GlobalResponse.success("ํ•ด๋‹น ์ฑ„ํŒ…๋ฐฉ์— ์ž…์žฅํ•˜์…จ์Šต๋‹ˆ๋‹ค");
7174
} else {
7275
throw new IllegalArgumentException("๋กœ๊ทธ์ธ ํ›„์— ์ด์šฉํ•ด ์ฃผ์„ธ์š”.");
@@ -253,8 +256,33 @@ public GlobalResponse<Object> searchRoomName(
253256
}
254257
}
255258

256-
257-
259+
// // ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ (ํŒŒ์ผ ์—…๋กœ๋“œ ํฌํ•จ)
260+
// @PostMapping(value = "/room", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
261+
// public GlobalResponse<Object> createRoom(
262+
// @ModelAttribute @Valid ChatRoomRequest chatRoomRequest) throws IOException {
263+
//
264+
//
265+
// // ์ธ์ฆ ํ™•์ธ
266+
// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
267+
//
268+
// if (authentication instanceof OAuth2AuthenticationToken) {
269+
// CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
270+
// Long userId = principal.getId(); // getId()๋ฅผ ํ†ตํ•ด userId๋ฅผ ์ถ”์ถœ
271+
//
272+
// // ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ
273+
// String imageUrl = null;
274+
// if (chatRoomRequest.getFile() != null && !chatRoomRequest.getFile().isEmpty()) {
275+
// imageUrl = fileService.saveFile(chatRoomRequest.getFile()); // ํŒŒ์ผ ์ €์žฅ
276+
// }
277+
//
278+
// // ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ ์„œ๋น„์Šค ํ˜ธ์ถœ (์ด๋ฏธ์ง€ URL ํฌํ•จ)
279+
// chatRoomService.createRoom(chatRoomRequest, userId, imageUrl);
280+
//
281+
// return GlobalResponse.success("์ฑ„ํŒ…๋ฐฉ์„ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
282+
// } else {
283+
// throw new IllegalArgumentException("๋กœ๊ทธ์ธ ํ›„์— ์ด์šฉํ•ด ์ฃผ์„ธ์š”.");
284+
// }
285+
// }
258286

259287

260288
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmf.commitField.domain.chat.chatRoom.controller.request;
2+
3+
import jakarta.validation.constraints.Max;
4+
import jakarta.validation.constraints.NotNull;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import org.hibernate.validator.constraints.Length;
8+
9+
@Getter
10+
@Setter
11+
public class ChatRoomJoinRequest {
12+
@NotNull
13+
@Length(min = 2, max = 20)
14+
private String title;
15+
16+
@NotNull
17+
@Max(100)
18+
private Integer userCountMax;
19+
20+
@Length(min = 4, max = 20)
21+
private String password;
22+
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/controller/request/ChatRoomRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class ChatRoomRequest {
2828

2929
private MultipartFile file; // ํŒŒ์ผ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ํ•„๋“œ
3030

31-
public boolean isFileSizeValid() {
32-
return file == null || file.getSize() <= 5 * 1024 * 1024; // 5MB
33-
}
31+
// public boolean isFileSizeValid() {
32+
// return file == null || file.getSize() <= 5 * 1024 * 1024; // 5MB
33+
// }
3434

3535
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cmf.commitField.domain.chat.chatRoom.service;
22

3+
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomJoinRequest;
34
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest;
45
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest;
56
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
@@ -34,7 +35,7 @@ public interface ChatRoomService {
3435

3536
List<ChatRoomDto> myHeartRoomList(Long userId, Pageable pageable);
3637

37-
void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest);
38+
void joinRoom(Long roomId, Long userId, ChatRoomJoinRequest chatRoomJoinRequest);
3839

3940
List<ChatRoomDto> searchRoomByTitle(String roomName, Long userId, Pageable pageable);
4041
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmf.commitField.domain.chat.chatRoom.service;
22

33
import cmf.commitField.domain.chat.chatMessage.repository.ChatMessageRepository;
4+
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomJoinRequest;
45
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest;
56
import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest;
67
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
@@ -72,6 +73,7 @@ public void createRoom(ChatRoomRequest chatRoomRequest, Long userId, String imag
7273
.imageUrl(imageUrl) // ์ด๋ฏธ์ง€ URL ์ถ”๊ฐ€
7374
.build();
7475
if (password != null) {
76+
System.out.println("Setting password: " + password);
7577
chatRoom.setPassword(password);
7678
chatRoom.setIsPrivate(true);
7779
}
@@ -127,7 +129,7 @@ public List<ChatRoomDto> getUserByRoomPartList(Long userId, Pageable pageable) {
127129

128130
@Override
129131
@Transactional
130-
public void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest) {
132+
public void joinRoom(Long roomId, Long userId, ChatRoomJoinRequest chatRoomJoinRequest) {
131133
RLock lock = redissonClient.getLock("joinRoomLock:" + roomId);
132134
try {
133135
boolean available = lock.tryLock(1, TimeUnit.SECONDS);
@@ -145,13 +147,13 @@ public void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest)
145147
// user_chatroom ํ˜„์žฌ ์ธ์› ์นด์šดํŠธ (๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง)
146148
Long currentUserCount = userChatRoomRepository.countNonLockByChatRoomId(roomId); // lock (๊ธฐ์กด)
147149

148-
if (chatRoom.getIsPrivate() && chatRoomRequest.getPassword() == null) {
150+
if (chatRoom.getIsPrivate() && chatRoomJoinRequest.getPassword() == null) {
149151
throw new CustomException(ErrorCode.NEED_TO_PASSWORD);
150152

151153

152154

153155
}
154-
if (chatRoom.getIsPrivate() && !chatRoomRequest.getPassword().equals(chatRoom.getPassword())) {
156+
if (chatRoom.getIsPrivate() && !chatRoomJoinRequest.getPassword().equals(chatRoom.getPassword())) {
155157
throw new CustomException(ErrorCode.ROOM_PASSWORD_MISMATCH);
156158
}
157159
List<Long> userChatRoomByChatRoomId = userChatRoomRepository
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package cmf.commitField.global.aws.s3;
22

3-
import jakarta.annotation.PreDestroy;
43
import org.springframework.stereotype.Component;
54
import software.amazon.awssdk.services.s3.S3Client;
6-
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
7-
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
8-
import software.amazon.awssdk.services.s3.model.S3Object;
95

106
@Component
117
public class S3Cleaner {
@@ -17,21 +13,21 @@ public S3Cleaner(S3Client s3Client) {
1713
this.s3Client = s3Client;
1814
}
1915

20-
@PreDestroy
21-
public void cleanup() {
22-
// S3์—์„œ ํ…Œ์ŠคํŠธ ํŒŒ์ผ ์‚ญ์ œ
23-
ListObjectsV2Request listObjects = ListObjectsV2Request.builder()
24-
.bucket(bucketName)
25-
.build();
26-
27-
// ํŒŒ์ผ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
28-
ListObjectsV2Response listObjectsResponse = s3Client.listObjectsV2(listObjects);
29-
30-
// ๊ฐ ํŒŒ์ผ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
31-
for (S3Object s3Object : listObjectsResponse.contents()) {
32-
s3Client.deleteObject(builder -> builder.bucket(bucketName).key(s3Object.key()));
33-
}
34-
35-
System.out.println("S3์— ์žˆ๋Š” ํ…Œ์ŠคํŠธ ํŒŒ์ผ๋“ค์„ ์‚ญ์ œํ–ˆ์Šต๋‹ˆ๋‹ค.");
36-
}
16+
// @PreDestroy
17+
// public void cleanup() {
18+
// // S3์—์„œ ํ…Œ์ŠคํŠธ ํŒŒ์ผ ์‚ญ์ œ
19+
// ListObjectsV2Request listObjects = ListObjectsV2Request.builder()
20+
// .bucket(bucketName)
21+
// .build();
22+
//
23+
// // ํŒŒ์ผ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
24+
// ListObjectsV2Response listObjectsResponse = s3Client.listObjectsV2(listObjects);
25+
//
26+
// // ๊ฐ ํŒŒ์ผ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
27+
// for (S3Object s3Object : listObjectsResponse.contents()) {
28+
// s3Client.deleteObject(builder -> builder.bucket(bucketName).key(s3Object.key()));
29+
// }
30+
//
31+
// System.out.println("S3์— ์žˆ๋Š” ํ…Œ์ŠคํŠธ ํŒŒ์ผ๋“ค์„ ์‚ญ์ œํ–ˆ์Šต๋‹ˆ๋‹ค.");
32+
// }
3733
}
Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmf.commitField.global.aws.s3;
22

33
import lombok.RequiredArgsConstructor;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46
import org.springframework.stereotype.Service;
57
import org.springframework.web.multipart.MultipartFile;
68
import software.amazon.awssdk.core.sync.RequestBody;
@@ -11,28 +13,55 @@
1113
import java.io.IOException;
1214
import java.util.UUID;
1315

16+
1417
@Service
1518
@RequiredArgsConstructor
1619
public class S3Service {
1720
private final S3Client s3Client;
1821
private static final String BUCKET_NAME = "cmf-bucket-dev-seoyeon-1";
1922
private static final String REGION = "ap-northeast-2";
23+
private static final long MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
24+
private final Logger logger = LoggerFactory.getLogger(S3Service.class.getName());
2025

2126
// ํŒŒ์ผ ์—…๋กœ๋“œ ๊ธฐ๋Šฅ
2227
public String uploadFile(MultipartFile file, String dirName) throws IOException {
23-
String fileName = dirName + "/" + UUID.randomUUID() + "_" + file.getOriginalFilename();
24-
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
25-
.bucket(BUCKET_NAME)
26-
.key(fileName)
27-
.contentType(file.getContentType())
28-
.build();
28+
try {
29+
30+
// ํŒŒ์ผ ํฌ๊ธฐ ๊ฒ€์ฆ
31+
validateFileSize(file);
2932

30-
s3Client.putObject(putObjectRequest,
31-
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
33+
// UUID๋กœ ๊ณ ์œ ํ•œ ํŒŒ์ผ๋ช… ์ƒ์„ฑ
34+
String fileName = dirName + "/" + UUID.randomUUID() + "_" + file.getOriginalFilename();
35+
// PutObjectRequest ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
36+
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
37+
.bucket(BUCKET_NAME)
38+
.key(fileName)
39+
.contentType(file.getContentType())
40+
// .acl(ObjectCannedACL.PUBLIC_READ) // Public Read ๊ถŒํ•œ ์ถ”๊ฐ€
41+
.build();
42+
// ํŒŒ์ผ S3์— ์—…๋กœ๋“œ
43+
s3Client.putObject(putObjectRequest,
44+
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
45+
logger.info("ํŒŒ์ผ ์—…๋กœ๋“œ ์„ฑ๊ณต");
3246

33-
return "https://" + BUCKET_NAME + ".s3." + REGION + ".amazonaws.com/" + fileName;
47+
// S3 ํŒŒ์ผ URL ๋ฐ˜ํ™˜
48+
return "https://" + BUCKET_NAME + ".s3." + REGION + ".amazonaws.com/" + fileName;
49+
50+
} catch (IOException e) {
51+
logger.error("ํŒŒ์ผ ์—…๋กœ๋“œ ์‹คํŒจ: {}", e.getMessage());
52+
throw new IOException("ํŒŒ์ผ ์—…๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ", e);
53+
}
54+
}
55+
56+
// ํŒŒ์ผ ํฌ๊ธฐ ๊ฒ€์ฆ ๋ฉ”์„œ๋“œ
57+
private void validateFileSize(MultipartFile file) throws IOException {
58+
if (file.getSize() > MAX_FILE_SIZE) {
59+
throw new IOException("ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ 5MB๋ฅผ ์ดˆ๊ณผํ•˜์—ฌ ์—…๋กœ๋“œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
60+
}
3461
}
3562

63+
64+
3665
// ํŒŒ์ผ ์‚ญ์ œ ๊ธฐ๋Šฅ
3766
public void deleteFile(String fileName) {
3867
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
@@ -41,5 +70,6 @@ public void deleteFile(String fileName) {
4170
.build();
4271

4372
s3Client.deleteObject(deleteObjectRequest);
73+
logger.info("ํŒŒ์ผ ์‚ญ์ œ ์„ฑ๊ณต: {}", fileName);
4474
}
4575
}

0 commit comments

Comments
ย (0)