Skip to content

Commit

Permalink
fix: InputStreamResource를 ByteArrayResource로 변경하여 스트림 재사용 문제 해결
Browse files Browse the repository at this point in the history
- InputStreamResource는 한 번만 읽을 수 있어 Content-Length 계산과 실제 요청 시 문제 발생
- ByteArrayResource로 변경하여 여러 번 읽기 가능하도록 수정
  • Loading branch information
yugyeom-ghim committed Nov 4, 2024
1 parent 8d55a03 commit 5071eb1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/notai/client/ai/AiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import notai.client.ai.request.LlmTaskRequest;
import notai.client.ai.response.TaskResponse;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
Expand All @@ -14,5 +14,5 @@ public interface AiClient {
TaskResponse submitLlmTask(@RequestBody LlmTaskRequest request);

@PostExchange(url = "/api/ai/stt", contentType = MediaType.MULTIPART_FORM_DATA_VALUE)
TaskResponse submitSttTask(@RequestPart("audio") InputStreamResource audioFile);
TaskResponse submitSttTask(@RequestPart("audio") ByteArrayResource audioFile);
}
10 changes: 6 additions & 4 deletions src/main/java/notai/stt/application/SttTaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import notai.stt.domain.SttRepository;
import notai.sttTask.domain.SttTask;
import notai.sttTask.domain.SttTaskRepository;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;

@Service
@Transactional
Expand All @@ -36,8 +36,10 @@ public void submitSttTask(SttRequestCommand command) {
Recording recording = recordingRepository.getById(command.recordingId());
File audioFile = validateAudioFile(command.audioFilePath());

try (FileInputStream fileInputStream = new FileInputStream(audioFile)) {
InputStreamResource resource = new InputStreamResource(fileInputStream) {
try {
byte[] audioBytes = Files.readAllBytes(audioFile.toPath());

ByteArrayResource resource = new ByteArrayResource(audioBytes) {
@Override
public String getFilename() {
return audioFile.getName();
Expand Down

0 comments on commit 5071eb1

Please sign in to comment.