-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
207 lines (167 loc) · 5.47 KB
/
server.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
// server.js
// where your node app starts
// init project
const path = require("path");
const fs = require("fs");
const filename = '/upload/data.json';
const express = require('express');
const app = express();
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/index.html');
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});
// put the HTML file containing your form in a directory named "public" (relative to where this script is located)
app.get("/", express.static(path.join(__dirname, "./public")));
const multer = require("multer");
const handleError = (err, res) => {
res
.status(500)
.contentType("text/plain")
.end("Oops! Something went wrong!");
};
const upload = multer({
dest: "./images/" //"/path/to/temporary/directory/to/store/uploaded/files"
// you might also want to set some limits: https://github.com/expressjs/multer#limits
});
app.post(
"/upload",
upload.single("file" /* name attribute of <file> element in your form */),
(req, res) => {
console.log(req.body);
console.log(req.file);
if (req['file']){
const tempPath = req.file.path;
const ext = path.extname(req.file.originalname).toLowerCase();
const targetPath = createFile(path.join(__dirname, "./images/"), req.file.filename, ext);
const obj = { groupid: req.body.groupid, textcontent: req.body.textcontent, image: targetPath, ts: Date.now() };
writeTextFile(filename , obj);
if (ext === ".png"|| ext === ".jpg" || ext === ".jpeg") {
fs.rename(tempPath, targetPath, err => {
if (err) return handleError(err, res);
res
.status(200)
.redirect('/results');
//.sendFile(targetPath);
});
} else {
fs.unlink(tempPath, err => {
if (err) return handleError(err, res);
res
.status(403)
.contentType("text/plain")
.end("Only .png files are allowed!");
});
}
}else {
const obj = { groupid: req.body.groupid, textcontent: req.body.textcontent, ts: Date.now() };
writeTextFile(filename , obj);
res
.status(200)
.redirect('/results');
}
}
);
function createFile(fpath, fname, ext){
var targetPath = fpath + fname + ext;
var i = 1;
while (fs.existsSync(targetPath)) {
targetPath = fpath + fname + i + ext;
i++;
};
return targetPath;
};
function writeTextFile(fname, obj) {
const fpath = path.join(__dirname, fname)
fs.readFile(fpath, function (err, data) {
if (data.toString() == '') {
var json = [];
} else {
var json = JSON.parse(data)
};
json.push(obj)
fs.writeFile(fpath, JSON.stringify(json), function(err, data){
if (err) console.log(err);
console.log("Successfully Written to File.");
});
})
/*var record = data;
fs.writeFile(path.join(__dirname, fname), JSON.stringify(record), function(err, data){
if (err) console.log(err);
console.log("Successfully Written to File.");
});*/
};
app.get("/results", (req, res) => {
res.sendFile(path.join(__dirname, "./views/results.html"));
});
app.get("/reset", (req, res) => {
res.sendFile(path.join(__dirname, "./views/reset.html"));
});
app.get("/images/*", (req, res) => {
console.log('load images')
console.log(req.originalUrl)
res.sendFile(path.join(__dirname, req.originalUrl));
});
function readTextFile(fname) {
fs.readFile(path.join(__dirname, fname), 'utf-8' ,function(err, buf) {
console.log(buf.toString());
return buf.toString();
});
};
app.get("/getResults", (req, res) => {
console.log('getResults')
//var output = readTextFile('/upload/text.txt');
fs.readFile(path.join(__dirname, filename), 'utf-8' ,function(err, buf) {
if (buf.length > 0) {
console.log(buf.toString());
var data = sortByKey(JSON.parse(buf), 'groupid', 'ts');
res.send({ success: true , obj: data});
} else {
res.send({ success: false});
};
});
//res.send(path.join(__dirname, "./views/results.html"));
});
app.get("/clearresults", (req, res) => {
console.log('clearresults')
console.log(req.query);
if (req.query.password == process.env.SECRET) {
fs.writeFile(path.join(__dirname, filename), '', function(err, data){
if (err) console.log(err);
console.log("Successfully Clear to File.");
});
const directory = 'images';
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(directory, file), err => {
if (err) throw err;
});
}
});
res.send("Successfully Clear to File.");
} else {
res.send("Incorrect Password.");
};
});
function sortByKey(array, key1, key2) {
return array.sort(function(a, b) {
var x1 = a[key1]; var y1 = b[key1];
var x2 = a[key2]; var y2 = b[key2];
if(x1 == y1)
{
return ((x2 < y2) ? -1 : ((x2 > y2) ? 1 : 0));
}
else
{
return ((x1 < y1) ? -1 : ((x1 > y1) ? 1 : 0));
}
});
}