implemente attachemtn download route

This commit is contained in:
2024-01-05 12:39:22 +01:00
parent 8b7cd45087
commit eea2ca1492
6 changed files with 82 additions and 33 deletions

View File

@@ -0,0 +1,18 @@
import Link from 'next/link';
import { FaceFrownIcon } from '@heroicons/react/24/outline';
export default function NotFound() {
return (
<main className="flex h-full flex-col items-center justify-center gap-2">
<FaceFrownIcon className="w-10 text-gray-400" />
<h2 className="text-xl font-semibold">404 File Not Found</h2>
<p>Could not find the requested attachment.</p>
<Link
href="/"
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
>
Go Back
</Link>
</main>
);
}

View File

@@ -0,0 +1,27 @@
import { fetchBillById } from '@/app/lib/fetchBillById';
import { notFound } from 'next/navigation';
export async function GET(request: Request, { params:{ id } }: { params: { id:string } }) {
const [invoiceID, billID] = id.split('-');
const bill = await fetchBillById(invoiceID, billID);
if(!bill?.attachment) {
notFound();
}
// convert fileContentsBase64 from Base64 string to binary string
const fileContentsBuffer = Buffer.from(bill.attachment.fileContentsBase64, 'base64');
// convert fileContentsBuffer to format that can be sent to the client
const fileContents = new Uint8Array(fileContentsBuffer);
return new Response(fileContents, {
status: 200,
headers: {
'Content-Type': "application/octet-stream",
'Content-Disposition': `attachment; filename="${bill.attachment.fileName}"`,
'Last-Modified': `${bill.attachment.fileLastModified}`
}
});
}