From 92ad8a740bfefa8c7465c23e41208a7e36caa59d Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Thu, 28 Aug 2025 00:36:01 +0000
Subject: (대표님) dolce 문서리스트 파일 다운로드 함수 문제 처리, 파일다운로드
 use client 추가
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../user-vendor-document-table-container.tsx       | 79 ++++++++++++++--------
 1 file changed, 50 insertions(+), 29 deletions(-)

(limited to 'components/ship-vendor-document/user-vendor-document-table-container.tsx')

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) {
-- 
cgit v1.2.3

