Skip to content

Commit

Permalink
🔀 충돌 해결
Browse files Browse the repository at this point in the history
junhaa committed May 7, 2024
1 parent 53a13a5 commit 4285c6c
Showing 3 changed files with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package fairytale.tbd.domain.faceSwap.service;

import fairytale.tbd.domain.faceSwap.web.dto.FaceSwapRequestDto;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -15,6 +18,8 @@
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Slf4j
@Service
@@ -25,12 +30,13 @@ public class FaceSwapApiServiceImpl implements FaceSwapApiService{
@Value("${face.akool.apikey}")
private String apikey;


@Value("${face.akool.clientId}")
private String clientId;

private static final Logger LOGGER = LogManager.getLogger(FaceSwapApiServiceImpl.class);

private final ObjectMapper objectMapper;

@Override
@Transactional
public Map<String, String> getFaceSwapImg(FaceSwapRequestDto.FaceSwapRequest faceSwapRequest) throws IOException{
@@ -39,7 +45,8 @@ public Map<String, String> getFaceSwapImg(FaceSwapRequestDto.FaceSwapRequest fac
String customCharacterUrl = "";
String customCharacterId = "";

OkHttpClient client = new OkHttpClient().newBuilder().build();
OkHttpClient client = new OkHttpClient().newBuilder()
.build();

MediaType mediaType = MediaType.parse("application/json");

@@ -56,7 +63,7 @@ public Map<String, String> getFaceSwapImg(FaceSwapRequestDto.FaceSwapRequest fac
" \"opts\": \"" + faceSwapRequest.getTargetImage() + "\"\n" +
" }\n" +
" ],\n" +
" \"face_enhance\": 1" + ",\n" +
" \"face_enhance\": 1" + ",\n" + // 나중에 쉼표 넣기
" \"modifyImage\": \"" + faceSwapRequest.getModifyImage() + "\",\n" +
" \"webhookUrl\": \"" + faceSwapRequest.getWebhookUrl() + "\"\n" +
"}";
@@ -103,46 +110,30 @@ public Map<String, String> getFaceSwapImg(FaceSwapRequestDto.FaceSwapRequest fac
return apiResponse;
}

private String getToken() {
String accessToken = "";
public String getToken() {

OkHttpClient client = new OkHttpClient().newBuilder().build();
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");

JSONObject jsonBody = new JSONObject();
jsonBody.put("clientId", clientId);
jsonBody.put("clientSecret", apikey);

RequestBody body = RequestBody.create(mediaType, jsonBody.toString());

RequestBody body = RequestBody.create(mediaType,
"{\r\n \"clientId\": \"" + clientId + "\" ,\r\n \"clientSecret\": \"" + apikey + "\"\r\n}");
Request request = new Request.Builder()
.url("https://openapi.akool.com/api/open/v3/getToken")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();

try (Response response = client.newCall(request).execute()) {
String responseData = response.body().string();

LOGGER.info("response : {}", responseData);

JSONObject jsonObject = new JSONObject(responseData);

int errCode = jsonObject.getInt("code");
if (errCode != 1000) {
throw new IOException("Error! \n" +
"error code : " +
errCode + "\n");
}

accessToken = jsonObject.getString("token");
.url("https://openapi.akool.com/api/open/v3/getToken")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
try {
Response response = client.newCall(request).execute();
String responseBody = response.body().string();
JsonNode jsonNode = objectMapper.readTree(responseBody);
String token = jsonNode.get("token").asText();
LOGGER.info("token = {}", token);
return token;
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("getToken() 요청 중 에러 발생");
throw new RuntimeException(e);
}

LOGGER.info("access Token from AKOOL : {}", accessToken);
return accessToken;
}

}

Original file line number Diff line number Diff line change
@@ -3,12 +3,14 @@
import fairytale.tbd.domain.faceSwap.service.FaceDetectApiServiceImpl;
import fairytale.tbd.domain.faceSwap.service.FaceSwapApiServiceImpl;
import fairytale.tbd.domain.faceSwap.service.PhotoUploadServiceImpl;
import fairytale.tbd.domain.faceSwap.util.CryptoUtils;
import fairytale.tbd.domain.faceSwap.web.dto.FaceDetectRequestDto;
import fairytale.tbd.domain.faceSwap.web.dto.FaceDetectResponseDto;
import fairytale.tbd.domain.faceSwap.web.dto.FaceSwapRequestDto;
import fairytale.tbd.domain.faceSwap.web.dto.FaceSwapResponseDto;
import fairytale.tbd.domain.faceSwap.web.dto.WebhookRequestDTO;
import fairytale.tbd.domain.user.entity.User;
import fairytale.tbd.global.annotation.LoginUser;
import fairytale.tbd.global.aws.s3.AmazonS3Manager;
import fairytale.tbd.global.response.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.Logger;
@@ -30,24 +32,28 @@ public class FaceSwapRestController {
private final PhotoUploadServiceImpl photoUploadService;
private final FaceDetectApiServiceImpl faceDetectApiService;
private final FaceSwapApiServiceImpl faceSwapApiService;
private final CryptoUtils cryptoUtils;

@PostMapping("/uploadImg")
public ApiResponse<FaceDetectRequestDto> uploadImg(@LoginUser User user, @ModelAttribute MultipartFile file) throws IOException{
public ApiResponse<FaceDetectResponseDto> uploadImg(@LoginUser User user, @ModelAttribute MultipartFile file) throws IOException{
FaceDetectRequestDto faceDetectRequestDto = photoUploadService.savePhotos(user, file);
return ApiResponse.onSuccess(faceDetectRequestDto);
String imgURL = faceDetectRequestDto.getImgURL();
return test(imgURL);
}


@PostMapping("/test")
public ApiResponse<FaceDetectResponseDto> test(@RequestBody FaceDetectRequestDto faceDetectRequestDto){

public ApiResponse<FaceDetectResponseDto> test(@RequestBody String faceDetectRequestDto){

LOGGER.info("Face detect request : {}", faceDetectRequestDto);

FaceDetectRequestDto requestDto = new FaceDetectRequestDto();
requestDto.setImgURL(faceDetectRequestDto);

FaceDetectResponseDto optFromFaceDetectApi = null;

try{
optFromFaceDetectApi = faceDetectApiService.getOptFromFaceDetectApi(faceDetectRequestDto);
optFromFaceDetectApi = faceDetectApiService.getOptFromFaceDetectApi(requestDto);
}
catch( Exception e){
e.printStackTrace();
@@ -58,7 +64,7 @@ public ApiResponse<FaceDetectResponseDto> test(@RequestBody FaceDetectRequestDto
}

@PostMapping("/testSwapApi")
public ApiResponse<Map<String, String>> test(@RequestBody FaceSwapRequestDto.FaceSwapRequest faceSwapRequestDto, BindingResult bindingResult){
public ApiResponse<Map<String, String>> test(@RequestBody FaceSwapRequestDto.FaceSwapRequest faceSwapRequestDto){

Map<String, String> test = new HashMap<>();

@@ -72,4 +78,9 @@ public ApiResponse<Map<String, String>> test(@RequestBody FaceSwapRequestDto.Fac

return ApiResponse.onSuccess(test);
}
}

@PostMapping("/webhook")
public void webhook(@ModelAttribute WebhookRequestDTO.RequestDTO request){
LOGGER.info("request = {}", request);
}
}
Original file line number Diff line number Diff line change
@@ -11,9 +11,9 @@ public class FaceSwapRequestDto {
@Setter
public static class FaceSwapRequest {

private List<SourceImage> sourceImage;
private SourceImage sourceImage;

private List<TargetImage> targetImage;
private TargetImage targetImage;

private String modifyImage;

@@ -41,4 +41,4 @@ public static class TargetImage {
private String targetOpts;

}
}
}

0 comments on commit 4285c6c

Please sign in to comment.