-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.js
52 lines (46 loc) · 1.27 KB
/
home.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from "express";
import fs from "fs";
import archive from "./functions/archive.js";
import { errorResponse, validate } from "./validate.js";
import writeFile from "./functions/writeToFile.js";
import scripter from "./functions/scripter.js";
const router = express.Router();
const homeRouter = () => {
router.get("/", async (req, res) => {
const arr = fs.readdirSync("./hosted", (err, files) => {
if (err) {
throw err;
}
return files;
});
console.log(arr);
res.render("index", {
files: arr,
});
});
router.get("/dl", (req, res) => {
res.download("./missionFiles.zip");
});
router.get("/zip", (req, res) => {
archive();
});
router.post("/", validate, errorResponse, async (req, res) => {
try {
const { move, name } = req.body;
if (!move || !name) {
res.send("<h1>You need to Fill the name and move fields</h1>");
} else {
const path = `./hosted/${name}.sqf`;
const script = scripter(req);
await writeFile(path, script);
res.send(
`<script>alert("File Created"); window.location.href = "/"; </script>`
);
}
} catch (err) {
res.send(err.name).redirect("/");
}
});
return router;
};
export default homeRouter;