Skip to content

Commit

Permalink
Merge pull request #109 from tukcomCD2024/feat#104/elevenlabs-API-add…
Browse files Browse the repository at this point in the history
…ition-and-modification

Feat#104/elevenlabs api addition and modification
  • Loading branch information
SeungEEE authored Jun 19, 2024
2 parents 502c888 + 09e8c29 commit 8106371
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.memetory.domain.voice.controller;

import com.example.memetory.domain.voice.dto.request.GenerateVoiceRequestDto;
import com.example.memetory.domain.voice.dto.response.ElevenlabsVoiceLibraryResponse;
import com.example.memetory.domain.voice.dto.response.ElevenlabsVoiceResponse;
import com.example.memetory.global.response.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -13,6 +11,7 @@
import org.springframework.http.ResponseEntity;

import java.io.IOException;
import java.rmi.AlreadyBoundException;

@Tag(name = "Voice")
public interface VoiceApi {
Expand All @@ -31,7 +30,7 @@ public interface VoiceApi {
ResponseEntity<ResultResponse> register(
@Parameter(hidden = true) String email,
GenerateVoiceRequestDto generateVoiceRequestDto
) throws IOException;
) throws IOException, AlreadyBoundException;

@Operation(
summary = "멤버별 목소리 조회",
Expand All @@ -46,18 +45,20 @@ ResponseEntity<ResultResponse> register(
})
ResponseEntity<ResultResponse> findByMemberId(
@Parameter(hidden = true) String email
) throws IOException;
);

@Operation(
summary = "기본 라이브러리 목소리 조회",
description = "기본 라이브러리 목소리를 조회한다.",
summary = "목소리 삭제",
description = "voice_id에 해당하는 목소리를 삭제한다.",
security = {@SecurityRequirement(name = "access_token")}
)
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "기본 라이브러리 목소리 조회!"
description = "목소리 삭제!"
)
})
ResponseEntity<ResultResponse> getVoiceLibrary() throws IOException;
void deleteVoice(
@Parameter(hidden = true) String email
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.example.memetory.domain.voice.dto.VoiceServiceDto;
import com.example.memetory.domain.voice.dto.request.GenerateVoiceRequestDto;
import com.example.memetory.domain.voice.dto.response.ElevenlabsVoiceLibraryResponse;
import com.example.memetory.domain.voice.dto.response.ElevenlabsVoiceResponse;
import com.example.memetory.domain.voice.dto.response.GenerateVoiceResponseDto;
import com.example.memetory.domain.voice.service.VoiceService;
import com.example.memetory.global.annotation.LoginMemberEmail;
Expand Down Expand Up @@ -36,18 +34,22 @@ public class VoiceController implements VoiceApi {
@Value("${elevenlabs.api.url.get}")
private String getApiUrl;

@Value("${elevenlabs.api.url.library}")
private String getLibraryApiUrl;
@Value("${elevenlabs.api.url.delete}")
private String deleteApiUrl;

@Value("${elevenlabs.api.key}")
private String apiKey;

// 보이스 추출 및 생성
@PostMapping
@Override
public ResponseEntity<ResultResponse> register(@LoginMemberEmail String email, @RequestBody GenerateVoiceRequestDto generateVoiceRequestDto) throws IOException {
public ResponseEntity<ResultResponse> register(@LoginMemberEmail String email, @RequestBody GenerateVoiceRequestDto generateVoiceRequestDto) throws IOException{

// s3 url을 받아서 음성파일을 가져오는 ServiceDto 로직 1개
VoiceServiceDto voiceServiceDtoS3 = generateVoiceRequestDto.toServiceDtoS3(email);

// 보이스가 이미 존재하는지 체크
voiceService.isExistVoice(voiceServiceDtoS3);
MultiValueMap<String, Object> formData = voiceService.generateVoice(voiceServiceDtoS3);

return WebClient
Expand All @@ -72,9 +74,10 @@ public ResponseEntity<ResultResponse> register(@LoginMemberEmail String email, @
.block();
}

// 멤버별 보이스 조회
@GetMapping("/member")
@Override
public ResponseEntity<ResultResponse> findByMemberId(@LoginMemberEmail String email) throws IOException {
public ResponseEntity<ResultResponse> findByMemberId(@LoginMemberEmail String email){
VoiceServiceDto voiceServiceDto = VoiceServiceDto.create(email);
String voiceId = voiceService.findVoiceByMemberId(voiceServiceDto);

Expand All @@ -95,23 +98,19 @@ public ResponseEntity<ResultResponse> findByMemberId(@LoginMemberEmail String em
.block();
}

@GetMapping("/library")
@DeleteMapping("/member")
@Override
public ResponseEntity<ResultResponse> getVoiceLibrary() throws IOException {
return WebClient
.create(getLibraryApiUrl)
.get()
public void deleteVoice(@LoginMemberEmail String email) {
VoiceServiceDto voiceServiceDto = VoiceServiceDto.create(email);
String voiceId = voiceService.findVoiceByMemberId(voiceServiceDto);

WebClient.create(deleteApiUrl + "/" + voiceId)
.delete()
.header("xi-api-key", apiKey)
.retrieve()
.bodyToMono(ElevenlabsVoiceLibraryResponse.class)
.flatMap(response -> {
// elevenlabs API 호출이 완료 되면 실행할 로직
return Mono.just(ResponseEntity.ok(ResultResponse.of(ResultCode.GET_VOICE_LIBRARY_SUCCESS, response)));
})
.onErrorResume(error -> {
System.out.println(("An error occurred while processing the request: {}" + error.getMessage()));
return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
})
.block();
.bodyToMono(Void.class)
.subscribe();

voiceService.deleteVoice(voiceServiceDto);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.memetory.domain.voice.exception;

import com.example.memetory.global.exception.BusinessException;
import com.example.memetory.global.response.ErrorCode;

public class AlreadyExistVoiceException extends BusinessException {
public AlreadyExistVoiceException() {
super(ErrorCode.VOICE_ALREADY_EXIST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
import com.example.memetory.domain.voice.entity.Voice;
import org.springframework.data.jpa.repository.JpaRepository;

public interface VoiceRepository extends JpaRepository<Voice, Long>, VoiceQueryRepository{
import java.util.Optional;

public interface VoiceRepository extends JpaRepository<Voice, Long>, VoiceQueryRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.memetory.domain.member.service.MemberService;
import com.example.memetory.domain.voice.dto.VoiceServiceDto;
import com.example.memetory.domain.voice.entity.Voice;
import com.example.memetory.domain.voice.exception.AlreadyExistVoiceException;
import com.example.memetory.domain.voice.exception.NotFoundVoiceException;
import com.example.memetory.domain.voice.repository.VoiceRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,6 +18,7 @@
import org.springframework.util.MultiValueMap;

import java.io.*;
import java.util.Optional;


@Service
Expand All @@ -40,19 +42,37 @@ public void register(VoiceServiceDto voiceServiceDto) {

@Transactional(readOnly = true)
public String findVoiceByMemberId(VoiceServiceDto voiceServiceDto) {
Member foundMember = memberService.findMemberFromEmail(voiceServiceDto.getEmail());
Voice foundVoice = voiceRepository.findByMemberId(foundMember.getId()).orElseThrow(NotFoundVoiceException::new);

Voice foundVoice = findVoiceByMemberEmail(voiceServiceDto);
return foundVoice.getElevenlabsVoiceId();
}

public void deleteVoice(VoiceServiceDto voiceServiceDto) {
Voice foundVoice = findVoiceByMemberEmail(voiceServiceDto);
voiceRepository.delete(foundVoice);
}

private Voice findVoiceByMemberEmail(VoiceServiceDto voiceServiceDto) {
Member foundMember = memberService.findMemberFromEmail(voiceServiceDto.getEmail());
return voiceRepository.findByMemberId(foundMember.getId()).orElseThrow(NotFoundVoiceException::new);
}

// 목소리 생성
public MultiValueMap<String, Object> generateVoice(VoiceServiceDto voiceServiceDto) throws IOException {
S3Object s3Object = getS3File(voiceServiceDto);

return createFormData(s3Object, voiceServiceDto);
}

// 목소리가 이미 생성되어 있는지 체크
public void isExistVoice(VoiceServiceDto voiceServiceDto) {
Member foundMember = memberService.findMemberFromEmail(voiceServiceDto.getEmail());
Optional<Voice> foundVoice = voiceRepository.findByMemberId(foundMember.getId());

if (foundVoice.isPresent()) {
throw new AlreadyExistVoiceException();
}
}

// S3 파일 가져오기
private S3Object getS3File(VoiceServiceDto voiceServiceDto) {
return amazonS3Client.getObject(bucketName, voiceServiceDto.getS3Key());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum ErrorCode {

// Voice
VOICE_NOT_FOUND(404, "보이스 찾기 실패"),
VOICE_ALREADY_EXIST(404, "보이스 이미 존재")
;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum ResultCode {

// voice
CREATE_VOICE_SUCCESS(201, "목소리 생성 성공"),
DELETE_VOICE_SUCCESS(200, "목소리 삭제 성공"),
GET_MEMBER_VOICE_SUCCESS(200, "멤버별 목소리 조회 성공"),
GET_VOICE_LIBRARY_SUCCESS(200, "기본 목소리 라이브러리 조회 성공"),
;
Expand Down

0 comments on commit 8106371

Please sign in to comment.