Skip to content

Commit 746ce8f

Browse files
committed
feat(post) : #75 - 게시글 작성하기 이미지 s3 업로드
1 parent da0a727 commit 746ce8f

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/main/java/com/example/FixLog/controller/PostController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
import com.example.FixLog.dto.Response;
55
import com.example.FixLog.dto.post.PostResponseDto;
66
import com.example.FixLog.service.PostService;
7+
import com.example.FixLog.service.S3Service;
78
import org.springframework.web.bind.annotation.*;
9+
import org.springframework.web.multipart.MultipartFile;
810

911
@RestController
1012
@RequestMapping("/posts")
1113
public class PostController {
1214
private final PostService postService;
15+
private final S3Service s3Service;
1316

14-
public PostController(PostService postService){
17+
public PostController(PostService postService, S3Service s3Service){
1518
this.postService = postService;
19+
this.s3Service = s3Service;
1620
}
1721

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

28+
@PostMapping("/images")
29+
public Response<String> uploadImage(@RequestPart("imageFile") MultipartFile imageFile){
30+
String markdownImage = postService.uploadImage(imageFile);
31+
return Response.success("이미지 마크다운 형식으로 변환", markdownImage);
32+
}
33+
2434
@GetMapping("/{postId}")
2535
public Response<Object> viewPost(@PathVariable("postId") Long postId){
2636
PostResponseDto viewPost = postService.viewPost(postId);

src/main/java/com/example/FixLog/exception/ErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public enum ErrorCode {
2828
SAME_AS_OLD_PASSWORD(HttpStatus.BAD_REQUEST, "다른 비밀번호 입력 바랍니다."),
2929
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "권한이 없습니다."),
3030
INVALID_REQUEST(HttpStatus.BAD_REQUEST, "요청 데이터가 유효하지 않습니다."),
31-
S3_UPLOAD_FAILED(HttpStatus.BAD_REQUEST, "S3 파일 업로드에 실패했습니다.");
31+
S3_UPLOAD_FAILED(HttpStatus.BAD_REQUEST, "S3 파일 업로드에 실패했습니다."),
32+
IMAGE_UPLOAD_FAILED(HttpStatus.NOT_FOUND, "이미지 파일이 업로드되지 않았습니다.");
3233

3334
private final HttpStatus status;
3435
private final String message;

src/main/java/com/example/FixLog/service/PostService.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.example.FixLog.repository.tag.TagRepository;
2121
import jakarta.transaction.Transactional;
2222
import org.springframework.stereotype.Service;
23+
import org.springframework.web.multipart.MultipartFile;
2324

2425
import java.time.LocalDate;
2526
import java.time.LocalDateTime;
@@ -36,16 +37,19 @@ public class PostService {
3637
private final TagRepository tagRepository;
3738
private final BookmarkFolderRepository bookmarkFolderRepository;
3839
private final MemberService memberService;
40+
private final S3Service s3Service;
3941

4042
public PostService(PostRepository postRepository, PostLikeRepository postLikeRepository,
4143
BookmarkRepository bookmarkRepository, TagRepository tagRepository,
42-
BookmarkFolderRepository bookmarkFolderRepository, MemberService memberService){
44+
BookmarkFolderRepository bookmarkFolderRepository, MemberService memberService,
45+
S3Service s3Service){
4346
this.postRepository = postRepository;
4447
this.postLikeRepository = postLikeRepository;
4548
this.bookmarkRepository = bookmarkRepository;
4649
this.tagRepository = tagRepository;
4750
this.bookmarkFolderRepository = bookmarkFolderRepository;
4851
this.memberService = memberService;
52+
this.s3Service = s3Service;
4953
}
5054

5155
// 이미지 null일 때 default 사진으로 변경 (프로필 사진,
@@ -83,6 +87,7 @@ public void createPost(PostRequestDto postRequestDto){
8387
.editedAt(LocalDateTime.now())
8488
.postTags(new ArrayList<>())
8589
.build();
90+
// Todo : 여기서 사진 발생하면 s3 처리하기
8691

8792
// 태그 저장
8893
for (Tag tag : tags) {
@@ -92,6 +97,16 @@ public void createPost(PostRequestDto postRequestDto){
9297
postRepository.save(newPost);
9398
}
9499

100+
// 이미지 파일 마크다운으로 변경
101+
public String uploadImage(MultipartFile imageFile){
102+
if (imageFile == null || imageFile.isEmpty()){
103+
throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED);
104+
}
105+
106+
String imageUrl = s3Service.upload(imageFile, "post-image");
107+
return "![image](" + imageUrl + ")";
108+
}
109+
95110
// 태그 다 선택 했는지
96111
private List<Tag> fetchAndValidateTags(List<Long> tagIds){
97112
// 태그 ID로 Tag 엔티티 조회

0 commit comments

Comments
 (0)