Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix#67 fix s3 error #68

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/kdt/web_ide/chat/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.v3.oas.annotations.Operation;
import kdt.web_ide.chat.dto.request.ChatMessageRequestDto;
import kdt.web_ide.chat.dto.response.GetChatMessageResponseDto;
import kdt.web_ide.chat.service.ChatService;
Expand Down Expand Up @@ -40,4 +42,15 @@ public ResponseEntity<List<GetChatMessageResponseDto>> getChatMessage(
Long memberId = memberService.getMember(userDetails.getMember()).getMemberId();
return ResponseEntity.status(HttpStatus.OK).body(chatService.getChatMessage(roomId, memberId));
}

@PostMapping(value = "/{roomId}/images", consumes = "multipart/form-data")
@Operation(summary = "이미지 전송")
public ResponseEntity<Void> sendImages(
@PathVariable("roomId") Long roomId,
@RequestPart("imageFile") List<MultipartFile> imageFiles,
@AuthenticationPrincipal CustomUserDetails userDetails) {
Long memberId = userDetails.getMember().getMemberId();
chatService.sendImage(memberId, roomId, imageFiles);
return ResponseEntity.status(HttpStatus.OK).body(null);
}
}
25 changes: 25 additions & 0 deletions src/main/java/kdt/web_ide/chat/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import kdt.web_ide.chat.dto.request.ChatMessageRequestDto;
import kdt.web_ide.chat.dto.response.GetChatMessageResponseDto;
Expand All @@ -19,6 +20,7 @@
import kdt.web_ide.chat.entity.repository.ChatRoomRepository;
import kdt.web_ide.common.exception.CustomException;
import kdt.web_ide.common.exception.ErrorCode;
import kdt.web_ide.file.service.S3Service;
import kdt.web_ide.members.entity.Member;
import kdt.web_ide.members.entity.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -35,6 +37,8 @@ public class ChatService {

private final MemberRepository memberRepository;

private final S3Service s3Service;

// 메세지 전송
@Transactional
public void sendMessage(Long chatRoomId, ChatMessageRequestDto requestDto) {
Expand Down Expand Up @@ -110,4 +114,25 @@ private void notifyUnreadMessageCount(Long roomId, Long senderId) {
}
}
}

@Transactional
public void sendImage(Long senderId, Long chatRoomId, List<MultipartFile> multipartFileList) {

chatRoomRepository
.findById(chatRoomId)
.orElseThrow(() -> new CustomException(ErrorCode.CHATROOM_NOT_FOUND));

memberRepository
.findById(senderId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<String> imageUrlList = s3Service.uploadImages(multipartFileList);

for (String imageUrl : imageUrlList) {

ChatMessageRequestDto requestDto = new ChatMessageRequestDto(senderId, imageUrl);

sendMessage(chatRoomId, requestDto);
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/kdt/web_ide/file/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import kdt.web_ide.common.exception.CustomException;
import kdt.web_ide.common.exception.ErrorCode;
Expand Down Expand Up @@ -252,4 +256,38 @@ public void deleteFile(String filePath) {
private String extractFileName(String filePath) {
return filePath.substring(filePath.lastIndexOf("/") + 1);
}

public List<String> uploadImages(List<MultipartFile> imageFiles) {

List<String> imageUrls = new ArrayList<>();

for (MultipartFile imageFile : imageFiles) {
try {
String fileName = generateChatImageFileName();

PutObjectRequest request =
PutObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.contentType(imageFile.getContentType())
.build();

amazonS3.putObject(
request, RequestBody.fromInputStream(imageFile.getInputStream(), imageFile.getSize()));

String imageUrl = getFileUrl(fileName);

imageUrls.add(imageUrl);

} catch (IOException e) {
log.error("Error uploading file to S3.", e);
throw new CustomException(ErrorCode.S3_UPLOAD_ERROR);
}
}
return imageUrls;
}

private String generateChatImageFileName() {
return "chat" + '/' + UUID.randomUUID().toString();
}
}
5 changes: 2 additions & 3 deletions src/main/java/kdt/web_ide/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@ public PostResponseDto createPost(PostRequestDto requestDto) {

String fileName =
generateFileName(requestDto.getName(), String.valueOf(requestDto.getLanguage()));
String filePath = "test";
// uploadEmptyFileToS3(fileName, board.getId());
uploadEmptyFileToS3(fileName, board.getId());

Post post =
Post.builder()
.board(board)
.name(requestDto.getName())
.language(requestDto.getLanguage())
.filePath(filePath)
.filePath(fileName)
.createdAt(LocalDateTime.now())
.build();

Expand Down