diff options
Diffstat (limited to 'components')
| -rw-r--r-- | components/ship-vendor-document/user-vendor-document-table-container.tsx | 79 |
1 files changed, 50 insertions, 29 deletions
diff --git a/components/ship-vendor-document/user-vendor-document-table-container.tsx b/components/ship-vendor-document/user-vendor-document-table-container.tsx index 9e45df6b..c9118cd0 100644 --- a/components/ship-vendor-document/user-vendor-document-table-container.tsx +++ b/components/ship-vendor-document/user-vendor-document-table-container.tsx @@ -39,6 +39,7 @@ import { NewRevisionDialog } from "./new-revision-dialog" import { useRouter } from 'next/navigation' import { AddAttachmentDialog } from "./add-attachment-dialog" // ✅ import 추가 import { EditRevisionDialog } from "./edit-revision-dialog" // ✅ 추가 +import { downloadFile } from "@/lib/file-download" // ✅ 공용 다운로드 함수 import /* ------------------------------------------------------------------------------------------------- * Types & Constants @@ -764,6 +765,55 @@ function SubTables() { } }, [allData, editingRevision, setAllData, selectedRevisionId, setSelectedRevisionId, router]) + // ✅ 파일 다운로드 함수 - 공용함수 사용 + const handleDownloadFile = React.useCallback(async (attachment: AttachmentInfo) => { + try { + // 파일 경로 처리 + let downloadPath = attachment.filePath + + // 공용 다운로드 함수 사용 (보안 검증, 파일 체크 모두 포함) + const result = await downloadFile(downloadPath, attachment.fileName, { + action: 'download', + showToast: true, + showSuccessToast: true, + }) + + if (!result.success) { + throw new Error(result.error || 'Download failed') + } + + } catch (error) { + console.error('File download error:', error) + + // fallback: API 엔드포인트를 통한 다운로드 시도 + try { + const queryParam = attachment.id + ? `id=${encodeURIComponent(attachment.id)}` + : `path=${encodeURIComponent(attachment.filePath)}` + + const response = await fetch(`/api/document-download?${queryParam}`) + + if (!response.ok) { + const errorData = await response.json() + throw new Error(errorData.error || 'Failed to download file.') + } + + const blob = await response.blob() + const url = window.URL.createObjectURL(blob) + const link = window.document.createElement('a') + link.href = url + link.download = attachment.fileName + window.document.body.appendChild(link) + link.click() + window.document.body.removeChild(link) + window.URL.revokeObjectURL(url) + } catch (fallbackError) { + console.error('Fallback download also failed:', fallbackError) + alert(`File download failed: ${fallbackError instanceof Error ? fallbackError.message : 'Unknown error'}`) + } + } + }, []) + // 파일 삭제 함수 const handleDeleteFile = React.useCallback(async (attachment: AttachmentInfo) => { try { @@ -980,35 +1030,6 @@ function SubTables() { initialized.current = false }, []) - // 파일 다운로드 함수 - const handleDownloadFile = React.useCallback(async (attachment: AttachmentInfo) => { - try { - const queryParam = attachment.id - ? `id=${encodeURIComponent(attachment.id)}` - : `path=${encodeURIComponent(attachment.filePath)}` - - const response = await fetch(`/api/document-download?${queryParam}`) - - if (!response.ok) { - const errorData = await response.json() - throw new Error(errorData.error || 'Failed to download file.') - } - - const blob = await response.blob() - const url = window.URL.createObjectURL(blob) - const link = window.document.createElement('a') - link.href = url - link.download = attachment.fileName - window.document.body.appendChild(link) - link.click() - window.document.body.removeChild(link) - window.URL.revokeObjectURL(url) - } catch (error) { - console.error('File download error:', error) - alert(`File download failed: ${error instanceof Error ? error.message : 'Unknown error'}`) - } - }, []) - // WebViewer 초기화 React.useEffect(() => { if (viewerOpen && !initialized.current) { |
