add file download
This commit is contained in:
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user