add file download
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -42,7 +43,7 @@ export async function getUserFilesData() {
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeUserFile(fileId : string) {
|
||||
export async function removeUserFile(fileId: string) {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user