diff --git a/src/main/java/org/example/gather_back_end/bucket/service/BucketService.java b/src/main/java/org/example/gather_back_end/bucket/service/BucketService.java index 5fc5a2a..2a77558 100644 --- a/src/main/java/org/example/gather_back_end/bucket/service/BucketService.java +++ b/src/main/java/org/example/gather_back_end/bucket/service/BucketService.java @@ -14,4 +14,9 @@ public interface BucketService { MultipartFile downloadFile(Long userId, String type) throws Exception; String defaultProfileImgUrl(); + + String createThumbnailImg(MultipartFile file) throws Exception; + + String createPortfolioPdf(MultipartFile file) throws Exception; + } diff --git a/src/main/java/org/example/gather_back_end/bucket/service/BucketServiceImpl.java b/src/main/java/org/example/gather_back_end/bucket/service/BucketServiceImpl.java index 9d5ca43..ab96739 100644 --- a/src/main/java/org/example/gather_back_end/bucket/service/BucketServiceImpl.java +++ b/src/main/java/org/example/gather_back_end/bucket/service/BucketServiceImpl.java @@ -36,6 +36,12 @@ public class BucketServiceImpl implements BucketService { @Value("${bucket.profile_img_dir}") private String BUCKET_PROFILE_IMG_DIR; + @Value(("${bucket.thumbnail_img_dir}")) + private String BUCKET_THUMBNAIL_IMG_DIR; + + @Value(("${bucket.pdf_dir}")) + private String BUCKET_PDF_DIR; + private final UserRepository userRepository; public static final String DEFAULT_URI_PREFIX = "https://objectstorage.ap-chuncheon-1.oraclecloud.com/n/"; @@ -108,4 +114,66 @@ private File convertMultiPartToFile(MultipartFile file) throws Exception { public String defaultProfileImgUrl() { return DEFAULT_URI_PREFIX + BUCKET_NAME_SPACE + "/b/" + BUCKET_NAME + "/o/" + "default_profile.png"; } + + // 썸네일 이미지 생성 + @Override + public String createThumbnailImg(MultipartFile file) throws Exception { + + File uploadFile = convertMultiPartToFile(file); + ObjectStorage client = getClient(); + UploadManager uploadManager = getManager(client); + + String fileName = BUCKET_THUMBNAIL_IMG_DIR + + "/" + UUID.randomUUID() + + "_" + file.getOriginalFilename(); + String contentType = file.getContentType(); + + PutObjectRequest request = PutObjectRequest.builder() + .bucketName(BUCKET_NAME) + .namespaceName(BUCKET_NAME_SPACE) + .objectName(fileName) + .contentType(contentType) + .build(); + + UploadRequest uploadDetails = UploadRequest.builder(uploadFile).build(request); + uploadManager.upload(uploadDetails); + + String thumbNailImgUrl = DEFAULT_URI_PREFIX + BUCKET_NAME_SPACE + "/b/" + BUCKET_NAME + "/o/" + fileName.replace("/", "%2F"); + + log.info("이미지 업로드 완료: {}", thumbNailImgUrl); + client.close(); + + return thumbNailImgUrl; + } + + // 포트폴리오 생성 + @Override + public String createPortfolioPdf(MultipartFile file) throws Exception { + + File uploadFile = convertMultiPartToFile(file); + ObjectStorage client = getClient(); + UploadManager uploadManager = getManager(client); + + String fileName = BUCKET_PDF_DIR + + "/" + UUID.randomUUID() + + "_" + file.getOriginalFilename(); + String contentType = file.getContentType(); + + PutObjectRequest request = PutObjectRequest.builder() + .bucketName(BUCKET_NAME) + .namespaceName(BUCKET_NAME_SPACE) + .objectName(fileName) + .contentType(contentType) + .build(); + + UploadRequest uploadDetails = UploadRequest.builder(uploadFile).build(request); + uploadManager.upload(uploadDetails); + + String portfolioPdfUrl = DEFAULT_URI_PREFIX + BUCKET_NAME_SPACE + "/b/" + BUCKET_NAME + "/o/" + fileName.replace("/", "%2F"); + + log.info("이미지 업로드 완료: {}", portfolioPdfUrl); + client.close(); + + return portfolioPdfUrl; + } } diff --git a/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java b/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java index 8090c67..7a60cce 100644 --- a/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java +++ b/src/main/java/org/example/gather_back_end/creator/controller/CreatorController.java @@ -24,7 +24,7 @@ @RestController @RequiredArgsConstructor @RequestMapping("/api/creator") -public class CreatorController { +public class CreatorController implements CreatorControllerApi { private final CreatorService creatorService; private final PortfolioService portfolioService; diff --git a/src/main/java/org/example/gather_back_end/creator/controller/CreatorControllerApi.java b/src/main/java/org/example/gather_back_end/creator/controller/CreatorControllerApi.java index 7dea94d..8b8cada 100644 --- a/src/main/java/org/example/gather_back_end/creator/controller/CreatorControllerApi.java +++ b/src/main/java/org/example/gather_back_end/creator/controller/CreatorControllerApi.java @@ -10,6 +10,8 @@ import org.example.gather_back_end.creator.dto.CreateCreatorReq; import org.example.gather_back_end.util.response.SuccessResponse; import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -32,6 +34,21 @@ public interface CreatorControllerApi { schema = @Schema(implementation = SuccessResponse.class))) }) @PostMapping - SuccessResponse createCreator(Authentication authentication, @RequestBody CreateCreatorReq req) throws IOException; + SuccessResponse createCreator(Authentication authentication, @RequestBody CreateCreatorReq req) throws Exception; + @Operation(summary = "사용자 프로필, 실명 가져오기 완료") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "사용자 프로필, 실명 가져오기 완료", + content = @Content(mediaType = "application/json", + examples = @ExampleObject(value = "{\n" + + " \"timestamp\": \"2024-11-03T05:07:47.704694\",\n" + + " \"isSuccess\": true,\n" + + " \"code\": \"200\",\n" + + " \"message\": \"호출에 성공하였습니다.\",\n" + + " \"data\": \"null" + + "}"), + schema = @Schema(implementation = SuccessResponse.class))) + }) + @GetMapping + SuccessResponse getCreator(@PathVariable String nickname); } diff --git a/src/main/java/org/example/gather_back_end/portfolio/dto/CreatePortfolioReq.java b/src/main/java/org/example/gather_back_end/portfolio/dto/CreatePortfolioReq.java index f6fb50e..197492f 100644 --- a/src/main/java/org/example/gather_back_end/portfolio/dto/CreatePortfolioReq.java +++ b/src/main/java/org/example/gather_back_end/portfolio/dto/CreatePortfolioReq.java @@ -1,11 +1,12 @@ package org.example.gather_back_end.portfolio.dto; import lombok.Builder; +import org.springframework.web.multipart.MultipartFile; @Builder public record CreatePortfolioReq( String title, - String thumbnailImgUrl, - String fileUrl + MultipartFile thumbnailImgUrl, + MultipartFile fileUrl ) { } diff --git a/src/main/java/org/example/gather_back_end/portfolio/service/PortfolioServiceImpl.java b/src/main/java/org/example/gather_back_end/portfolio/service/PortfolioServiceImpl.java index 0e48f12..91e67de 100644 --- a/src/main/java/org/example/gather_back_end/portfolio/service/PortfolioServiceImpl.java +++ b/src/main/java/org/example/gather_back_end/portfolio/service/PortfolioServiceImpl.java @@ -1,6 +1,7 @@ package org.example.gather_back_end.portfolio.service; import lombok.RequiredArgsConstructor; +import org.example.gather_back_end.bucket.service.BucketService; import org.example.gather_back_end.domain.Portfolio; import org.example.gather_back_end.domain.User; import org.example.gather_back_end.portfolio.dto.CreatePortfolioReq; @@ -17,6 +18,7 @@ public class PortfolioServiceImpl implements PortfolioService { private final PortfolioRepository portfolioRepository; private final UserRepository userRepository; + private final BucketService bucketService; @Override public void createPortfolio(Long userId, List createPortfolioReqList){ @@ -24,12 +26,16 @@ public void createPortfolio(Long userId, List createPortfoli User user = userRepository.getById(userId); for (CreatePortfolioReq portfolio : createPortfolioReqList){ - portfolioRepository.save(Portfolio.createPortfolioInfo( - user, - portfolio.title(), - portfolio.thumbnailImgUrl(), - portfolio.fileUrl() - )); + try { + portfolioRepository.save(Portfolio.createPortfolioInfo( + user, + portfolio.title(), + bucketService.createThumbnailImg(portfolio.thumbnailImgUrl()), + bucketService.createPortfolioPdf(portfolio.fileUrl()) + )); + } catch (Exception e) { + throw new RuntimeException(e); + } } }