diff --git a/src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlData.java b/src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlData.java new file mode 100644 index 0000000..f108310 --- /dev/null +++ b/src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlData.java @@ -0,0 +1,15 @@ +package kr.bb.product.domain.presigendurl; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class PresignedUrlData { + private String presignedUrl; +} diff --git a/src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlService.java b/src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlService.java new file mode 100644 index 0000000..70ac5af --- /dev/null +++ b/src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlService.java @@ -0,0 +1,47 @@ +package kr.bb.product.domain.presigendurl; + +import com.amazonaws.HttpMethod; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.Headers; +import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; +import java.util.Date; +import java.util.UUID; +import org.springframework.stereotype.Component; + +@Component +public class PresignedUrlService { + public static PresignedUrlData getPresignedUrl( + String prefix, String fileName, AmazonS3 amazonS3, String bucket) { + String onlyOneFileName = onlyOneFileName(fileName); + + GeneratePresignedUrlRequest generatePresignedUrlRequest = + getGeneratePreSignedUrlRequest(bucket, prefix + "/" + onlyOneFileName); + return PresignedUrlData.builder() + .presignedUrl(amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString()) + .build(); + } + + private static GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest( + String bucket, String fileName) { + GeneratePresignedUrlRequest generatePresignedUrlRequest = + new GeneratePresignedUrlRequest(bucket, fileName) + .withMethod(HttpMethod.PUT) + .withExpiration(getPreSignedUrlExpiration()); + generatePresignedUrlRequest.addRequestParameter( + Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString()); + return generatePresignedUrlRequest; + } + + private static Date getPreSignedUrlExpiration() { + Date expiration = new Date(); + long expTimeMillis = expiration.getTime(); + expTimeMillis += 1000 * 60 * 5; + expiration.setTime(expTimeMillis); + return expiration; + } + + private static String onlyOneFileName(String fileName) { + return UUID.randomUUID().toString() + fileName; + } +} diff --git a/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java b/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java index bf31891..e1d80ae 100644 --- a/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java +++ b/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java @@ -1,7 +1,5 @@ package kr.bb.product.domain.product.application.port.in; -import bloomingblooms.domain.aws.PresignedUrlData; -import bloomingblooms.domain.aws.PresignedUrlService; import bloomingblooms.domain.flower.StockChangeDto; import bloomingblooms.domain.order.ProcessOrderDto; import bloomingblooms.domain.product.IsProductPriceValid; @@ -24,6 +22,8 @@ import kr.bb.product.domain.flower.application.port.out.FlowerQueryOutPort; import kr.bb.product.domain.flower.entity.Flower; import kr.bb.product.domain.flower.mapper.FlowerCommand; +import kr.bb.product.domain.presigendurl.PresignedUrlData; +import kr.bb.product.domain.presigendurl.PresignedUrlService; import kr.bb.product.domain.product.application.port.out.ProductQueryOutPort; import kr.bb.product.domain.product.application.usecase.ProductQueryUseCase; import kr.bb.product.domain.product.entity.Product; @@ -374,26 +374,29 @@ public ProductsForAdmin getProductsForAdmin( @Override public ProductList searchByUser(String sentence, Pageable pageable) { - String prompt = getPrompt(sentence); - String response = chatgptService.sendMessage(prompt); - Page products = - productQueryOutPort.findProductsByFlowerId(Long.parseLong(response.trim()), pageable); + Long flowerId = getFlowerId(sentence); + Page products = productQueryOutPort.findProductsByFlowerId(flowerId, pageable); List product = getProduct(products); return ProductList.getData(product, products.getTotalElements()); } @Override public ProductList searchByUser(Long userId, String sentence, Pageable pageable) { - String prompt = getPrompt(sentence); - String response = chatgptService.sendMessage(prompt); - Page products = - productQueryOutPort.findProductsByFlowerId(Long.parseLong(response.trim()), pageable); + Long flowerId = getFlowerId(sentence); + Page products = productQueryOutPort.findProductsByFlowerId(flowerId, pageable); List product = getProduct(products); List ids = getProductIdsFromProducts(products.getContent()); List data = getProductsIsLiked(userId, ids); return ProductList.getData(product, data, products.getTotalElements()); } + @NotNull + private Long getFlowerId(String sentence) { + String prompt = getPrompt(sentence); + String response = chatgptService.sendMessage(prompt); + return Long.valueOf(response.split(":")[1]); + } + private String getPrompt(String sentence) { return "- Please choose one of several flowers. Types include " + "\n" @@ -404,8 +407,7 @@ private String getPrompt(String sentence) { + sentence + "\"" + "\n" - + "- respond just flowerId exclude flowerName" - + "- response data must be only number"; + + "- response must be number like 'red rose:1'"; } @Override diff --git a/src/main/java/kr/bb/product/domain/product/application/usecase/ProductQueryUseCase.java b/src/main/java/kr/bb/product/domain/product/application/usecase/ProductQueryUseCase.java index 7463521..2597f09 100644 --- a/src/main/java/kr/bb/product/domain/product/application/usecase/ProductQueryUseCase.java +++ b/src/main/java/kr/bb/product/domain/product/application/usecase/ProductQueryUseCase.java @@ -1,6 +1,5 @@ package kr.bb.product.domain.product.application.usecase; -import bloomingblooms.domain.aws.PresignedUrlData; import bloomingblooms.domain.flower.StockChangeDto; import bloomingblooms.domain.order.ProcessOrderDto; import bloomingblooms.domain.product.IsProductPriceValid; @@ -13,6 +12,7 @@ import bloomingblooms.domain.wishlist.likes.LikedProductInfoResponse; import java.util.List; import java.util.Map; +import kr.bb.product.domain.presigendurl.PresignedUrlData; import kr.bb.product.domain.product.entity.ProductSaleStatus; import kr.bb.product.domain.product.mapper.ProductCommand; import kr.bb.product.domain.product.mapper.ProductCommand.ProductList; diff --git a/src/main/java/kr/bb/product/domain/product/infrastructure/http/api/ProductRestController.java b/src/main/java/kr/bb/product/domain/product/infrastructure/http/api/ProductRestController.java index 28431ba..f8f5d8d 100644 --- a/src/main/java/kr/bb/product/domain/product/infrastructure/http/api/ProductRestController.java +++ b/src/main/java/kr/bb/product/domain/product/infrastructure/http/api/ProductRestController.java @@ -1,10 +1,10 @@ package kr.bb.product.domain.product.infrastructure.http.api; -import bloomingblooms.domain.aws.PresignedUrlData; import bloomingblooms.response.CommonResponse; import java.util.List; import java.util.Optional; import javax.validation.Valid; +import kr.bb.product.domain.presigendurl.PresignedUrlData; import kr.bb.product.domain.product.application.usecase.ProductCommandUseCase; import kr.bb.product.domain.product.application.usecase.ProductQueryUseCase; import kr.bb.product.domain.product.entity.ProductSaleStatus;