-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathVideoHelperService.js
More file actions
322 lines (283 loc) · 10 KB
/
Copy pathVideoHelperService.js
File metadata and controls
322 lines (283 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* VideoHelperService - 로컬 헬퍼 프로그램과 통신하는 서비스
*
* 헬퍼 프로그램은 YouTube 영상을 다운로드하여 로컬에서 제공합니다.
* API 엔드포인트: localhost:15123
*/
const VideoHelperService = (() => {
const BASE_URL = "http://localhost:15123";
const DOWNLOAD_URL = "https://ivlis.kr/ivLyrics/extensions/#helper";
// 연결 상태
let isConnected = false;
let lastHealthCheck = 0;
const HEALTH_CHECK_INTERVAL = 30000; // 30초
/**
* 헬퍼 서버 상태 확인
* @returns {Promise<boolean>} 연결 여부
*/
const checkHealth = async () => {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 3000);
const response = await fetch(`${BASE_URL}/health`, {
signal: controller.signal,
});
clearTimeout(timeoutId);
isConnected = response.ok && (await response.text()) === "OK";
lastHealthCheck = Date.now();
return isConnected;
} catch (e) {
isConnected = false;
lastHealthCheck = Date.now();
return false;
}
};
/**
* 캐시된 연결 상태 반환 (일정 시간 이내면 캐시 사용)
* @returns {Promise<boolean>}
*/
const isHelperAvailable = async () => {
if (Date.now() - lastHealthCheck < HEALTH_CHECK_INTERVAL) {
return isConnected;
}
return await checkHealth();
};
/**
* 비디오 상태 확인 (단순 조회)
* @param {string} videoId - YouTube 비디오 ID
* @returns {Promise<{success: boolean, url: string|null, message: string}>}
*/
const getVideoStatus = async (videoId) => {
if (!videoId) {
return { success: false, url: null, message: "Invalid video ID" };
}
try {
const response = await fetch(`${BASE_URL}/video/status?id=${encodeURIComponent(videoId)}`);
const data = await response.json();
return {
success: data.success,
url: data.url,
message: data.message,
videoId: data.video_id,
};
} catch (e) {
return { success: false, url: null, message: "Failed to check video status" };
}
};
/**
* 비디오 요청 (SSE 스트림으로 다운로드 진행 상황 받기)
* @param {string} videoId - YouTube 비디오 ID
* @param {object} callbacks - 콜백 함수들
* @param {function} callbacks.onProgress - 진행 상황 콜백 (percent, speed, eta, message, status)
* @param {function} callbacks.onComplete - 완료 콜백 (url)
* @param {function} callbacks.onError - 에러 콜백 (message)
* @returns {function} abort 함수
*/
const requestVideo = (videoId, callbacks = {}) => {
const { onProgress, onComplete, onError } = callbacks;
let aborted = false;
if (!videoId) {
onError?.("Invalid video ID");
return () => { };
}
const controller = new AbortController();
const fetchVideo = async () => {
try {
const response = await fetch(`${BASE_URL}/video/request?id=${encodeURIComponent(videoId)}`, {
signal: controller.signal,
});
const contentType = response.headers.get("content-type") || "";
// JSON 응답 (이미 존재하는 경우)
if (contentType.includes("application/json")) {
const data = await response.json();
if (aborted) return;
if (data.success) {
onComplete?.(data.url);
} else {
onError?.(data.message || "Video request failed");
}
return;
}
// SSE 스트림 (다운로드 진행 중)
if (contentType.includes("text/event-stream")) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let pendingData = null; // data가 먼저 오고 event가 나중에 오는 형식 대응
while (true) {
const { done, value } = await reader.read();
if (done || aborted) break;
buffer += decoder.decode(value, { stream: true });
// 줄 단위로 처리
const lines = buffer.split("\n");
buffer = lines.pop() || ""; // 마지막 불완전한 줄은 버퍼에 유지
for (const line of lines) {
const trimmedLine = line.trim();
// 빈 줄이면 이벤트 끝
if (!trimmedLine) {
pendingData = null;
continue;
}
// data: 줄 처리 (먼저 옴)
if (trimmedLine.startsWith("data:")) {
const dataStr = trimmedLine.slice(5).trim();
if (!dataStr) continue;
try {
pendingData = JSON.parse(dataStr);
} catch (e) {
pendingData = null;
}
continue;
}
// event: 줄 처리 (나중에 옴)
if (trimmedLine.startsWith("event:")) {
const eventType = trimmedLine.slice(6).trim();
if (!pendingData) continue;
const data = pendingData;
pendingData = null;
// progress 이벤트
if (eventType === "progress") {
onProgress?.({
percent: data.percent || 0,
speed: data.speed,
eta: data.eta,
message: data.message,
status: data.status,
});
}
// complete 이벤트
else if (eventType === "complete") {
// status가 completed이고 message가 URL인 경우에만 완료 처리
if (data.status === "completed") {
const videoUrl = data.url || data.message;
// URL 형식인지 확인 (http로 시작하는 경우만)
if (videoUrl && typeof videoUrl === 'string' && videoUrl.startsWith('http')) {
window.__ivLyricsDebugLog?.("[VideoHelperService] Download complete, URL:", videoUrl);
onComplete?.(videoUrl);
return;
}
// "Download completed" 같은 메시지면 다음 이벤트 기다림
}
// status가 error지만 WARNING 메시지면 무시하고 계속 진행
else if (data.status === "error") {
const msg = data.message || "";
if (msg.startsWith("WARNING")) {
window.__ivLyricsDebugLog?.("[VideoHelperService] Ignoring warning:", msg);
// WARNING은 무시하고 계속 진행
} else {
// 진짜 에러
onError?.(msg || "Download failed");
return;
}
}
}
// error 이벤트
else if (eventType === "error") {
onError?.(data.message || "Download failed");
return;
}
}
}
}
// 스트림이 끝났는데 완료 콜백이 호출되지 않은 경우
// 버퍼에 남은 데이터 처리
if (buffer.trim()) {
const dataMatch = buffer.match(/data:\s*({.*})/);
if (dataMatch) {
try {
const data = JSON.parse(dataMatch[1]);
if (data.status === "completed") {
const videoUrl = data.url || data.message;
if (videoUrl && videoUrl.startsWith('http')) {
window.__ivLyricsDebugLog?.("[VideoHelperService] Final buffer complete, URL:", videoUrl);
onComplete?.(videoUrl);
return;
}
}
} catch (e) {
// ignore
}
}
}
} else {
// 알 수 없는 응답 타입
const text = await response.text();
if (!aborted) {
onError?.("Unexpected response: " + text.substring(0, 100));
}
}
} catch (e) {
if (aborted) return;
if (e.name === "AbortError") return;
console.error("[VideoHelperService] Request error:", e);
onError?.(e.message || "Request failed");
}
};
fetchVideo();
// abort 함수 반환
return () => {
aborted = true;
controller.abort();
};
};
/**
* 비디오 파일 URL 생성
* @param {string} videoId - YouTube 비디오 ID
* @returns {string} 비디오 파일 URL
*/
const getVideoFileUrl = (videoId) => {
return `${BASE_URL}/video/files/${encodeURIComponent(videoId)}.webm`;
};
/**
* 헬퍼 프로그램 다운로드 URL 반환
* @returns {string}
*/
const getDownloadUrl = () => {
return DOWNLOAD_URL;
};
/**
* 헬퍼 프로그램 다운로드 페이지 열기
*/
const openDownloadPage = () => {
window.open(DOWNLOAD_URL, "_blank");
};
/**
* YouTube 비디오 ID 추출
* @param {string} url - YouTube URL 또는 비디오 ID
* @returns {string|null} 비디오 ID
*/
const extractVideoId = (url) => {
if (!url) return null;
// 이미 비디오 ID인 경우 (11자 영숫자+하이픈+언더스코어)
if (/^[a-zA-Z0-9_-]{11}$/.test(url)) {
return url;
}
// 다양한 YouTube URL 형식 지원
const patterns = [
/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/)([a-zA-Z0-9_-]{11})/,
/youtube\.com\/shorts\/([a-zA-Z0-9_-]{11})/,
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match) return match[1];
}
return null;
};
// 공개 API
return {
checkHealth,
isHelperAvailable,
getVideoStatus,
requestVideo,
getVideoFileUrl,
getDownloadUrl,
openDownloadPage,
extractVideoId,
get isConnected() {
return isConnected;
},
BASE_URL,
};
})();
// 전역으로 노출
window.VideoHelperService = VideoHelperService;