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
+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 }
);
}
}