Skip to content

Commit 82169bc

Browse files
authored
refactor: presigned URL 기반 파일 다운로드 유틸로 추출 (#356)
[refactor] presigned URL 기반 파일 다운로드 유틸로 추출 (#356)
2 parents a9d377a + e50f73a commit 82169bc

2 files changed

Lines changed: 30 additions & 38 deletions

File tree

src/features/project/post/components/PostDetailDrawer.jsx

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as postAPI from "@/api/post";
2222
import FileAttachmentViewer from "./FileAttachmentViewer";
2323
import FilePreviewModal from "./FilePreviewModal";
2424
import PostDetailTopSection from "./PostDetailTopSection";
25+
import { downloadAttachment } from "@/utils/downloadUtils";
2526

2627
export default function PostDetailDrawer({
2728
open,
@@ -78,44 +79,8 @@ export default function PostDetailDrawer({
7879
};
7980

8081
// 파일 다운로드
81-
const handleDownload = async (attachment) => {
82-
try {
83-
// 다운로드 URL 발급 API 호출
84-
const response = await postAPI.getAttachmentDownloadUrl(attachment.id);
85-
86-
if (response.data.result === "SUCCESS") {
87-
const downloadUrl = response.data.data.downloadUrl;
88-
89-
// S3에서 파일 다운로드 후 브라우저에서 다운로드
90-
const fileResponse = await fetch(downloadUrl);
91-
if (!fileResponse.ok) {
92-
throw new Error(`파일 다운로드 실패: ${fileResponse.status}`);
93-
}
94-
95-
// Blob으로 변환
96-
const blob = await fileResponse.blob();
97-
98-
// Blob URL 생성하여 다운로드
99-
const blobUrl = URL.createObjectURL(blob);
100-
const link = document.createElement("a");
101-
link.href = blobUrl;
102-
link.download = attachment.fileName; // 파일명 지정
103-
link.style.display = "none";
104-
105-
// DOM에 추가 후 클릭하여 다운로드 실행
106-
document.body.appendChild(link);
107-
link.click();
108-
109-
// 정리
110-
document.body.removeChild(link);
111-
URL.revokeObjectURL(blobUrl); // 메모리 정리
112-
} else {
113-
throw new Error("다운로드 URL 발급 실패");
114-
}
115-
} catch (error) {
116-
console.error("파일 다운로드 실패:", error);
117-
alert("파일 다운로드에 실패했습니다.");
118-
}
82+
const handleDownload = (attachment) => {
83+
downloadAttachment(attachment, postAPI.getAttachmentDownloadUrl);
11984
};
12085

12186
useEffect(() => {

src/utils/downloadUtils.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export async function downloadAttachment(attachment, getPresignedUrlFn) {
2+
try {
3+
const response = await getPresignedUrlFn(attachment.id);
4+
5+
if (response.data.result === "SUCCESS") {
6+
const downloadUrl = response.data.data.downloadUrl;
7+
8+
const fileResponse = await fetch(downloadUrl);
9+
if (!fileResponse.ok) throw new Error("파일 다운로드 실패");
10+
11+
const blob = await fileResponse.blob();
12+
const blobUrl = URL.createObjectURL(blob);
13+
const link = document.createElement("a");
14+
link.href = blobUrl;
15+
link.download = attachment.fileName;
16+
document.body.appendChild(link);
17+
link.click();
18+
document.body.removeChild(link);
19+
URL.revokeObjectURL(blobUrl);
20+
} else {
21+
throw new Error("다운로드 URL 발급 실패");
22+
}
23+
} catch (error) {
24+
console.error(error);
25+
alert("파일 다운로드에 실패했습니다.");
26+
}
27+
}

0 commit comments

Comments
 (0)