diff --git a/src/app/actions/fileEntity.ts b/src/app/actions/fileEntity.ts index 2dfc5f5..8e26813 100644 --- a/src/app/actions/fileEntity.ts +++ b/src/app/actions/fileEntity.ts @@ -117,4 +117,27 @@ export async function viewFileInfo(fileId: string) { } catch (error) { return error } +} + +export async function downloadFile(fileId: string, fileName: string) { + const token = await getSessionData('token') + + const response = await fetch(`${API_BASE_URL}/api/v1/files/download/${fileId}`, { + headers: { + 'Authorization': `Bearer ${token}`, + }, + }) + + if (!response.ok) { + throw 'failed-to-download-file'; + } + + const arrayBuffer = await response.arrayBuffer() + const base64 = Buffer.from(arrayBuffer).toString('base64') + + return { + data: base64, + contentType: response.headers.get('content-type') || 'application/octet-stream', + fileName: fileName + } } \ No newline at end of file diff --git a/src/app/api/download/[id]/route.ts b/src/app/api/download/[id]/route.ts deleted file mode 100644 index f302580..0000000 --- a/src/app/api/download/[id]/route.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { NextRequest, NextResponse } from 'next/server'; -import { getSessionData } from '@/app/actions/session'; -import { API_BASE_URL } from '@/app/actions/definitions'; - -export async function GET( - request: NextRequest, - { params }: { params: Promise<{ id: string }> } -) { - try { - const token = await getSessionData('token'); - - if (!token) { - return NextResponse.json( - { error: 'need authorization' }, - { status: 401 } - ); - } - - const { id } = await params; - - const response = await fetch(`${API_BASE_URL}/api/v1/files/download/${id}`, { - method: 'GET', - headers: { - "Authorization": token - } - }); - - if (!response.ok) { - throw new Error(`backend error: ${response.status}`); - } - - const fileBlob = await response.blob(); - const headers = new Headers(response.headers); - - return new NextResponse(fileBlob, { - status: 200, - headers: { - 'Content-Type': headers.get('content-type') || 'application/octet-stream', - 'Content-Disposition': headers.get('content-disposition') || `attachment; filename="file-${id}"`, - 'Content-Length': headers.get('content-length') || String(fileBlob.size), - }, - }); - - } catch (error) { - console.error('download error:', error); - return NextResponse.json( - { error: 'download file error' }, - { status: 500 } - ); - } -} \ No newline at end of file diff --git a/src/app/components/tanstak-table/TanstakTable.tsx b/src/app/components/tanstak-table/TanstakTable.tsx index 11f546f..d4c0674 100644 --- a/src/app/components/tanstak-table/TanstakTable.tsx +++ b/src/app/components/tanstak-table/TanstakTable.tsx @@ -27,6 +27,10 @@ import { fetchReferralUserStats } from '@/app/actions/referralsActions'; import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown'; import { formatDate, formatDateTime } from '@/app/lib/formatDate'; import { useViewport } from '@/app/hooks/useViewport'; +import { API_BASE_URL } from '@/app/actions/definitions'; +import { getSessionData } from '@/app/actions/session'; + +import { downloadFile } from '@/app/actions/fileEntity' type FileType = 'image' | 'video' | 'audio'; @@ -482,31 +486,38 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) { }; const handleDownload = async (file: FileItem) => { - setIsFileLoading(true); + setIsFileLoading(true) try { - const response = await fetch(`/api/download/${file.id}`); + const result = await downloadFile( + file.id, + file._original?.fileName || file.fileName || `file-${file.id}` + ); - if (!response.ok) { - throw new Error(`error: ${response.status}`); + const byteCharacters = atob(result.data); + const byteNumbers = new Array(byteCharacters.length); + for (let i = 0; i < byteCharacters.length; i++) { + byteNumbers[i] = byteCharacters.charCodeAt(i) } - const blob = await response.blob(); + const byteArray = new Uint8Array(byteNumbers); + const blob = new Blob([byteArray], { type: result.contentType }); + const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; - a.download = file._original?.fileName || file.fileName || `file-${file.id}`; + a.download = result.fileName; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); toast.success(`${t('file-is-downloading')} - ${file.fileName}`); } catch (error) { - toast.error(t('failed-to-download-file')) + toast.error(t('failed-to-download-file')); } finally { setIsFileLoading(false); } - }; + } const handleMonitoring = async (file: FileItem) => { console.log(`Мониторинг:`, file);