-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
105 lines (91 loc) · 2.37 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const express = require("express");
const { spawn } = require("child_process");
const multer = require("multer");
const path = require("path");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
const PORT = 8000;
const options = {
root: path.join(__dirname),
};
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
if (req.files?.input) {
cb(null, path.join(__dirname, "Input"));
} else if (req.files?.images) {
cb(null, path.join(__dirname, "BubbleSheet/dataset/Input"));
}
},
filename: (req, file, cb) => {
cb(null, file.originalname);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use((_req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET,POST,PUT,DELETE,PATCH,OPTIONS"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
next();
});
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).fields([
{ name: "input", maxCount: 1 },
{ name: "images", maxCount: 100 },
])
);
app.post("/", (req, res) => {
const codesChoice = req.body.codesChoice;
const digitsChoice = req.body.digitsChoice;
const python = spawn("python", [
"excel.py",
codesChoice.toString(),
digitsChoice.toString(),
]);
python.on("close", (code) => {
res.setHeader("Content-Type", "application/vnd.ms-excel");
res.sendFile("autoFiller.xls", options, function (err) {
if (err) {
console.log(err.message);
} else {
console.log("Sent");
}
});
});
});
app.post("/bubble", (req, res) => {
const python = spawn("python", ["BubbleSheet/bubbleScript.py"]);
python.on("close", (code) => {
res.setHeader(
"Content-Type",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
res.sendFile(
path.join("BubbleSheet", "answers.xlsx"),
options,
function (err) {
if (err) {
console.log(err.message);
} else {
console.log("Sent");
}
}
);
});
});
app.listen(PORT, () => {
console.log(`Started on port ${PORT}`);
});