el.xwx.moe/pages/api/archives/[...params].ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]";
import getPermission from "@/lib/api/getPermission";
import readFile from "@/lib/api/storage/readFile";
export default async function Index(req: NextApiRequest, res: NextApiResponse) {
if (!req.query.params)
return res.status(401).json({ response: "Invalid parameters." });
const collectionId = req.query.params[0];
2023-06-13 09:30:52 -05:00
const linkId = req.query.params[1];
const session = await getServerSession(req, res, authOptions);
2023-07-08 05:35:43 -05:00
if (!session?.user?.username)
return res.status(401).json({ response: "You must be logged in." });
const collectionIsAccessible = await getPermission(
session.user.id,
Number(collectionId)
);
if (!collectionIsAccessible)
return res
.status(401)
.json({ response: "You don't have access to this collection." });
const { file, contentType } = await readFile({
filePath: `archives/${collectionId}/${linkId}`,
});
res.setHeader("Content-Type", contentType).status(200);
2023-03-23 10:25:17 -05:00
return res.send(file);
}