2023-03-08 15:31:24 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-04-26 15:40:48 -05:00
|
|
|
import getPermission from "@/lib/api/getPermission";
|
2023-07-01 09:11:39 -05:00
|
|
|
import readFile from "@/lib/api/storage/readFile";
|
2023-11-02 13:59:31 -05:00
|
|
|
import authenticateUser from "@/lib/api/authenticateUser";
|
2023-03-08 15:31:24 -06:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
2023-03-08 15:31:24 -06:00
|
|
|
if (!req.query.params)
|
|
|
|
return res.status(401).json({ response: "Invalid parameters." });
|
|
|
|
|
2023-11-02 13:59:31 -05:00
|
|
|
const user = await authenticateUser({ req, res });
|
|
|
|
if (!user) return res.status(404).json({ response: "User not found." });
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const collectionId = req.query.params[0];
|
2023-06-13 09:30:52 -05:00
|
|
|
const linkId = req.query.params[1];
|
2023-03-08 15:31:24 -06:00
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
const collectionIsAccessible = await getPermission({
|
2023-11-02 13:59:31 -05:00
|
|
|
userId: user.id,
|
2023-10-22 23:28:39 -05:00
|
|
|
collectionId: Number(collectionId),
|
|
|
|
});
|
2023-03-08 15:31:24 -06:00
|
|
|
|
|
|
|
if (!collectionIsAccessible)
|
|
|
|
return res
|
|
|
|
.status(401)
|
|
|
|
.json({ response: "You don't have access to this collection." });
|
|
|
|
|
2023-08-10 11:16:44 -05:00
|
|
|
const { file, contentType, status } = await readFile(
|
|
|
|
`archives/${collectionId}/${linkId}`
|
|
|
|
);
|
|
|
|
res.setHeader("Content-Type", contentType).status(status as number);
|
2023-03-23 10:25:17 -05:00
|
|
|
|
|
|
|
return res.send(file);
|
2023-03-08 15:31:24 -06:00
|
|
|
}
|