Skip to content

Commit c868a75

Browse files
committed
refactor: 다운로드 함수에서 진행률 관련 코드 제거 및 새 탭에서 열기 기능 추가
1 parent fb305cc commit c868a75

File tree

1 file changed

+6
-59
lines changed

1 file changed

+6
-59
lines changed

src/utils/download.ts

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,34 @@
22
* 영상 다운로드 관련 유틸리티 함수들
33
*/
44

5-
interface DownloadProgress {
6-
loaded: number;
7-
total: number;
8-
percentage: number;
9-
}
10-
115
interface DownloadOptions {
126
filename?: string;
13-
onProgress?: (progress: DownloadProgress) => void;
147
onError?: (error: Error) => void;
158
onComplete?: () => void;
169
}
1710

1811
/**
1912
* 영상 URL로부터 파일을 다운로드하는 함수
2013
*/
21-
export async function downloadVideo(
14+
export function downloadVideo(
2215
videoUrl: string,
2316
options: DownloadOptions = {}
24-
): Promise<void> {
25-
const {
26-
filename = `video_${Date.now()}.mp4`,
27-
onProgress,
28-
onError,
29-
onComplete,
30-
} = options;
17+
): void {
18+
const { filename = `video_${Date.now()}.mp4`, onError, onComplete } = options;
3119

3220
try {
33-
// AbortController로 다운로드 취소 가능하게 구현
34-
const controller = new AbortController();
35-
36-
const response = await fetch(videoUrl, {
37-
signal: controller.signal,
38-
});
39-
40-
if (!response.ok) {
41-
throw new Error(`HTTP error! status: ${response.status}`);
42-
}
43-
44-
const contentLength = response.headers.get("content-length");
45-
const total = contentLength ? parseInt(contentLength, 10) : 0;
46-
47-
if (!response.body) {
48-
throw new Error("Response body is null");
49-
}
50-
51-
const reader = response.body.getReader();
52-
const chunks: Uint8Array[] = [];
53-
let loaded = 0;
54-
55-
while (true) {
56-
const { done, value } = await reader.read();
57-
58-
if (done) break;
59-
60-
chunks.push(value);
61-
loaded += value.length;
62-
63-
// 진행률 콜백 호출
64-
if (onProgress && total > 0) {
65-
const percentage = Math.round((loaded / total) * 100);
66-
onProgress({ loaded, total, percentage });
67-
}
68-
}
69-
70-
// Blob 생성
71-
const blob = new Blob(chunks, { type: "video/mp4" });
72-
7321
// 다운로드 링크 생성 및 클릭
74-
const url = URL.createObjectURL(blob);
7522
const link = document.createElement("a");
76-
link.href = url;
23+
link.href = videoUrl;
7724
link.download = filename;
25+
link.target = "_blank"; // 새 탭에서 열기
7826

7927
// 링크를 DOM에 임시로 추가하고 클릭
8028
document.body.appendChild(link);
8129
link.click();
8230

8331
// 정리
8432
document.body.removeChild(link);
85-
URL.revokeObjectURL(url);
8633

8734
onComplete?.();
8835
} catch (error) {
@@ -115,4 +62,4 @@ export function generateSafeFilename(
11562
/**
11663
* 다운로드 상태를 관리하는 커스텀 훅에서 사용할 수 있는 타입들
11764
*/
118-
export type { DownloadProgress, DownloadOptions };
65+
export type { DownloadOptions };

0 commit comments

Comments
 (0)