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" });
};