-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
191 lines (161 loc) · 4.51 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
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
/*global -Promise */
/*jshint -W024 */
var express = require("express");
var bodyParser = require("body-parser");
var multer = require("multer");
var fs = require("fs");
var path = require("path");
var app = express();
var moment = require("moment");
var Promise = require("bluebird");
Promise.promisifyAll(fs);
var data = {};
moment.locale("de");
//static root
app.use(express.static("uploads"));
// for parsing application/json
app.use(bodyParser.json());
// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// multipart upload
app.use(multipart());
app.post("/upload", function (req, res) {
save(data).then(function () {
res.send(data);
}).catch(function () {
res.status(500).send("error saving data.json");
});
});
// index file
app.get("/", function (req, res) {
res.sendFile(__dirname + "/form.html");
});
// get uploaded files
app.get("/uploads/", function (req, res) {
res.send(Object.keys(data).sort(sortByTimestamp).map(function (item) {
var now = moment();
var uploaded = moment(Number(data[item].timestamp));
data[item].time = uploaded.from(now);
return data[item];
}));
});
// delete uploaded files
app.delete("/uploads/", function (req, res) {
data = {};
save(res, data);
});
app.get("/vin/:vin", function (req, res) {
res.send(Object.keys(data)
.map(function (item) {
return data[item];
})
.filter(function (item) {
return item.vin === req.params.vin;
}
));
});
app.delete("/vin/:vin", function (req, res) {
var key = Object.keys(data)
.map(function (key) {
return data[key];
})
.filter(function (item) {
return item.vin === req.params.vin;
})
.reduce(function (memo, item) {
return item.key;
}, "");
delete data[key];
save(data).then(function () {
res.send(data);
}).catch(function () {
res.status(500).send("error saving data.json");
});
});
app.get("/key/:key", function (req, res) {
res.send(Object.keys(data)
.map(function (item) {
return data[item];
})
.filter(function (item) {
return item.key === req.params.key;
}
));
});
app.delete("/key/:key", function (req, res) {
delete data[req.params.key];
save(data).then(function () {
res.send(data);
}).catch(function () {
res.status(500).send("error saving data.json");
});
});
start();
function multipart() {
return multer({
dest: "./uploads",
rename: function (fieldname, filename) {
return filename.replace(/\W+/g, "-").toLowerCase() + Date.now();
},
changeDest: function(dest, req) {
var changedDest = path.join(dest, req.body.key || "");
if (!fs.existsSync(changedDest)) {
fs.mkdirSync(changedDest);
}
return changedDest;
},
onFileUploadComplete: function (file, req) {
if (!req.body.key) {
return;
}
if (!data[req.body.key]) {
data[req.body.key] = {
vin: req.body.vin,
key: req.body.key,
timestamp: req.body.timestamp,
images: []
};
}
data[req.body.key].images.push(path.basename(file.path));
}
});
}
function start() {
fs.readFile("data.json", function (err, filedata) {
if (err) {
console.log("error reading data.json", err);
}
try {
data = JSON.parse(filedata);
} catch (e) {
fs.writeFileSync("data.json", "{}");
data = {};
}
startServer();
});
}
function startServer() {
var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("App listening at http://%s:%s", host, port);
});
}
function sortByTimestamp(a, b) {
if (data[a].time < data[b].time) {
return -1;
}
if (data[a].time > data[b].time) {
return 1;
}
// a must be equal to b
return 0;
}
function save(obj) {
return fs.writeFileAsync("data.json", JSON.stringify(obj || {}));
}