This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (201 loc) · 5.44 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* eslint-disable no-console */
const { send } = require("micro");
const { applyMiddleware } = require("micro-middleware");
const { withNamespace, router, get, put, del } = require("microrouter");
const compress = require("micro-compress");
const handler = require("serve-handler");
const parser = require("form-parser");
const rrdir = require("rrdir");
const fs = require("fs-extra");
const path = require("path");
const TABLE = {
"Информатика и вычислительная техника (09.03.01)": {
headers: [
{
text: "Абитуриенты",
align: "left",
sortable: false,
value: "name"
},
{ text: "Русский язык", value: "rus" },
{ text: "Математика", value: "math" },
{ text: "Информатика", value: "inf" },
{ text: "Сумма баллов", value: "sum" }
],
abiturs: [
{
name: "Аксонов Алексей Викторович",
rus: 92,
math: 70,
inf: 62,
sum: 224
},
{
name: "Васнецов Иван Андреевич",
rus: 100,
math: 60,
inf: 72,
sum: 232
}
]
},
"Прикладные математика и физика (03.03.01)": {
headers: [
{
text: "Абитуриенты",
align: "left",
sortable: false,
value: "name"
},
{ text: "Русский язык", value: "rus" },
{ text: "Математика", value: "math" },
{ text: "Физика", value: "phys" },
{ text: "Сумма баллов", value: "sum" }
],
abiturs: [
{
name: "Махдуми Азам",
rus: 70,
math: 98,
phys: 90,
sum: 258
},
{
name: "Клинтски Радко Алексеевич",
rus: 72,
math: 86,
phys: 82,
sum: 240
}
]
}
};
async function vueServe(req, res) {
await handler(req, res, {
public: "./dist/",
directoryListing: false,
rewrites: [
{
source: "/",
destination: "/index.html"
}
],
headers: [
{
source: "*",
headers: [
{
key: "Strict-Transport-Security",
value: "max-age=31536000; includeSubDomains; preload"
},
{ key: "X-DNS-Prefetch-Control", value: "off" },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Download-Options", value: "noopen" },
{ key: "X-Frame-Options", value: "SAMEORIGIN" },
{ key: "X-Xss-Protection", value: "1; mode=block" }
]
}
]
});
}
function getSpecialities(req, res) {
send(
res,
200,
Object.keys(TABLE).sort((a, b) => {
let aNum = a.match(/(\d{2})/g).map(Number);
let bNum = b.match(/(\d{2})/g).map(Number);
for (let i = 0; i < aNum.length; i++) {
if (aNum[i] != bNum[i]) {
return aNum[i] - bNum[i];
}
}
})
);
}
function getTable(req, res) {
send(res, 200, TABLE[req.query.spec] || { headers: [], abiturs: [] });
}
async function addDocs(req, res) {
let requestFormData = [];
await parser(req, async field => {
requestFormData.push(field);
});
let filesAreInvalid = requestFormData
.filter(obj => obj.fieldType === "file" && obj.fieldName === "files")
.some(
file =>
!["image/png", "image/jpeg", "application/pdf"].includes(
file.fieldContent.fileType
)
);
if (filesAreInvalid) {
send(res, 400, "INVALID_FILES");
return;
}
let spec = requestFormData.filter(
obj => obj.fieldType === "text" && obj.fieldName === "spec"
)[0].fieldContent;
let fullname = requestFormData.filter(
obj => obj.fieldType === "text" && obj.fieldName === "name"
)[0].fieldContent;
let email = requestFormData.filter(
obj => obj.fieldType === "text" && obj.fieldName === "email"
)[0].fieldContent;
let docsPath = path.join(
path.resolve(),
"documents",
spec,
`${fullname} (${req.params.uuid})`
);
fs.ensureDir(docsPath)
.then(() => {
requestFormData
.filter(obj => obj.fieldType === "file")
.forEach(async file => {
file.fieldContent.fileStream.pipe(
fs.createWriteStream(
path.join(docsPath, file.fieldContent.fileName)
)
);
});
})
.then(() => {
fs.ensureFile(path.join(docsPath, "email.txt")).then(() => {
fs.writeFile(path.join(docsPath, "email.txt"), email);
});
});
send(res, 200, "OK");
}
async function removeDocs(req, res) {
for await (const entry of rrdir.stream(
path.join(path.resolve(), "documents")
)) {
if (entry.directory && entry.path.includes(req.params.uuid)) {
fs.emptyDir(entry.path).then(() => {
fs.remove(entry.path);
});
let spec = path.basename(path.dirname(entry.path));
let fullname = path
.basename(entry.path)
.replace(` (${req.params.uuid})`, "");
TABLE[spec].abiturs = TABLE[spec].abiturs.filter(
abitur => abitur.name !== fullname
);
break;
}
}
send(res, 200, "OK");
}
module.exports = applyMiddleware(
router(
withNamespace("/api")(
get("/getSpecialities", getSpecialities),
get("/getTable", getTable),
put("/addDocs/:uuid", addDocs),
del("/removeDocs/:uuid", removeDocs)
),
get("/*", vueServe)
),
[compress]
);