update download function
This commit is contained in:
@@ -117,4 +117,27 @@ export async function viewFileInfo(fileId: string) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
return 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -27,6 +27,10 @@ import { fetchReferralUserStats } from '@/app/actions/referralsActions';
|
|||||||
import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown';
|
import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown';
|
||||||
import { formatDate, formatDateTime } from '@/app/lib/formatDate';
|
import { formatDate, formatDateTime } from '@/app/lib/formatDate';
|
||||||
import { useViewport } from '@/app/hooks/useViewport';
|
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';
|
type FileType = 'image' | 'video' | 'audio';
|
||||||
|
|
||||||
@@ -482,31 +486,38 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleDownload = async (file: FileItem) => {
|
const handleDownload = async (file: FileItem) => {
|
||||||
setIsFileLoading(true);
|
setIsFileLoading(true)
|
||||||
|
|
||||||
try {
|
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) {
|
const byteCharacters = atob(result.data);
|
||||||
throw new Error(`error: ${response.status}`);
|
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 url = window.URL.createObjectURL(blob);
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
a.download = file._original?.fileName || file.fileName || `file-${file.id}`;
|
a.download = result.fileName;
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
window.URL.revokeObjectURL(url);
|
window.URL.revokeObjectURL(url);
|
||||||
document.body.removeChild(a);
|
document.body.removeChild(a);
|
||||||
toast.success(`${t('file-is-downloading')} - ${file.fileName}`);
|
toast.success(`${t('file-is-downloading')} - ${file.fileName}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error(t('failed-to-download-file'))
|
toast.error(t('failed-to-download-file'));
|
||||||
} finally {
|
} finally {
|
||||||
setIsFileLoading(false);
|
setIsFileLoading(false);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const handleMonitoring = async (file: FileItem) => {
|
const handleMonitoring = async (file: FileItem) => {
|
||||||
console.log(`Мониторинг:`, file);
|
console.log(`Мониторинг:`, file);
|
||||||
|
|||||||
Reference in New Issue
Block a user