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
import { saveFile } from "https://esm.town/v/omgwtfbrblolttyl/saveFile";
import { tokenAllows } from "https://esm.town/v/omgwtfbrblolttyl/tokenAllows";
export const writeFileEndpoint = async (
req: express.Request,
res: express.Response,
) => {
if (req.method !== "POST") {
return res.status(404).json({ msg: "Not found" });
}
const body = req.body;
console.log(body.path, body.data);
if (!body.path || !body.file) {
return res.status(400).json({
msg: "Body must contain path and data fields",
});
}
const token = req.get("token");
if (!token) {
return res.status(401).json({ msg: "No token provided" });
}
const { allowed, error } = await tokenAllows(
token,
body.path,
"write",
);
if (!allowed) {
return res.status(error.code)
.json({ msg: error.msg });
}
const saveRes = await saveFile(body.path, body.file);
return res.status(201).json({ msg: "created" });
};