Files
dataroom/pages/api/jobs/get-thumbnail.ts
Marc SeitzandHanzo Dev d0a74fd593 fix: pass documentId through chart data and add rate limiting to thumbnail API
The tooltip was using router.query.id (dataroom ID) instead of the
actual document ID, and versionNumber was NaN. Pass documentId through
BarChartComponent data so the tooltip resolves the correct thumbnail.
Add rate limiting (150 req/min per user) to the get-thumbnail endpoint.

Co-authored-by: Hanzo Dev <[email protected]>
2026-02-25 17:14:36 +11:00

51 lines
1.3 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { getFileForDocumentPage } from "@/lib/documents/get-file-helper";
import { ratelimit } from "@/lib/redis";
import { CustomUser } from "@/lib/types";
import { authOptions } from "../auth/[...nextauth]";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== "GET") {
res.status(405).json({ message: "Method Not Allowed" });
return;
}
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}
const { success } = await ratelimit(150, "1 m").limit(
`get-thumbnail:${(session.user as CustomUser).id}`,
);
if (!success) {
return res.status(429).json({ message: "Too many requests" });
}
const { documentId, pageNumber, versionNumber } = req.query as {
documentId: string;
pageNumber: string;
versionNumber: string;
};
try {
const imageUrl = await getFileForDocumentPage(
Number(pageNumber),
documentId,
versionNumber === "undefined" ? undefined : Number(versionNumber),
);
return res.status(200).json({ imageUrl });
} catch (error) {
res.status(500).json({ message: (error as Error).message });
return;
}
}