Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : Async/ Send image to Server #44

Merged
merged 6 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/zero/eight/donut/domain/Gift.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ public void setIsAssigned() {
public void updateGiftbox(Giftbox giftbox) {
this.giftbox = giftbox;
}

public void updateImageUrl(String imageUrl){
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zero.eight.donut.dto.donation;

import lombok.Getter;

@Getter
public class SendImageResponseDto {
private Long giftId;
private String resultUrl;
}
54 changes: 32 additions & 22 deletions src/main/java/zero/eight/donut/service/SerialDonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import zero.eight.donut.dto.donation.DonateGiftRequestDto;
import zero.eight.donut.dto.donation.GiftValueDto;
import zero.eight.donut.dto.donation.GiftboxRequestDto;
import zero.eight.donut.dto.donation.SendImageResponseDto;
import zero.eight.donut.exception.ApiException;
import zero.eight.donut.exception.Error;
import zero.eight.donut.exception.InternalServerErrorException;
Expand All @@ -32,6 +33,7 @@
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -153,16 +155,9 @@ public ApiResponse<?> donateGift(DonateGiftRequestDto requestDto) throws IOExcep
//기부 통계 업데이트
updateDonateInfo(requestDto);

/**
* !!!비동기 처리!!!
* 1. 받은 이미지 중 복구 실행해야 하는 것들 복구
* (복구에 성공했다면)
* -> 기존 imageUrl 객체 삭제
* -> newGift의 imageUrl 수정
**/
//Send Image to AI
// if(requestDto.getIsRestored())
// imageUrl = sendImageToAI(requestDto.getGiftImage());
//Send Image to AI & UPDATE Gift
if(requestDto.getIsRestored())
sendImageToAI(newGift.getId(), requestDto.getGiftImage());

return ApiResponse.success(Success.DONATE_GIFT_SUCCESS, Map.of("giftId", newGift.getId()));
}
Expand Down Expand Up @@ -253,20 +248,35 @@ private String uploadImageToGCS(DonateGiftRequestDto requestDto) throws IOExcept
String imgUrl = "https://storage.googleapis.com/" + BUCKET_NAME + "/" + uuid;
return imgUrl;
}
private String sendImageToAI(MultipartFile giftImage){
WebClient webClient = WebClient.builder().baseUrl("http://34.64.144.108:8000").build();

MultipartBodyBuilder image = new MultipartBodyBuilder();
image.part("file", giftImage.getResource());

Mono<String> imgResponse = webClient.post()
.uri("/api/server/enhancement")
private void sendImageToAI(Long giftId, MultipartFile giftImage){
WebClient webClient = WebClient.builder().baseUrl("http://127.0.0.1:8000").build();
MultipartBodyBuilder sandImageRequestDto = new MultipartBodyBuilder();
sandImageRequestDto.part("giftId", giftId);
log.info("giftId -> {}", giftId);
sandImageRequestDto.part("image", giftImage.getResource());
log.info("image -> {}", giftImage.getResource());
log.info("Start tp Sending image -> {}", LocalDateTime.now());
webClient.post()
.uri("/api/server/enhancement/optional")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(image.build()))
.bodyValue(sandImageRequestDto.build())
.retrieve()
.bodyToMono(String.class);
log.info("Sending image to AI Server");
String imgUrl = imgResponse.block();
return imgUrl.replace("\"", "");
.bodyToMono(SendImageResponseDto.class)
.flatMap(SendImageResponseDto -> {
log.info("Get result image -> {}", LocalDateTime.now());
if(giftId == SendImageResponseDto.getGiftId()){
Gift updateGift = giftRepository.findById(giftId)
.orElseThrow(() -> new IllegalArgumentException("There is no gift"));
updateGift.updateImageUrl(SendImageResponseDto.getResultUrl());
log.info("Update Gift image -> {}", LocalDateTime.now());
return Mono.just(SendImageResponseDto.getGiftId());
}
else{
log.info("Fail to Update Gift image -> {}", LocalDateTime.now());
return Mono.just(0);
}
})
.subscribe();
}
}
Loading