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

⚠️AWS S3 업로드 관련 로그 추가 #16

Merged
merged 1 commit into from
Jul 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ongjong.namanmoo.global.security.util.DateUtil;
import ongjong.namanmoo.domain.Member;
import ongjong.namanmoo.domain.answer.Answer;
Expand All @@ -27,6 +28,7 @@
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/challenges")
Expand Down Expand Up @@ -145,6 +147,7 @@ public ResponseEntity<ApiResponse<Map<String, String>>> savePhotoAnswer(
@ModelAttribute PhotoAnswerRequest photoAnswerRequest) throws Exception {
// @RequestPart("challengeId") Long challengeId,
// @RequestPart("answer") MultipartFile answerFile) throws Exception {
log.debug("PhotoAnswerRequest received: {}", photoAnswerRequest);

Long challengeId = photoAnswerRequest.getChallengeId();
MultipartFile answerFile = photoAnswerRequest.getAnswer();
Expand All @@ -164,14 +167,17 @@ public ResponseEntity<ApiResponse<Map<String, String>>> savePhotoAnswer(

// S3에 파일 업로드
String uploadImageUrl = awsS3Service.uploadFile(answerFile);
log.debug("Uploaded image URL: {}", uploadImageUrl);

// Answer 객체 수정 및 저장
Optional<Answer> optionalAnswer = answerService.findAnswerByChallengeAndMember(challenge, member);
if (optionalAnswer.isPresent()) {
Answer answer = optionalAnswer.get();
log.debug("Answer found: {}", answer);
answer.setAnswerContent(uploadImageUrl);
answer.setModifiedDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss")));
answerService.saveAnswer(answer);
log.debug("Answer saved: {}", answer);

// Map 형태로 답변 URL 반환
Map<String, String> responseData = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ongjong.namanmoo.dto.member.LoginRequestDto;
import ongjong.namanmoo.dto.member.MemberInfoDto;
import ongjong.namanmoo.dto.member.MemberSignUpDto;
Expand All @@ -19,6 +20,7 @@
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
public class MemberController {
Expand Down Expand Up @@ -55,18 +57,24 @@ public ResponseEntity<ApiResponse<Map<String, Boolean>>> duplicate(@Valid @Reque
@PostMapping("/users")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<MemberInfoDto> updateBasicInfo(@RequestPart("userInfo") MemberUpdateDto memberUpdateDto) throws Exception {
log.debug("Received MemberUpdateDto: {}", memberUpdateDto);

String uploadImageUrl = null;

// 파일을 전송했을 경우에만 S3 파일 업로드 수행
if (memberUpdateDto.userImg().isPresent() && !memberUpdateDto.userImg().get().isEmpty()) {
log.debug("Uploading file to S3...");
uploadImageUrl = awsS3Service.uploadFile(memberUpdateDto.userImg().get());
log.debug("File uploaded to S3: {}", uploadImageUrl);
}

log.debug("Updating member information...");
memberService.update(memberUpdateDto);
log.debug("Member information updated.");

// MemberService의 update 메소드에서 imagePath를 갱신해야 합니다.
MemberInfoDto info = memberService.getMyInfo();
log.debug("Member info retrieved: {}", info);
return new ApiResponse<>("200", "Update User Info Success", info);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public ResponseEntity<ApiResponse<String>> handleIllegalStateException(IllegalSt

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<String>> handleException(Exception ex, WebRequest request) {
ApiResponse<String> response = new ApiResponse<>("500", "Internal Server Error", null);
ex.printStackTrace(); // 콘솔에 예외의 스택 트레이스를 출력합니다.
ApiResponse<String> response = new ApiResponse<>("500", "Internal Server Error: " + ex.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ongjong/namanmoo/service/AwsS3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ public class AwsS3Service {
private String bucket;

public String uploadFile(MultipartFile multipartFile) throws IOException {
log.debug("Converting MultipartFile to File...");
File uploadFile = convertFile(multipartFile)
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File convert fail"));

String fileName = generateFileName(uploadFile);

log.debug("Uploading file to S3: {}", fileName);
String uploadImageUrl = uploadFileToS3(uploadFile, fileName);
log.debug("File uploaded to S3: {}", uploadImageUrl);

removeNewFile(uploadFile);
return uploadImageUrl;
}
Expand Down