add file download

This commit is contained in:
smanylov
2026-01-02 12:52:01 +07:00
parent a085685d33
commit 3537bd1f3a
3 changed files with 81 additions and 3 deletions
+1
View File
@@ -1,5 +1,6 @@
'use server'
import { NextRequest, NextResponse } from 'next/server';
import { getSessionData } from '@/app/actions/session';
import { API_BASE_URL } from '@/app/actions/definitions';
+51
View File
@@ -0,0 +1,51 @@
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 }
);
}
}
+28 -2
View File
@@ -128,6 +128,8 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
const [openWindow, setOpenWindow] = useState<boolean>(false);
const [openWindowChildren, setOpenWindowChildren] = useState<ReactNode>(null);
const [isFileLoading, setIsFileLoading] = useState(false);
const t = useTranslations("Global");
// Определение колонок
@@ -343,6 +345,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
</button>
<button
onClick={() => handleDownload(row.original)}
disabled={isFileLoading}
className="px-3 py-1 bg-green-500 text-white rounded hover:bg-green-600 text-sm"
>
<IconFileOpen />
@@ -373,8 +376,31 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
console.log(`Просмотр файла: ${file.fileName}`);
};
const handleDownload = (file: FileItem) => {
console.log(`Скачать: ${file.fileName}`);
const handleDownload = async (file: FileItem) => {
setIsFileLoading(true);
console.log(`download: ${file.id}`);
try {
const response = await fetch(`/api/download/${file.id}`);
if (!response.ok) {
throw new Error(`error: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = file.fileName || `file-${file.id}`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (error) {
toast.error('Не удалось скачать файл')
} finally {
setIsFileLoading(false);
}
};
const handleProtect = (file: FileItem) => {