1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { readFile } from "https://esm.town/v/omgwtfbrblolttyl/readFile";
import { tokenAllows } from "https://esm.town/v/omgwtfbrblolttyl/tokenAllows";
export const readFileEndpoint = async (
req: express.Request,
res: express.Response,
) => {
if (req.method !== "GET") {
console.log("wrong method", req.method);
return res.status(404).json({ msg: "Not found" });
}
const path = req.query["path"];
if (!path || typeof path !== "string") {
return res.status(400).json({
msg: "Missing or invalid 'path' query parameter",
query: req.query,
});
}
const token = req.get("token");
if (!token) {
return res.status(401).json({ msg: "No token provided" });
}
const { allowed, error } = await tokenAllows(
token,
path,
"read",
);
if (!allowed) {
return res.status(error.code)
.json({ msg: error.msg });
}
const readRes = await readFile(path);
if (readRes === null) {
return res.status(404).json({ msg: "Not found" });
}
return res.status(200).json(readRes);
};