-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from kjungw1025/feat/chatting
feat: 채팅방 이미지 업로드, 다운로드 기능 추가 및 버그 수정
- Loading branch information
Showing
26 changed files
with
632 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/dku/council/domain/chat/controller/ChatFileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.dku.council.domain.chat.controller; | ||
|
||
import com.dku.council.domain.chat.exception.InvalidChatRoomUserException; | ||
import com.dku.council.domain.chat.model.dto.request.RequestChatFileDto; | ||
import com.dku.council.domain.chat.service.ChatService; | ||
import com.dku.council.global.auth.jwt.AppAuthentication; | ||
import com.dku.council.global.auth.role.UserAuth; | ||
import com.dku.council.infra.nhn.global.service.service.NHNAuthService; | ||
import com.dku.council.infra.nhn.s3.model.ChatUploadedImage; | ||
import com.dku.council.infra.nhn.s3.model.ImageRequest; | ||
import com.dku.council.infra.nhn.s3.service.ChatImageUploadService; | ||
import com.dku.council.infra.nhn.s3.service.ObjectDownloadService; | ||
import com.dku.council.infra.nhn.s3.service.ObjectStorageService; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.parameters.P; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
|
||
@Tag(name = "채팅방 파일", description = "채팅방 파일/이미지 업로드 및 다운로드 관련 api") | ||
@RestController | ||
@RequestMapping("/chat") | ||
@RequiredArgsConstructor | ||
public class ChatFileController { | ||
|
||
private final ChatService chatService; | ||
private final ChatImageUploadService chatImageUploadService; | ||
private final ObjectDownloadService objectDownloadService; | ||
|
||
private final NHNAuthService nhnAuthService; | ||
private final ObjectStorageService objectStorageService; | ||
|
||
// @PostMapping("/file/upload") | ||
|
||
/** | ||
* 이미지 업로드 기능 | ||
* | ||
* @param request roomId와 전송할 이미지 파일에 대한 dto | ||
*/ | ||
@PostMapping(value = "/image/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
@UserAuth | ||
public List<ChatUploadedImage> uploadImage(AppAuthentication auth, | ||
@Valid @ModelAttribute RequestChatFileDto request) { | ||
// 채팅방에 현재 참여중인 유저가 아니면, 해당 채팅방에 이미지 업로드를 할 수 없게 | ||
if (chatService.alreadyInRoom(request.getRoomId(), auth.getUserId())) { | ||
return chatImageUploadService.newContext().uploadChatImages( | ||
ImageRequest.ofList(request.getFiles()), | ||
request.getRoomId(), | ||
auth.getUserId()); | ||
} else { | ||
throw new InvalidChatRoomUserException(); | ||
} | ||
} | ||
|
||
@GetMapping("/download/{fileName}") | ||
@UserAuth | ||
public ResponseEntity<byte[]> download(AppAuthentication auth, | ||
@PathVariable String fileName, | ||
@RequestParam("roomId") String roomId, | ||
@RequestParam("fileUrl") String fileUrl) { | ||
if (chatService.alreadyInRoom(roomId, auth.getUserId())) { | ||
return objectDownloadService.downloadObject(fileName, fileUrl); | ||
} else { | ||
throw new InvalidChatRoomUserException(); | ||
} | ||
} | ||
|
||
// TODO : 파일 삭제시, chatRoomMessage에도 삭제된게 반영 되어야함 | ||
// @DeleteMapping("/file/delete") | ||
// public void deleteFile(@RequestParam("roomId") String roomId, | ||
// @RequestParam("fileUrl") String fileUrl) { | ||
// objectStorageService.deleteChatFileByDirectUrl(nhnAuthService.requestToken(), fileUrl); | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/dku/council/domain/chat/exception/InvalidChatRoomUserException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.dku.council.domain.chat.exception; | ||
|
||
import com.dku.council.global.error.exception.LocalizedMessageException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class InvalidChatRoomUserException extends LocalizedMessageException { | ||
public InvalidChatRoomUserException() { super(HttpStatus.BAD_REQUEST, "notfound.chat-room-user"); } | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/dku/council/domain/chat/model/FileType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.dku.council.domain.chat.model; | ||
|
||
public enum FileType { | ||
/** | ||
* 이미지 | ||
*/ | ||
IMAGE, | ||
|
||
/** | ||
* 파일 | ||
*/ | ||
FILE, | ||
|
||
/** | ||
* 일반 메시지 형태일 경우 | ||
*/ | ||
NONE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/dku/council/domain/chat/model/dto/request/RequestChatFileDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.dku.council.domain.chat.model.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Getter | ||
public class RequestChatFileDto { | ||
|
||
@NotBlank | ||
@Schema(description = "채팅방 번호", example = "d118101z-c737-4253-9911-ea2579405f42") | ||
private final String roomId; | ||
|
||
@Schema(description = "첨부 파일 목록") | ||
private final List<MultipartFile> files; | ||
|
||
public RequestChatFileDto(String roomId, List<MultipartFile> files) { | ||
this.roomId = roomId; | ||
this.files = Objects.requireNonNullElseGet(files, ArrayList::new); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/dku/council/domain/chat/service/ChatFileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.dku.council.domain.chat.service; | ||
|
||
import com.dku.council.domain.chatmessage.model.entity.ChatRoomMessage; | ||
import com.dku.council.domain.chatmessage.repository.ChatRoomMessageRepository; | ||
import com.dku.council.infra.nhn.global.service.service.NHNAuthService; | ||
import com.dku.council.infra.nhn.s3.service.ObjectStorageService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ChatFileService { | ||
|
||
private final NHNAuthService nhnAuthService; | ||
private final ObjectStorageService objectStorageService; | ||
|
||
private final ChatRoomMessageRepository chatRoomMessageRepository; | ||
|
||
public void deleteAllFilesInChatRoom(String roomId) { | ||
List<ChatRoomMessage> imageMessageList = chatRoomMessageRepository.findAllByRoomIdAndFileType(roomId, "IMAGE"); | ||
List<ChatRoomMessage> fileMessageList = chatRoomMessageRepository.findAllByRoomIdAndFileType(roomId, "FILE"); | ||
|
||
if (!imageMessageList.isEmpty()) { | ||
for (ChatRoomMessage chatRoomMessage : imageMessageList) { | ||
objectStorageService.deleteChatFileByDirectUrl(nhnAuthService.requestToken(), chatRoomMessage.getFileUrl()); | ||
} | ||
} | ||
|
||
if (!fileMessageList.isEmpty()) { | ||
for (ChatRoomMessage chatRoomMessage : fileMessageList) { | ||
objectStorageService.deleteChatFileByDirectUrl(nhnAuthService.requestToken(), chatRoomMessage.getFileUrl()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.