sunnymh/app/api/pages/route.ts
yiekheng 4f5d74e1c8 Squashed 'manga-site/' content from commit f2ef775
git-subtree-dir: manga-site
git-subtree-split: f2ef775f7095dc2b107b576cd4053593e89dd887
2026-04-12 18:47:51 +08:00

31 lines
897 B
TypeScript

import { prisma } from "@/lib/db";
import { signUrl } from "@/lib/r2";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const chapterId = parseInt(searchParams.get("chapterId") ?? "", 10);
const offset = Math.max(parseInt(searchParams.get("offset") ?? "0", 10), 0);
const limit = Math.min(Math.max(parseInt(searchParams.get("limit") ?? "7", 10), 1), 20);
if (isNaN(chapterId)) {
return Response.json({ error: "Missing chapterId" }, { status: 400 });
}
const pages = await prisma.page.findMany({
where: { chapterId },
orderBy: { number: "asc" },
skip: offset,
take: limit,
select: { number: true, imageUrl: true },
});
const signedPages = await Promise.all(
pages.map(async (p) => ({
number: p.number,
imageUrl: await signUrl(p.imageUrl),
}))
);
return Response.json(signedPages);
}