Skip to content

Commit

Permalink
fix: AI서버 stt요청 multipart formdata 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
yugyeom-ghim committed Nov 4, 2024
1 parent 79647f6 commit 8d55a03
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/main/java/notai/client/ai/AiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import notai.client.ai.request.LlmTaskRequest;
import notai.client.ai.response.TaskResponse;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.service.annotation.PostExchange;

import java.io.InputStream;

public interface AiClient {

@PostExchange(url = "/api/ai/llm")
TaskResponse submitLlmTask(@RequestBody LlmTaskRequest request);

@PostExchange(url = "/api/ai/stt", contentType = MediaType.MULTIPART_FORM_DATA_VALUE)
TaskResponse submitSttTask(@RequestBody InputStream audioFileStream);
TaskResponse submitSttTask(@RequestPart("audio") InputStreamResource audioFile);
}
32 changes: 23 additions & 9 deletions src/main/java/notai/client/ai/AiClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;

import static notai.client.HttpInterfaceUtil.createHttpInterface;
import static notai.common.exception.ErrorMessages.AI_SERVER_ERROR;
Expand All @@ -20,15 +23,26 @@ public class AiClientConfig {

@Bean
public AiClient aiClient() {
RestClient restClient =
RestClient.builder().baseUrl(aiServerUrl).requestInterceptor((request, body, execution) -> {
request.getHeaders().setContentLength(body.length); // Content-Length 설정 안하면 411 에러 발생
return execution.execute(request, body);
}).defaultStatusHandler(HttpStatusCode::isError, (request, response) -> {
String responseBody = new String(response.getBody().readAllBytes());
log.error("Response Status: {}", response.getStatusCode());
throw new ExternalApiException(AI_SERVER_ERROR, response.getStatusCode().value());
}).build();
RestClient restClient = RestClient.builder()
.baseUrl(aiServerUrl)
.messageConverters(converters -> {
converters.addAll(new RestTemplate().getMessageConverters());
converters.add(new FormHttpMessageConverter());
})
.requestInterceptor((request, body, execution) -> {
request.getHeaders().setContentLength(body.length); // Content-Length 설정 안하면 411 에러 발생
return execution.execute(request, body);
})
.defaultStatusHandler(HttpStatusCode::isError, (request, response) -> {
String responseBody = new String(response.getBody().readAllBytes());
log.error("AI 서버에서 오류가 발생했습니다. - Status: {}, Body: {}",
response.getStatusCode(),
responseBody
);
throw new ExternalApiException(AI_SERVER_ERROR, response.getStatusCode().value());
})
.build();

return createHttpInterface(restClient, AiClient.class);
}
}
10 changes: 9 additions & 1 deletion src/main/java/notai/stt/application/SttTaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import notai.stt.domain.SttRepository;
import notai.sttTask.domain.SttTask;
import notai.sttTask.domain.SttTaskRepository;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -36,7 +37,14 @@ public void submitSttTask(SttRequestCommand command) {
File audioFile = validateAudioFile(command.audioFilePath());

try (FileInputStream fileInputStream = new FileInputStream(audioFile)) {
TaskResponse response = aiClient.submitSttTask(fileInputStream);
InputStreamResource resource = new InputStreamResource(fileInputStream) {
@Override
public String getFilename() {
return audioFile.getName();
}
};

TaskResponse response = aiClient.submitSttTask(resource);
createAndSaveSttTask(recording, response);
} catch (IOException e) {
throw new FileProcessException(FILE_READ_ERROR);
Expand Down

0 comments on commit 8d55a03

Please sign in to comment.