2023-03-01 19:54:22 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
import { getDocument } from "@documenso/lib/query";
|
2023-04-04 22:02:32 +00:00
|
|
|
import { defaultHandler, defaultResponder, getUserFromToken } from "@documenso/lib/server";
|
|
|
|
|
import prisma from "@documenso/prisma";
|
2023-03-02 17:54:19 +01:00
|
|
|
import { addDigitalSignature } from "@documenso/signing/addDigitalSignature";
|
2023-04-04 22:02:32 +00:00
|
|
|
import { Document as PrismaDocument } from "@prisma/client";
|
2023-03-01 19:54:22 +01:00
|
|
|
|
2023-03-02 18:44:54 +01:00
|
|
|
// todo remove before launch
|
|
|
|
|
|
2023-03-01 19:54:22 +01:00
|
|
|
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const documentId = req.query.id || 1;
|
|
|
|
|
const document: PrismaDocument = await getDocument(+documentId, req, res);
|
2023-03-02 18:42:38 +01:00
|
|
|
const signedDocument = await addDigitalSignature(document.document);
|
2023-03-01 19:54:22 +01:00
|
|
|
res.setHeader("Content-Type", "application/pdf");
|
2023-03-02 18:42:38 +01:00
|
|
|
res.setHeader("Content-Length", signedDocument.length);
|
2023-04-04 22:02:32 +00:00
|
|
|
res.setHeader("Content-Disposition", `attachment; filename=${document.title}`);
|
2023-03-01 19:54:22 +01:00
|
|
|
|
2023-03-02 18:42:38 +01:00
|
|
|
return res.status(200).send(signedDocument);
|
2023-03-01 19:54:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
|
GET: Promise.resolve({ default: defaultResponder(getHandler) }),
|
|
|
|
|
});
|