castad front 작업
This commit is contained in:
1 parent
aa39cde4c6
commit
a42562279e
30 files changed
+4332
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import {
|
||||
CrawlingResponse,
|
||||
LyricGenerateRequest,
|
||||
LyricGenerateResponse,
|
||||
SongGenerateRequest,
|
||||
SongGenerateResponse,
|
||||
SongStatusResponse,
|
||||
SongDownloadResponse,
|
||||
} from '../types/api';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'http://40.82.133.44';
|
||||
|
||||
export async function crawlUrl(url: string): Promise<CrawlingResponse> {
|
||||
const response = await fetch(`${API_URL}/crawling`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ url }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 가사 생성 API
|
||||
export async function generateLyric(request: LyricGenerateRequest): Promise<LyricGenerateResponse> {
|
||||
const response = await fetch(`${API_URL}/lyric/generate`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(request),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 생성 API
|
||||
export async function generateSong(request: SongGenerateRequest): Promise<SongGenerateResponse> {
|
||||
const response = await fetch(`${API_URL}/song/generate`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(request),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 상태 확인 API
|
||||
export async function getSongStatus(taskId: string): Promise<SongStatusResponse> {
|
||||
const response = await fetch(`${API_URL}/song/status/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 다운로드 API
|
||||
export async function downloadSong(taskId: string): Promise<SongDownloadResponse> {
|
||||
const response = await fetch(`${API_URL}/song/download/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 생성 완료까지 폴링 (2분 타임아웃)
|
||||
const POLL_TIMEOUT = 2 * 60 * 1000; // 2분
|
||||
const POLL_INTERVAL = 5000; // 5초
|
||||
|
||||
export async function waitForSongComplete(
|
||||
taskId: string,
|
||||
onStatusChange?: (status: string) => void
|
||||
): Promise<SongDownloadResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const startTime = Date.now();
|
||||
|
||||
const pollInterval = setInterval(async () => {
|
||||
try {
|
||||
// 2분 타임아웃 체크
|
||||
if (Date.now() - startTime > POLL_TIMEOUT) {
|
||||
clearInterval(pollInterval);
|
||||
reject(new Error('TIMEOUT'));
|
||||
return;
|
||||
}
|
||||
|
||||
const statusResponse = await getSongStatus(taskId);
|
||||
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);
|
||||
}
|
||||
}, POLL_INTERVAL);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user