Skip to content
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
11 changes: 0 additions & 11 deletions src/main/java/com/example/FixLog/controller/Controller

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public MainPageController(MainPageService mainPageService){

@GetMapping
public Response<Object> mainPageView(@RequestParam(value = "sort", defaultValue = "0") int sort,
@RequestParam(value = "page", defaultValue = "12") int size){
@RequestParam(value = "size", defaultValue = "12") int size){
MainPageResponseDto mainPageView = mainPageService.mainPageView(sort, size);
return Response.success("메인페이지 불러오기 성공", mainPageView);
}

@GetMapping("/full")
public Response<Object> mainPageFullView(@RequestParam(value = "sort", defaultValue = "0") int sort,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "page", defaultValue = "12") int size){
@RequestParam(value = "size", defaultValue = "12") int size){
MainPageResponseDto mainPageFullView = mainPageService.mainPageFullView(sort, page, size);
return Response.success("메인페이지 전체보기 성공", mainPageFullView);
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/example/FixLog/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import com.example.FixLog.dto.Response;
import com.example.FixLog.dto.post.PostResponseDto;
import com.example.FixLog.service.PostService;
import com.example.FixLog.service.S3Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
private final S3Service s3Service;

public PostController(PostService postService){
public PostController(PostService postService, S3Service s3Service){
this.postService = postService;
this.s3Service = s3Service;
}

@PostMapping
Expand All @@ -21,6 +25,12 @@ public Response<Object> createPost(@RequestBody PostRequestDto postRequestDto){
return Response.success("게시글 작성 성공.", null);
}

@PostMapping("/images")
public Response<String> uploadImage(@RequestPart("imageFile") MultipartFile imageFile){
String markdownImage = postService.uploadImage(imageFile);
return Response.success("이미지 마크다운 형식으로 변환", markdownImage);
}
Comment on lines +28 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

이미지 업로드 엔드포인트에 입력 검증을 추가해주세요.

MultipartFile에 대한 기본적인 검증(null 체크, 파일 크기, 파일 타입 등)을 컨트롤러 레벨에서 수행하는 것이 좋습니다.

 @PostMapping("/images")
-public Response<String> uploadImage(@RequestPart("imageFile") MultipartFile imageFile){
+public Response<String> uploadImage(@RequestPart("imageFile") MultipartFile imageFile){
+    if (imageFile == null || imageFile.isEmpty()) {
+        throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED);
+    }
     String markdownImage = postService.uploadImage(imageFile);
     return Response.success("이미지 마크다운 형식으로 변환", markdownImage);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@PostMapping("/images")
public Response<String> uploadImage(@RequestPart("imageFile") MultipartFile imageFile){
String markdownImage = postService.uploadImage(imageFile);
return Response.success("이미지 마크다운 형식으로 변환", markdownImage);
}
@PostMapping("/images")
public Response<String> uploadImage(@RequestPart("imageFile") MultipartFile imageFile) {
if (imageFile == null || imageFile.isEmpty()) {
throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED);
}
String markdownImage = postService.uploadImage(imageFile);
return Response.success("이미지 마크다운 형식으로 변환", markdownImage);
}
🤖 Prompt for AI Agents
In src/main/java/com/example/FixLog/controller/PostController.java around lines
28 to 32, the uploadImage method lacks input validation for the MultipartFile
parameter. Add null checks to ensure the file is provided, validate the file
size against a maximum allowed limit, and verify the file type (e.g., image MIME
types) before processing. If validation fails, return an appropriate error
response to prevent invalid files from being processed.


@GetMapping("/{postId}")
public Response<Object> viewPost(@PathVariable("postId") Long postId){
PostResponseDto viewPost = postService.viewPost(postId);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/FixLog/dto/post/PostDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
@Getter
@AllArgsConstructor
public class PostDto {
private Long userId;
private String nickname;
private String postTitle;
private String coverImageUrl;
private String problem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
@AllArgsConstructor
public class PostResponseDto {
private PostDto postInfo;
private LocalDate createdAt;

private String nickname;
private LocalDate createdAt;
private String profileImageUrl;
private boolean isLiked;
private boolean isMarked;
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/FixLog/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public enum ErrorCode {
SAME_AS_OLD_PASSWORD(HttpStatus.BAD_REQUEST, "다른 비밀번호 입력 바랍니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "권한이 없습니다."),
INVALID_REQUEST(HttpStatus.BAD_REQUEST, "요청 데이터가 유효하지 않습니다."),
S3_UPLOAD_FAILED(HttpStatus.BAD_REQUEST, "S3 파일 업로드에 실패했습니다.");
S3_UPLOAD_FAILED(HttpStatus.BAD_REQUEST, "S3 파일 업로드에 실패했습니다."),
IMAGE_UPLOAD_FAILED(HttpStatus.NOT_FOUND, "이미지 파일이 업로드되지 않았습니다.");

private final HttpStatus status;
private final String message;
Expand Down

This file was deleted.

85 changes: 0 additions & 85 deletions src/main/java/com/example/FixLog/mock/PostTestDataInitializer.java

This file was deleted.

33 changes: 0 additions & 33 deletions src/main/java/com/example/FixLog/mock/TagTestDataInitializer.java

This file was deleted.

28 changes: 22 additions & 6 deletions src/main/java/com/example/FixLog/service/MainPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand All @@ -37,9 +38,16 @@ public String getDefaultImage(String image){
// 메인페이지 보기
public MainPageResponseDto mainPageView(int sort, int size){
// 사용자 정보 불러오기
Member member = memberService.getCurrentMemberInfo();
String imageUrl = member.getProfileImageUrl();
String profileImageUrl = getDefaultImage(imageUrl);
Optional<Member> optionalMember = memberService.getCurrentOptionalMemberInfo();
String profileImageUrl;

if (optionalMember.isPresent()) {
Member member = optionalMember.get();
String imageUrl = member.getProfileImageUrl();
profileImageUrl = getDefaultImage(imageUrl);
} else {
profileImageUrl = "https://example.com/default-cover-image.png"; // 비로그인 기본 이미지
}

// 페이지 (글 12개) 불러오기
Page<Post> posts;
Expand All @@ -48,6 +56,7 @@ public MainPageResponseDto mainPageView(int sort, int size){
if (sort == 0) { // 최신순 정렬
sortOption = Sort.by(Sort.Direction.DESC, "createdAt");
} else if (sort == 1) { // 인기순 정렬
// Todo : 이거 정렬할 때 좋아요 0인거 이상하고, 이거랑 연결해서인지 totalpages 계산도 이상하게 됨
sortOption = Sort.by(Sort.Direction.DESC, "postLikes");
} else
throw new CustomException(ErrorCode.SORT_NOT_EXIST);
Expand Down Expand Up @@ -75,9 +84,16 @@ public MainPageResponseDto mainPageView(int sort, int size){
// 메인페이지 전체보기
public MainPageResponseDto mainPageFullView(int sort, int page, int size){
// 사용자 정보 불러오기
Member member = memberService.getCurrentMemberInfo();
String imageUrl = member.getProfileImageUrl();
String profileImageUrl = getDefaultImage(imageUrl);
Optional<Member> optionalMember = memberService.getCurrentOptionalMemberInfo();
String profileImageUrl;

if (optionalMember.isPresent()) {
Member member = optionalMember.get();
String imageUrl = member.getProfileImageUrl();
profileImageUrl = getDefaultImage(imageUrl);
} else {
profileImageUrl = "https://example.com/default-cover-image.png"; // 비로그인 기본 이미지
}

// 페이지 설정 (한 페이지당 12개)
Pageable pageable = PageRequest.of(page - 1, size);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/example/FixLog/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ public void signup(SignupRequestDto request) {
* 현재 로그인한 사용자 조회
*/
@Transactional(readOnly = true)
public Member getCurrentMemberInfo() {
public Member getCurrentMemberInfo() { // 예외 처리 O
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = authentication.getName();

return memberRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_EMAIL_NOT_FOUND));
}

@Transactional(readOnly = true)
public Optional<Member> getCurrentOptionalMemberInfo() { // 예외 처리 X
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = authentication.getName();

return memberRepository.findByEmail(email);
}

/**
* 닉네임 수정
*/
Expand Down
Loading