front 작업 중 .
This commit is contained in:
1 parent
ecbd81d091
commit
24d61f925a
8 files changed
+796
-124
No files matched your search
+233
-46
@@ -2,10 +2,17 @@ import {
|
||||
CrawlingResponse,
|
||||
LyricGenerateRequest,
|
||||
LyricGenerateResponse,
|
||||
LyricStatusResponse,
|
||||
LyricDetailResponse,
|
||||
SongGenerateRequest,
|
||||
SongGenerateResponse,
|
||||
SongStatusResponse,
|
||||
SongDownloadResponse,
|
||||
VideoGenerateResponse,
|
||||
VideoStatusResponse,
|
||||
VideoDownloadResponse,
|
||||
ImageUrlItem,
|
||||
ImageUploadResponse,
|
||||
} from '../types/api';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'http://40.82.133.44';
|
||||
@@ -43,9 +50,75 @@ export async function generateLyric(request: LyricGenerateRequest): Promise<Lyri
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 생성 API
|
||||
export async function generateSong(request: SongGenerateRequest): Promise<SongGenerateResponse> {
|
||||
const response = await fetch(`${API_URL}/song/generate`, {
|
||||
// 가사 상태 조회 API
|
||||
export async function getLyricStatus(taskId: string): Promise<LyricStatusResponse> {
|
||||
const response = await fetch(`${API_URL}/lyric/status/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 가사 상세 조회 API
|
||||
export async function getLyricDetail(taskId: string): Promise<LyricDetailResponse> {
|
||||
const response = await fetch(`${API_URL}/lyric/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 가사 생성 완료까지 폴링 (2분 타임아웃, 1초 간격)
|
||||
const LYRIC_POLL_TIMEOUT = 2 * 60 * 1000; // 2분
|
||||
const LYRIC_POLL_INTERVAL = 1000; // 1초
|
||||
|
||||
export async function waitForLyricComplete(
|
||||
taskId: string,
|
||||
onStatusChange?: (status: string) => void
|
||||
): Promise<LyricDetailResponse> {
|
||||
const startTime = Date.now();
|
||||
|
||||
// 재귀적으로 폴링하는 방식으로 변경 (async/await 제대로 동작)
|
||||
const poll = async (): Promise<LyricDetailResponse> => {
|
||||
// 2분 타임아웃 체크
|
||||
if (Date.now() - startTime > LYRIC_POLL_TIMEOUT) {
|
||||
throw new Error('TIMEOUT');
|
||||
}
|
||||
|
||||
try {
|
||||
const statusResponse = await getLyricStatus(taskId);
|
||||
onStatusChange?.(statusResponse.status);
|
||||
|
||||
if (statusResponse.status === 'completed') {
|
||||
// 완료되면 상세 조회로 가사 가져오기
|
||||
const detailResponse = await getLyricDetail(taskId);
|
||||
return detailResponse;
|
||||
} else if (statusResponse.status === 'failed') {
|
||||
throw new Error(statusResponse.error_message || '가사 생성에 실패했습니다.');
|
||||
}
|
||||
|
||||
// processing은 대기 후 재시도
|
||||
await new Promise(resolve => setTimeout(resolve, LYRIC_POLL_INTERVAL));
|
||||
return poll();
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
return poll();
|
||||
}
|
||||
|
||||
// 노래 생성 API (task_id는 URL 경로에 포함)
|
||||
export async function generateSong(taskId: string, request: SongGenerateRequest): Promise<SongGenerateResponse> {
|
||||
const response = await fetch(`${API_URL}/song/generate/${taskId}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -60,9 +133,9 @@ export async function generateSong(request: SongGenerateRequest): Promise<SongGe
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 상태 확인 API
|
||||
export async function getSongStatus(taskId: string): Promise<SongStatusResponse> {
|
||||
const response = await fetch(`${API_URL}/song/status/${taskId}`, {
|
||||
// 노래 상태 확인 API (suno_task_id 사용)
|
||||
export async function getSongStatus(sunoTaskId: string): Promise<SongStatusResponse> {
|
||||
const response = await fetch(`${API_URL}/song/status/${sunoTaskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
@@ -92,51 +165,165 @@ const POLL_INTERVAL = 5000; // 5초
|
||||
|
||||
export async function waitForSongComplete(
|
||||
taskId: string,
|
||||
sunoTaskId: string,
|
||||
onStatusChange?: (status: string) => void
|
||||
): Promise<SongDownloadResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const startTime = Date.now();
|
||||
const startTime = Date.now();
|
||||
|
||||
const pollInterval = setInterval(async () => {
|
||||
try {
|
||||
// 2분 타임아웃 체크
|
||||
if (Date.now() - startTime > POLL_TIMEOUT) {
|
||||
clearInterval(pollInterval);
|
||||
reject(new Error('TIMEOUT'));
|
||||
return;
|
||||
}
|
||||
// 재귀적으로 폴링하는 방식으로 변경 (async/await 제대로 동작)
|
||||
const poll = async (): Promise<SongDownloadResponse> => {
|
||||
// 2분 타임아웃 체크
|
||||
if (Date.now() - startTime > POLL_TIMEOUT) {
|
||||
throw new Error('TIMEOUT');
|
||||
}
|
||||
|
||||
const statusResponse = await getSongStatus(taskId);
|
||||
onStatusChange?.(statusResponse.status);
|
||||
try {
|
||||
// 상태 확인은 suno_task_id 사용
|
||||
const statusResponse = await getSongStatus(sunoTaskId);
|
||||
onStatusChange?.(statusResponse.status);
|
||||
|
||||
// status가 "SUCCESS" (대문자)인 경우 완료
|
||||
if (statusResponse.status === 'SUCCESS' && statusResponse.success) {
|
||||
clearInterval(pollInterval);
|
||||
|
||||
// clips에서 첫 번째 오디오 URL 가져오기
|
||||
if (statusResponse.clips && statusResponse.clips.length > 0) {
|
||||
const clip = statusResponse.clips[0];
|
||||
resolve({
|
||||
success: true,
|
||||
message: statusResponse.message || '노래 생성이 완료되었습니다.',
|
||||
file_path: '',
|
||||
file_url: clip.audio_url || clip.stream_audio_url,
|
||||
error_message: null,
|
||||
});
|
||||
} else {
|
||||
// clips가 없으면 download API 호출
|
||||
const downloadResponse = await downloadSong(taskId);
|
||||
resolve(downloadResponse);
|
||||
}
|
||||
} else if (statusResponse.status === 'FAILED' || statusResponse.status === 'failed') {
|
||||
clearInterval(pollInterval);
|
||||
reject(new Error('Song generation failed'));
|
||||
}
|
||||
// PENDING, PROCESSING 등은 계속 폴링
|
||||
} catch (error) {
|
||||
clearInterval(pollInterval);
|
||||
reject(error);
|
||||
// status가 "SUCCESS" (대문자)인 경우 완료
|
||||
if (statusResponse.status === 'SUCCESS' && statusResponse.success) {
|
||||
// 다운로드는 task_id 사용
|
||||
const downloadResponse = await downloadSong(taskId);
|
||||
return downloadResponse;
|
||||
} else if (statusResponse.status === 'FAILED' || statusResponse.status === 'failed') {
|
||||
throw new Error('Song generation failed');
|
||||
}
|
||||
}, POLL_INTERVAL);
|
||||
|
||||
// PENDING, PROCESSING 등은 대기 후 재시도
|
||||
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL));
|
||||
return poll();
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
return poll();
|
||||
}
|
||||
|
||||
// 영상 생성 API
|
||||
export async function generateVideo(taskId: string, orientation: 'vertical' | 'horizontal' = 'vertical'): Promise<VideoGenerateResponse> {
|
||||
const response = await fetch(`${API_URL}/video/generate/${taskId}?orientation=${orientation}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 영상 상태 확인 API
|
||||
export async function getVideoStatus(taskId: string): Promise<VideoStatusResponse> {
|
||||
const response = await fetch(`${API_URL}/video/status/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 영상 다운로드(결과 조회) API
|
||||
export async function downloadVideo(taskId: string): Promise<VideoDownloadResponse> {
|
||||
const response = await fetch(`${API_URL}/video/download/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 이미지 업로드 API (multipart/form-data)
|
||||
// 타임아웃: 5분 (많은 이미지 업로드 시 시간이 오래 걸릴 수 있음)
|
||||
const IMAGE_UPLOAD_TIMEOUT = 5 * 60 * 1000;
|
||||
|
||||
export async function uploadImages(
|
||||
imageUrls: ImageUrlItem[],
|
||||
files: File[]
|
||||
): Promise<ImageUploadResponse> {
|
||||
const formData = new FormData();
|
||||
|
||||
// URL 이미지들을 images_json으로 전달
|
||||
if (imageUrls.length > 0) {
|
||||
formData.append('images_json', JSON.stringify(imageUrls));
|
||||
}
|
||||
|
||||
// 파일들을 files로 전달
|
||||
files.forEach((file) => {
|
||||
formData.append('files', file);
|
||||
});
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), IMAGE_UPLOAD_TIMEOUT);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/image/upload/blob`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
throw new Error('이미지 업로드 시간이 초과되었습니다. 다시 시도해주세요.');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 영상 생성 완료까지 폴링 (3분 타임아웃, 3초 간격)
|
||||
const VIDEO_POLL_TIMEOUT = 3 * 60 * 1000; // 3분
|
||||
const VIDEO_POLL_INTERVAL = 3000; // 3초
|
||||
|
||||
export async function waitForVideoComplete(
|
||||
taskId: string,
|
||||
onStatusChange?: (status: string) => void
|
||||
): Promise<VideoStatusResponse> {
|
||||
const startTime = Date.now();
|
||||
|
||||
// 재귀적으로 폴링하는 방식으로 변경 (async/await 제대로 동작)
|
||||
const poll = async (): Promise<VideoStatusResponse> => {
|
||||
// 3분 타임아웃 체크
|
||||
if (Date.now() - startTime > VIDEO_POLL_TIMEOUT) {
|
||||
throw new Error('TIMEOUT');
|
||||
}
|
||||
|
||||
try {
|
||||
const statusResponse = await getVideoStatus(taskId);
|
||||
// render_data.status를 전달 (planned, waiting, transcribing, rendering, succeeded, failed)
|
||||
const renderStatus = statusResponse.render_data?.status;
|
||||
onStatusChange?.(renderStatus || statusResponse.status);
|
||||
|
||||
// render_data.status가 "succeeded"일 때만 완료
|
||||
if (renderStatus === 'succeeded') {
|
||||
return statusResponse;
|
||||
} else if (renderStatus === 'failed' || statusResponse.status === 'FAILED' || statusResponse.status === 'failed') {
|
||||
throw new Error(statusResponse.error_message || 'Video generation failed');
|
||||
}
|
||||
|
||||
// pending, rendering 등은 대기 후 재시도
|
||||
await new Promise(resolve => setTimeout(resolve, VIDEO_POLL_INTERVAL));
|
||||
return poll();
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
return poll();
|
||||
}
|
||||
Reference in new issue
Block a user