From 18954df6565108a469fb1608ea3715dd9bb1b02d Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Mon, 1 Sep 2025 09:12:09 +0000
Subject: (대표님) 구매 기본계약, gtc 개발
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../user-vendor-document-table-container.tsx       | 57 +++++++++++++++-------
 1 file changed, 39 insertions(+), 18 deletions(-)

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

diff --git a/components/ship-vendor-document-all/user-vendor-document-table-container.tsx b/components/ship-vendor-document-all/user-vendor-document-table-container.tsx
index 157bdb03..3dcd12e0 100644
--- a/components/ship-vendor-document-all/user-vendor-document-table-container.tsx
+++ b/components/ship-vendor-document-all/user-vendor-document-table-container.tsx
@@ -36,6 +36,7 @@ import {
 import { SimplifiedDocumentsView } from "@/db/schema"
 import { WebViewerInstance } from "@pdftron/webviewer"
 import { useRouter } from 'next/navigation'
+import { downloadFile } from "@/lib/file-download"
 
 /* -------------------------------------------------------------------------------------------------
  * Types & Constants
@@ -589,29 +590,49 @@ function SubTables() {
 
   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}`)
+      // 파일 경로 처리
+      let downloadPath = attachment.filePath
+      
+      // 공용 다운로드 함수 사용 (보안 검증, 파일 체크 모두 포함)
+      const result = await downloadFile(downloadPath, attachment.fileName, {
+        action: 'download',
+        showToast: true,
+        showSuccessToast: true,
+      })
 
-      if (!response.ok) {
-        const errorData = await response.json()
-        throw new Error(errorData.error || 'Failed to download file.')
+      if (!result.success) {
+        throw new Error(result.error || 'Download failed')
       }
 
-      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'}`)
+      
+      // 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'}`)
+      }
     }
   }, [])
 
-- 
cgit v1.2.3

