Skip to content

Commit

Permalink
Merge pull request #91 from lotteon2/fix/presigned-url
Browse files Browse the repository at this point in the history
Fix/presigned url
  • Loading branch information
qwerty1434 authored Jan 19, 2024
2 parents 6fa3bfb + 86787c5 commit 9be118c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Product> products =
productQueryOutPort.findProductsByFlowerId(Long.parseLong(response.trim()), pageable);
Long flowerId = getFlowerId(sentence);
Page<Product> products = productQueryOutPort.findProductsByFlowerId(flowerId, pageable);
List<ProductListItem> 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<Product> products =
productQueryOutPort.findProductsByFlowerId(Long.parseLong(response.trim()), pageable);
Long flowerId = getFlowerId(sentence);
Page<Product> products = productQueryOutPort.findProductsByFlowerId(flowerId, pageable);
List<ProductListItem> product = getProduct(products);
List<String> ids = getProductIdsFromProducts(products.getContent());
List<String> 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"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 9be118c

Please sign in to comment.