summaryrefslogtreecommitdiff
path: root/components/ship-vendor-document-all/user-vendor-document-table-container.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'components/ship-vendor-document-all/user-vendor-document-table-container.tsx')
-rw-r--r--components/ship-vendor-document-all/user-vendor-document-table-container.tsx57
1 files changed, 39 insertions, 18 deletions
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'}`)
+ }
}
}, [])