Skip to content

Commit e20e5f0

Browse files
committed
refactor: 스터디룸 파일 업로드 s3 + fileAttachment + Mapping 제거 방식으로 수정
1 parent 7d4abc5 commit e20e5f0

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

src/main/java/com/back/domain/studyroom/service/RoomService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ public void updateRoomSettings(Long roomId, String title, String description,
346346
// 썸네일 변경 처리
347347
String thumbnailUrl = room.getRawThumbnailUrl(); // 기존 URL 유지
348348
if (thumbnailAttachmentId != null) {
349-
// 기존 매핑 삭제 + 새 매핑 생성
349+
// 기존 매핑 삭제 + 새 매핑 생성 (S3 파일도 삭제)
350350
thumbnailUrl = roomThumbnailService.updateThumbnailMapping(
351-
roomId, thumbnailAttachmentId);
351+
roomId, thumbnailAttachmentId, userId);
352352
}
353353

354354
room.updateSettings(title, description, maxParticipants, thumbnailUrl);
@@ -448,8 +448,8 @@ public void terminateRoom(Long roomId, Long userId) {
448448
throw new CustomException(ErrorCode.NOT_ROOM_MANAGER);
449449
}
450450

451-
// 썸네일 매핑 삭제
452-
roomThumbnailService.deleteThumbnailMapping(roomId);
451+
// 썸네일 매핑 삭제 (S3 파일 + FileAttachment + Mapping)
452+
roomThumbnailService.deleteThumbnailMapping(roomId, userId);
453453

454454
room.terminate();
455455

src/main/java/com/back/domain/studyroom/service/RoomThumbnailService.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.back.domain.file.entity.FileAttachment;
66
import com.back.domain.file.repository.AttachmentMappingRepository;
77
import com.back.domain.file.repository.FileAttachmentRepository;
8+
import com.back.domain.file.service.FileService;
89
import com.back.global.exception.CustomException;
910
import com.back.global.exception.ErrorCode;
1011
import lombok.RequiredArgsConstructor;
@@ -25,6 +26,7 @@ public class RoomThumbnailService {
2526

2627
private final FileAttachmentRepository fileAttachmentRepository;
2728
private final AttachmentMappingRepository attachmentMappingRepository;
29+
private final FileService fileService;
2830

2931
/**
3032
* 방 생성 시 썸네일 매핑 생성
@@ -60,42 +62,83 @@ public String createThumbnailMapping(Long roomId, Long thumbnailAttachmentId) {
6062

6163
/**
6264
* 방 수정 시 썸네일 변경
63-
* 1. 기존 매핑 삭제
65+
* 1. 기존 매핑 및 파일 삭제 (S3 + FileAttachment + Mapping)
6466
* 2. 새 매핑 생성
6567
*
6668
* @param roomId 방 ID
6769
* @param newThumbnailAttachmentId 새 썸네일 파일 ID (null이면 변경 없음)
70+
* @param userId 요청자 ID (파일 삭제 권한 검증용)
6871
* @return 새 썸네일 URL (null이면 변경 없음)
6972
*/
7073
@Transactional
71-
public String updateThumbnailMapping(Long roomId, Long newThumbnailAttachmentId) {
74+
public String updateThumbnailMapping(Long roomId, Long newThumbnailAttachmentId, Long userId) {
7275
if (newThumbnailAttachmentId == null) {
7376
// null이면 썸네일 변경 없음
7477
return null;
7578
}
7679

77-
// 기존 매핑 모두 삭제
80+
// 기존 매핑 및 파일 삭제 (S3 + FileAttachment 포함)
81+
List<AttachmentMapping> mappings = attachmentMappingRepository
82+
.findAllByEntityTypeAndEntityId(EntityType.STUDY_ROOM, roomId);
83+
84+
for (AttachmentMapping mapping : mappings) {
85+
FileAttachment oldAttachment = mapping.getFileAttachment();
86+
if (oldAttachment != null) {
87+
// FileService를 사용하여 S3 파일 + FileAttachment 삭제
88+
try {
89+
fileService.deleteFile(oldAttachment.getId(), userId);
90+
log.info("기존 썸네일 파일 삭제 - RoomId: {}, AttachmentId: {}",
91+
roomId, oldAttachment.getId());
92+
} catch (Exception e) {
93+
log.error("썸네일 파일 삭제 실패 - RoomId: {}, AttachmentId: {}, Error: {}",
94+
roomId, oldAttachment.getId(), e.getMessage());
95+
}
96+
}
97+
}
98+
99+
// 매핑 삭제
78100
attachmentMappingRepository.deleteAllByEntityTypeAndEntityId(
79101
EntityType.STUDY_ROOM, roomId);
80102

81-
log.info("기존 썸네일 매핑 삭제 - RoomId: {}", roomId);
103+
log.info("기존 썸네일 매핑 및 파일 삭제 완료 - RoomId: {}", roomId);
82104

83105
// 새 매핑 생성
84106
return createThumbnailMapping(roomId, newThumbnailAttachmentId);
85107
}
86108

87109
/**
88-
* 방 삭제 시 썸네일 매핑 삭제
110+
* 방 삭제 시 썸네일 매핑 및 파일 삭제
111+
* S3 파일 + FileAttachment + AttachmentMapping 모두 삭제
89112
*
90113
* @param roomId 방 ID
114+
* @param userId 요청자 ID (파일 삭제 권한 검증용)
91115
*/
92116
@Transactional
93-
public void deleteThumbnailMapping(Long roomId) {
94-
// 연결된 파일 매핑 모두 삭제
117+
public void deleteThumbnailMapping(Long roomId, Long userId) {
118+
// 매핑 조회
119+
List<AttachmentMapping> mappings = attachmentMappingRepository
120+
.findAllByEntityTypeAndEntityId(EntityType.STUDY_ROOM, roomId);
121+
122+
// S3 파일 + FileAttachment 삭제
123+
for (AttachmentMapping mapping : mappings) {
124+
FileAttachment fileAttachment = mapping.getFileAttachment();
125+
if (fileAttachment != null) {
126+
try {
127+
fileService.deleteFile(fileAttachment.getId(), userId);
128+
log.info("썸네일 파일 삭제 완료 - RoomId: {}, AttachmentId: {}",
129+
roomId, fileAttachment.getId());
130+
} catch (Exception e) {
131+
log.error("썸네일 파일 삭제 실패 - RoomId: {}, AttachmentId: {}, Error: {}",
132+
roomId, fileAttachment.getId(), e.getMessage());
133+
}
134+
}
135+
}
136+
137+
// AttachmentMapping 삭제
95138
attachmentMappingRepository.deleteAllByEntityTypeAndEntityId(
96139
EntityType.STUDY_ROOM, roomId);
97140

98-
log.info("방 삭제 - 썸네일 매핑 삭제 완료 - RoomId: {}", roomId);
141+
log.info("방 삭제 - 썸네일 매핑 및 파일 삭제 완료 - RoomId: {}", roomId);
99142
}
100143

101144
/**

src/main/resources/application.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ webrtc:
123123
- urls: stun:stun1.l.google.com:19302
124124
- urls: stun:stun2.l.google.com:19302
125125
turn:
126-
shared-secret: "${WEBRTC_TURN_SHARED_SECRET}"
127-
server-ip: "${WEBRTC_TURN_SERVER_IP}"
126+
shared-secret: "${WEBRTC_TURN_SHARED_SECRET:default-secret-key}"
127+
server-ip: "${WEBRTC_TURN_SERVER_IP:127.0.0.1}"
128128
ttl-seconds: 3600
129129

130130
# 스터디룸 설정

src/test/java/com/back/domain/studyroom/service/RoomServiceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void updateRoomSettings_Success() {
313313
assertThat(testRoom.getTitle()).isEqualTo("변경된 제목");
314314
assertThat(testRoom.getDescription()).isEqualTo("변경된 설명");
315315
assertThat(testRoom.getMaxParticipants()).isEqualTo(15);
316-
verify(roomThumbnailService, never()).updateThumbnailMapping(any(), any());
316+
verify(roomThumbnailService, never()).updateThumbnailMapping(any(), any(), any()); // userId 파라미터 추가
317317
}
318318

319319
@Test
@@ -348,7 +348,7 @@ void terminateRoom_Success() {
348348
// then
349349
assertThat(testRoom.getStatus()).isEqualTo(RoomStatus.TERMINATED);
350350
assertThat(testRoom.isActive()).isFalse();
351-
verify(roomThumbnailService, times(1)).deleteThumbnailMapping(1L);
351+
verify(roomThumbnailService, times(1)).deleteThumbnailMapping(1L, 1L); // userId 파라미터 추가
352352
}
353353

354354
@Test
@@ -617,7 +617,7 @@ void updateRoomSettings_WithThumbnailChange() {
617617
// given
618618
given(roomRepository.findById(1L)).willReturn(Optional.of(testRoom));
619619
given(roomParticipantService.getParticipantCount(1L)).willReturn(0L);
620-
given(roomThumbnailService.updateThumbnailMapping(eq(1L), eq(789L)))
620+
given(roomThumbnailService.updateThumbnailMapping(eq(1L), eq(789L), eq(1L))) // userId 파라미터 추가
621621
.willReturn("https://s3.amazonaws.com/bucket/new-thumbnail.jpg");
622622

623623
// when
@@ -633,6 +633,6 @@ void updateRoomSettings_WithThumbnailChange() {
633633
// then
634634
assertThat(testRoom.getTitle()).isEqualTo("변경된 제목");
635635
assertThat(testRoom.getThumbnailUrl()).isEqualTo("https://s3.amazonaws.com/bucket/new-thumbnail.jpg");
636-
verify(roomThumbnailService, times(1)).updateThumbnailMapping(eq(1L), eq(789L));
636+
verify(roomThumbnailService, times(1)).updateThumbnailMapping(eq(1L), eq(789L), eq(1L)); // userId 파라미터 추가
637637
}
638638
}

0 commit comments

Comments
 (0)