diff --git a/src/main/java/com/example/tripy/domain/currency/CurrencyController.java b/src/main/java/com/example/tripy/domain/currency/CurrencyController.java index b95aa8d..d934baa 100644 --- a/src/main/java/com/example/tripy/domain/currency/CurrencyController.java +++ b/src/main/java/com/example/tripy/domain/currency/CurrencyController.java @@ -1,11 +1,9 @@ package com.example.tripy.domain.currency; import com.example.tripy.domain.currency.dto.CurrencyResponseDto; -import com.example.tripy.domain.post.dto.PostResponseDto; import com.example.tripy.global.common.response.ApiResponse; import java.util.List; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/src/main/java/com/example/tripy/domain/post/Post.java b/src/main/java/com/example/tripy/domain/post/Post.java index 5eba1e8..cdc1a47 100644 --- a/src/main/java/com/example/tripy/domain/post/Post.java +++ b/src/main/java/com/example/tripy/domain/post/Post.java @@ -1,13 +1,10 @@ package com.example.tripy.domain.post; import com.example.tripy.domain.city.City; -import com.example.tripy.domain.post.dto.PostCreateRequestDto; -import com.example.tripy.domain.postfile.PostFile; -import com.example.tripy.domain.posttag.PostTag; -import com.example.tripy.domain.travelplan.TravelPlan; import com.example.tripy.domain.member.Member; +import com.example.tripy.domain.post.dto.PostRequestDto.CreatePostRequest; +import com.example.tripy.domain.travelplan.TravelPlan; import com.example.tripy.global.utils.BaseTimeEntity; -import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -15,11 +12,7 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToMany; import jakarta.validation.constraints.NotNull; -import java.util.ArrayList; -import java.util.List; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -43,13 +36,11 @@ public class Post extends BaseTimeEntity { @NotNull private String content; - private String address; - @ColumnDefault("0") private Long view; @ColumnDefault("0") - private Integer thumbs; + private Integer recommendationCount; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") @@ -59,31 +50,18 @@ public class Post extends BaseTimeEntity { @JoinColumn(name = "city_id") private City city; - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "travelplan_id") private TravelPlan travelPlan; - public void viewCountUp() { - this.view++; - } - - public void thumbsCountUp() { - this.thumbs++; - } - - public void thumbsCountDown() { - this.thumbs--; - } - - // TODO: 2024/01/05 PostFile, PostTag리스트 추가 - public static Post toEntity(PostCreateRequestDto requestDto, TravelPlan travelPlan){ + public static Post toEntity(CreatePostRequest createPostRequest, Member member, City city + , TravelPlan travelPlan) { return Post.builder() - .title(requestDto.getTitle()) - .content(requestDto.getContent()) - .address(requestDto.getAddress()) - .travelPlan(travelPlan) - .build(); + .title(createPostRequest.getTitle()) + .content(createPostRequest.getContent()) + .member(member) + .city(city) + .travelPlan(travelPlan) + .build(); } - } \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/post/PostController.java b/src/main/java/com/example/tripy/domain/post/PostController.java index c75c71a..c64de45 100644 --- a/src/main/java/com/example/tripy/domain/post/PostController.java +++ b/src/main/java/com/example/tripy/domain/post/PostController.java @@ -1,56 +1,40 @@ package com.example.tripy.domain.post; -import com.example.tripy.domain.post.dto.PostCreateRequestDto; -import com.example.tripy.domain.post.dto.PostResponseDto; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.media.Schema; +import com.example.tripy.domain.post.dto.PostRequestDto.CreatePostRequest; +import com.example.tripy.global.common.response.ApiResponse; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.io.IOException; -import java.util.List; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; @RequiredArgsConstructor @RestController public class PostController { + private final PostService postService; /** * [POST] 게시글 작성 */ - @PostMapping - public ResponseEntity addCorporation(@RequestPart(value = "postCreateRequestDto") @Parameter(schema = @Schema(type = "string", format = MediaType.APPLICATION_JSON_VALUE)) PostCreateRequestDto postCreateRequestDto) throws IOException { - postService.addPost(postCreateRequestDto); - return new ResponseEntity<>(HttpStatus.CREATED); - } - - - /** - * [GET] 전체 글 조회 - */ - @GetMapping - public ResponseEntity> getPostList(){ - return ResponseEntity.ok(postService.getPostList()); + @PostMapping("api/posts") + public ApiResponse createPost( + @RequestBody CreatePostRequest createPostRequest, + @RequestParam(required = false) Long travelPlanId, + @RequestParam Long cityId) { + postService.addPost(createPostRequest, travelPlanId, cityId); + return ApiResponse.onSuccess("글 작성에 성공했습니다."); } /** - * [GET] 도시가 속한 나라별 전체 글 조회 - */ -// @GetMapping("/{cityId}") -// public ResponseEntity> getPostListByCity(@PathVariable Long cityId){ -// return ResponseEntity.ok(postService.getPostList(cityId)); -// } - - /** - * [GET] 특정 글 상세 조회 + * [DELETE] 게시글 삭제 */ - @GetMapping("/detail/{postId}") - public ResponseEntity getPost(@PathVariable Long postId){ - return ResponseEntity.ok(postService.getPostDetail(postId)); + @DeleteMapping("api/posts/{postsId}") + public ApiResponse deletePost( + @PathVariable Long postsId) { + postService.deletePost(postsId); + return ApiResponse.onSuccess("글 삭제에 성공했습니다."); } - - -} +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/post/PostService.java b/src/main/java/com/example/tripy/domain/post/PostService.java index 263637b..abc0964 100644 --- a/src/main/java/com/example/tripy/domain/post/PostService.java +++ b/src/main/java/com/example/tripy/domain/post/PostService.java @@ -1,66 +1,83 @@ package com.example.tripy.domain.post; +import com.example.tripy.domain.city.City; import com.example.tripy.domain.city.CityRepository; -import com.example.tripy.domain.post.dto.PostCreateRequestDto; -import com.example.tripy.domain.post.dto.PostResponseDto; +import com.example.tripy.domain.member.Member; +import com.example.tripy.domain.member.MemberRepository; +import com.example.tripy.domain.post.dto.PostRequestDto.CreatePostRequest; +import com.example.tripy.domain.postfile.FileType; +import com.example.tripy.domain.postfile.PostFileService; +import com.example.tripy.domain.posttag.PostTagService; import com.example.tripy.domain.travelplan.TravelPlan; import com.example.tripy.domain.travelplan.TravelPlanRepository; +import com.example.tripy.global.common.response.code.status.ErrorStatus; +import com.example.tripy.global.common.response.exception.GeneralException; import jakarta.transaction.Transactional; import java.util.List; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @RequiredArgsConstructor @Service public class PostService { + private final PostRepository postRepository; private final TravelPlanRepository travelPlanRepository; private final CityRepository cityRepository; + private final PostFileService postFileService; + private final MemberRepository memberRepository; + private final PostTagService postTagService; - // TODO: 2024/01/01 예외처리 및 MultipartFile 처리 @Transactional - public void addPost(PostCreateRequestDto postCreateRequestDto){ - try{ - TravelPlan travelPlan = travelPlanRepository.findById(postCreateRequestDto.getTravelPlanId()).get(); - Post post = Post.toEntity(postCreateRequestDto, travelPlan); - postRepository.save(post); + public void addPost(CreatePostRequest createPostRequest, Long travelPlanId, Long cityId) { + + Member member = memberRepository.findById(1L) + .orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_MEMBER)); + + TravelPlan travelPlan = null; + if (travelPlanId != null) { + travelPlan = travelPlanRepository.findById(travelPlanId) + .orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_TRAVEL_PLAN)); } - catch (Exception e){ - } - } + City city = cityRepository.findById(cityId) + .orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_CITY)); - /** - * 전체 글 조회 - */ + Post post = Post.toEntity(createPostRequest, member, city, travelPlan); + + postRepository.save(post); + if(createPostRequest.getImageUrls() != null) { + addImages(post, createPostRequest.getImageUrls()); + } + if(createPostRequest.getFileUrls() != null) { + addFiles(post, createPostRequest.getFileUrls()); + } + if(createPostRequest.getTagIds() != null) { + addTags(post, createPostRequest.getTagIds()); + } + } - public List getPostList(){ - List posts = postRepository.findAll(); - return posts.stream() - .map(PostResponseDto::toDTO) - .collect(Collectors.toList()); + private void addImages(Post post, List imageUrls) { + postFileService.saveFilesByType(post, imageUrls, FileType.IMAGE); } -// /** -// * 도시가 포함된 나라별 전체 글 조회 -// */ -// public List getPostList(Long cityId){ -// City city = cityRepository.findById(cityId).get(); -// Long countryId = city.getCountry().getId(); -// List posts = postRepository.findByCountry(countryId); -// -// return posts.stream() -// .map(PostResponseDto::toDTO) -// .collect(Collectors.toList()); -// } + private void addFiles(Post post, List fileUrls) { + postFileService.saveFilesByType(post, fileUrls, FileType.FILE); + } - /** - * 글 하나 조회 - */ - public PostResponseDto getPostDetail(Long postId){ - Post post = postRepository.findById(postId).get(); - post.viewCountUp(); - return PostResponseDto.toDTO(post); + private void addTags(Post post, List tagIds) { + postTagService.savePostTag(post, tagIds); } -} + public void deletePost(Long postsId) { + Member member = memberRepository.findById(1L) + .orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_MEMBER)); + + Post post = postRepository.findById(postsId) + .orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_POST)); + + postFileService.deleteFilesByPost(post); + postTagService.deletePostTagsByPost(post); + + postRepository.delete(post); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/post/dto/PostCreateRequestDto.java b/src/main/java/com/example/tripy/domain/post/dto/PostCreateRequestDto.java deleted file mode 100644 index cda9ad2..0000000 --- a/src/main/java/com/example/tripy/domain/post/dto/PostCreateRequestDto.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.example.tripy.domain.post.dto; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Getter -@Builder -public class PostCreateRequestDto { - //제목 - private String title; - - //내용 - private String content; - - //장소 - private String address; - - //특정 palnId를 선택한다고 가정 - private Long travelPlanId; - - //도시코드 - private Long countryId; -} diff --git a/src/main/java/com/example/tripy/domain/post/dto/PostRequestDto.java b/src/main/java/com/example/tripy/domain/post/dto/PostRequestDto.java new file mode 100644 index 0000000..2db42a5 --- /dev/null +++ b/src/main/java/com/example/tripy/domain/post/dto/PostRequestDto.java @@ -0,0 +1,25 @@ +package com.example.tripy.domain.post.dto; + +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class PostRequestDto { + + @NoArgsConstructor + @AllArgsConstructor + @Getter + public static class CreatePostRequest { + + private String title; + + private String content; + + private List imageUrls; + + private List fileUrls; + + private List tagIds; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/post/dto/PostResponseDto.java b/src/main/java/com/example/tripy/domain/post/dto/PostResponseDto.java deleted file mode 100644 index 5e05bf1..0000000 --- a/src/main/java/com/example/tripy/domain/post/dto/PostResponseDto.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.example.tripy.domain.post.dto; - -import com.example.tripy.domain.post.Post; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class PostResponseDto { - - private Long id; - private String title; - private String content; - private Long view; - private Integer thumbs; - - public static PostResponseDto toDTO(Post entity){ - return PostResponseDto.builder() - .id(entity.getId()) - .title(entity.getTitle()) - .content(entity.getContent()) - .view(entity.getView()) - .thumbs(entity.getThumbs()) - .build(); - } -} diff --git a/src/main/java/com/example/tripy/domain/postfile/FileType.java b/src/main/java/com/example/tripy/domain/postfile/FileType.java index c675821..8d6e812 100644 --- a/src/main/java/com/example/tripy/domain/postfile/FileType.java +++ b/src/main/java/com/example/tripy/domain/postfile/FileType.java @@ -1,5 +1,5 @@ package com.example.tripy.domain.postfile; public enum FileType { - 파일, 링크, 사진 -} + IMAGE, FILE +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/postfile/PostFile.java b/src/main/java/com/example/tripy/domain/postfile/PostFile.java index daca95e..f17d7e4 100644 --- a/src/main/java/com/example/tripy/domain/postfile/PostFile.java +++ b/src/main/java/com/example/tripy/domain/postfile/PostFile.java @@ -5,6 +5,7 @@ import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -12,6 +13,7 @@ @AllArgsConstructor @NoArgsConstructor @Entity +@Builder public class PostFile extends BaseTimeEntity { @Id @@ -21,17 +23,18 @@ public class PostFile extends BaseTimeEntity { @NotNull private String url; - @NotNull @Enumerated(EnumType.STRING) - private FileType type; + private FileType fileType; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id") private Post post; - - -} - - - + public static PostFile toEntity(Post post, String url, FileType fileType) { + return PostFile.builder() + .url(url) + .post(post) + .fileType(fileType) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/postfile/PostFileRepository.java b/src/main/java/com/example/tripy/domain/postfile/PostFileRepository.java index f7d56b2..f3a8cb4 100644 --- a/src/main/java/com/example/tripy/domain/postfile/PostFileRepository.java +++ b/src/main/java/com/example/tripy/domain/postfile/PostFileRepository.java @@ -1,6 +1,10 @@ package com.example.tripy.domain.postfile; +import com.example.tripy.domain.post.Post; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface PostFileRepository extends JpaRepository { -} + + List findAllByPost(Post post); +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/postfile/PostFileService.java b/src/main/java/com/example/tripy/domain/postfile/PostFileService.java index 51890a9..40521d0 100644 --- a/src/main/java/com/example/tripy/domain/postfile/PostFileService.java +++ b/src/main/java/com/example/tripy/domain/postfile/PostFileService.java @@ -1,9 +1,33 @@ package com.example.tripy.domain.postfile; +import com.example.tripy.domain.post.Post; +import com.example.tripy.global.s3.S3Service; +import jakarta.transaction.Transactional; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @RequiredArgsConstructor @Service public class PostFileService { -} + + private final PostFileRepository postFileRepository; + private final S3Service s3Service; + + @Transactional + public void saveFilesByType(Post post, List urls, FileType fileType) { + urls.forEach(url -> { + PostFile postFile = PostFile.toEntity(post, url, fileType); + postFileRepository.save(postFile); + }); + } + + @Transactional + public void deleteFilesByPost(Post post) { + List postFiles = postFileRepository.findAllByPost(post); + + for(PostFile postFile : postFiles) { + s3Service.deleteFile(s3Service.parseFileName(postFile.getUrl())); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/posttag/PostTag.java b/src/main/java/com/example/tripy/domain/posttag/PostTag.java index 0085f52..2414a79 100644 --- a/src/main/java/com/example/tripy/domain/posttag/PostTag.java +++ b/src/main/java/com/example/tripy/domain/posttag/PostTag.java @@ -10,14 +10,15 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @Getter @AllArgsConstructor @NoArgsConstructor +@Builder @Entity public class PostTag extends BaseTimeEntity { @@ -32,9 +33,11 @@ public class PostTag extends BaseTimeEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "tag_id") private Tag tag; - - -} - - + public static PostTag toEntity(Post post, Tag tag) { + return PostTag.builder() + .post(post) + .tag(tag) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/domain/posttag/PostTagRepository.java b/src/main/java/com/example/tripy/domain/posttag/PostTagRepository.java index 7398005..1e6e2f2 100644 --- a/src/main/java/com/example/tripy/domain/posttag/PostTagRepository.java +++ b/src/main/java/com/example/tripy/domain/posttag/PostTagRepository.java @@ -1,6 +1,9 @@ package com.example.tripy.domain.posttag; +import com.example.tripy.domain.post.Post; import org.springframework.data.jpa.repository.JpaRepository; public interface PostTagRepository extends JpaRepository { + + void deleteByPost(Post post); } diff --git a/src/main/java/com/example/tripy/domain/posttag/PostTagService.java b/src/main/java/com/example/tripy/domain/posttag/PostTagService.java index 1098f4a..02b6c55 100644 --- a/src/main/java/com/example/tripy/domain/posttag/PostTagService.java +++ b/src/main/java/com/example/tripy/domain/posttag/PostTagService.java @@ -1,9 +1,34 @@ package com.example.tripy.domain.posttag; +import com.example.tripy.domain.post.Post; +import com.example.tripy.domain.tag.Tag; +import com.example.tripy.domain.tag.TagRepository; +import com.example.tripy.global.common.response.code.status.ErrorStatus; +import com.example.tripy.global.common.response.exception.GeneralException; +import jakarta.transaction.Transactional; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @RequiredArgsConstructor @Service public class PostTagService { -} + + private final PostTagRepository postTagRepository; + private final TagRepository tagRepository; + + @Transactional + public void savePostTag(Post post, List tags) { + tags.forEach(tagId -> { + Tag tag = tagRepository.findById(tagId) + .orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_TAG)); + + PostTag postTag = PostTag.toEntity(post, tag); + postTagRepository.save(postTag); + }); + } + + public void deletePostTagsByPost(Post post) { + postTagRepository.deleteByPost(post); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/global/common/response/code/status/ErrorStatus.java b/src/main/java/com/example/tripy/global/common/response/code/status/ErrorStatus.java index e10c690..ad5996c 100644 --- a/src/main/java/com/example/tripy/global/common/response/code/status/ErrorStatus.java +++ b/src/main/java/com/example/tripy/global/common/response/code/status/ErrorStatus.java @@ -34,8 +34,16 @@ public enum ErrorStatus implements BaseErrorCode { // TravelPlan 관련 _EMPTY_TRAVEL_PLAN(HttpStatus.NOT_FOUND, "TRAVEL_PLAN_001", "존재하지 않는 여행 계획입니다."), _ALREADY_TRAVEL_PLAN_BAG_EXISTS(HttpStatus.BAD_REQUEST, "TRAVEL_PLAN_002", - "이미 가방이 존재하는 여행 계획입니다."); + "이미 가방이 존재하는 여행 계획입니다."), + //파일 업로드 관련 + _FILE_UPLOAD_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "FILE_001", "파일 업로드에 실패했습니다."), + + //태그 관련 + _EMPTY_TAG(HttpStatus.CONFLICT, "TAG_001", "태그가 존재하지 않습니다."), + + //게시글 관련 + _EMPTY_POST(HttpStatus.CONFLICT, "POST_001", "게시글이 존재하지 않습니다."); private final HttpStatus httpStatus; private final String code; diff --git a/src/main/java/com/example/tripy/global/s3/S3Controller.java b/src/main/java/com/example/tripy/global/s3/S3Controller.java new file mode 100644 index 0000000..052c0b3 --- /dev/null +++ b/src/main/java/com/example/tripy/global/s3/S3Controller.java @@ -0,0 +1,29 @@ +package com.example.tripy.global.s3; + +import com.example.tripy.global.common.response.code.status.ErrorStatus; +import com.example.tripy.global.common.response.exception.GeneralException; +import com.example.tripy.global.s3.dto.S3Result; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RequiredArgsConstructor +@RestController +public class S3Controller { + + private final S3Service s3Service; + + @PostMapping("api/file") + public ResponseEntity uploadFile(@RequestPart(value = "file") MultipartFile file) { + try { + S3Result result = s3Service.uploadFile(file); + return ResponseEntity.ok(result); + } catch (Exception e) { + throw new GeneralException(ErrorStatus._FILE_UPLOAD_ERROR); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/tripy/global/s3/S3Service.java b/src/main/java/com/example/tripy/global/s3/S3Service.java index 7f83343..76be54c 100644 --- a/src/main/java/com/example/tripy/global/s3/S3Service.java +++ b/src/main/java/com/example/tripy/global/s3/S3Service.java @@ -43,7 +43,7 @@ private String createFileName(String fileName) { return UUID.randomUUID().toString().concat(getFileExtension(fileName)); } - public List uploadFile(List multipartFiles) { + public List uploadFiles(List multipartFiles) { List fileList = new ArrayList<>(); // forEach 구문을 통해 multipartFile로 넘어온 파일들 하나씩 fileList에 추가 @@ -64,7 +64,27 @@ public List uploadFile(List multipartFiles) { return fileList; } + public S3Result uploadFile(MultipartFile multipartFile) { + String fileName = createFileName(multipartFile.getOriginalFilename()); + ObjectMetadata objectMetadata = new ObjectMetadata(); + objectMetadata.setContentLength(multipartFile.getSize()); + objectMetadata.setContentType(multipartFile.getContentType()); + + try (InputStream inputStream = multipartFile.getInputStream()) { + amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) + .withCannedAcl(CannedAccessControlList.PublicRead)); + } catch (IOException e) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드에 실패했습니다."); + } + return new S3Result(amazonS3Client.getUrl(bucket, fileName).toString()); + } + public void deleteFile(String fileName) { amazonS3Client.deleteObject(new DeleteObjectRequest(bucket, fileName)); } + + public String parseFileName(String imgURl) { + String[] st = imgURl.split("/"); + return st[st.length - 1]; + } } \ No newline at end of file