-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsources.js
345 lines (333 loc) · 11.6 KB
/
sources.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"use strict";
var express = require("express");
var router = express.Router();
var SourceSerializer = require("../serializers/source");
var ObjectSerializer = require("../serializers/object");
var ErrorSerializer = require("../serializers/error");
var sources;
/**
* @api {get} /sources/:source_id Get Sources
* @apiName Get Sources
* @apiGroup 6. Source and Over The Air OTA
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [rule_id] Rule Id
* @apiParam {String} [name] Rule name
*
* @apiUse 200
* @apiUse 401
* @apiUse 405
* @apiUse 429
*/
router.get("/?(:source_id([0-9a-z\-]+))?/?(:version([0-9]+))?", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var source_id = req.params.source_id;
var version = req.params.version;
var name = req.query.name;
var size = typeof req.query.size!=="undefined"?req.query.size:20;
var page = typeof req.query.page!=="undefined"?req.query.page:1;
page = page>0?page:1;
var offset = Math.ceil(size*(page-1));
sources = dbSources.getCollection("sources");
var query;
if ( typeof source_id !== "undefined" ) {
if ( typeof version !== "undefined" ) {
if (Number.isInteger(parseInt(version, 10))===true) { // so far it must be an integer as defined in the route. TODO: could be "latest"
query = {
"$and": [
{ "user_id" : req.user.id },
{ "root_source_id" : source_id }, // in case of version 0, root_source_id should be the same as id
{ "version" : parseInt(version, 10) },
]
};
}
} else {
query = {
"$and": [
{ "user_id" : req.user.id },
{ "id" : source_id },
]
};
}
} else {
if ( typeof name !== "undefined" ) {
query = {
"$and": [
{ "user_id" : req.user.id },
{ "name": { "$regex": [name, "i"] } }
]
};
} else {
query = {
"$and": [
{ "user_id" : req.user.id },
{ "version" : 0 },
]
};
}
}
var json = sources.chain().find(query).offset(offset).limit(size).data();
if ( json.length > 0 ) {
// TODO: If version==="latest", then json should be an array and we should filter only the correct version
/*if (version==="latest") {
json.filter(source => source.version === 4);
var total = sources.find(query).length;
json.size = size;
json.pageSelf = page;
json.pageFirst = 1;
json.pagePrev = json.pageSelf>json.pageFirst?Math.ceil(json.pageSelf)-1:json.pageFirst;
json.pageLast = Math.ceil(total/size);
json.pageNext = json.pageSelf<json.pageLast?Math.ceil(json.pageSelf)+1:undefined;
res.status(200).send(new SourceSerializer(json).serialize());
} else {
*/
json.map(function(s) {
s.subversions = new Array();
let q = {"$and": [{"root_source_id": s.root_source_id}, {"version": { "$ne" : 0 }}]};
(sources.chain().find(q).simplesort("version", false).data()).map(function(subsource) {
s.subversions.push({id: subsource.id, name: subsource.name, version: subsource.version});
});
});
var total = sources.find(query).length;
json.size = size;
json.pageSelf = page;
json.pageFirst = 1;
json.pagePrev = json.pageSelf>json.pageFirst?Math.ceil(json.pageSelf)-1:json.pageFirst;
json.pageLast = Math.ceil(total/size);
json.pageNext = json.pageSelf<json.pageLast?Math.ceil(json.pageSelf)+1:undefined;
res.status(200).send(new SourceSerializer(json).serialize());
/*}*/
} else {
res.status(404).send(new ErrorSerializer({"id": 14271, "code": 404, "message": "Not Found"}).serialize());
}
});
/**
* @api {get} /sources/:source_id/child Get Children of Source
* @apiName Get Children of Source
* @apiGroup 6. Source and Over The Air OTA
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [rule_id] Parent Rule Id
* @apiParam {String} [name] Rule name
*
* @apiUse 200
* @apiUse 401
* @apiUse 405
* @apiUse 429
*/
router.get("/:source_id([0-9a-z\-]+)/child", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var source_id = req.params.source_id;
var name = req.query.name;
var size = typeof req.query.size!=="undefined"?req.query.size:20;
var page = typeof req.query.page!=="undefined"?req.query.page:1;
page = page>0?page:1;
var offset = Math.ceil(size*(page-1));
sources = dbSources.getCollection("sources");
var query = {
"$and": [
{ "user_id" : req.user.id },
{ "parent_source_id" : source_id },
]
};
var json = sources.chain().find(query).offset(offset).limit(size).data();
t6console.debug(query);
var total = sources.find(query).length;
json.size = size;
json.pageSelf = page;
json.pageFirst = 1;
json.pagePrev = json.pageSelf>json.pageFirst?Math.ceil(json.pageSelf)-1:json.pageFirst;
json.pageLast = Math.ceil(total/size);
json.pageNext = json.pageSelf<json.pageLast?Math.ceil(json.pageSelf)+1:undefined;
res.status(200).send(new SourceSerializer(json).serialize());
});
/**
* @api {post} /sources Create new Source
* @apiName Create new Source
* @apiGroup 6. Source and Over The Air OTA
* @apiVersion 2.0.1
*
* @apiUse Auth
*
* @apiBody {String} content Content of the arduino source
* @apiBody {String} name Name of the file
* @apiBody {String} [password] Password for OTA
*
* @apiUse 201
* @apiUse 400
* @apiUse 429
*/
router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
sources = dbSources.getCollection("sources");
if ( typeof req.user.id !== "undefined" ) {
var source_id = uuid.v4();
let content;
if ( Array.isArray(req.body.content) ) {
content = req.body.content.join("\n");
} else {
content = req.body.content;
}
var newSource = {
id: source_id,
root_source_id: source_id,
version: parseInt(0, 10),
latest_version: parseInt(0, 10),
user_id: req.user.id,
name: typeof req.body.name!=="undefined"?req.body.name:"",
content: content,
password: typeof req.body.password!=="undefined"?req.body.password:"",
};
t6events.addStat("t6Api", "source add", newSource.id, req.user.id);
t6events.addAudit("t6Api", "source add", req.user.id, newSource.id, {error_id: null, status: 201});
sources.insert(newSource);
//t6console.log(sources);
res.header("Location", "/v"+version+"/sources/"+newSource.id);
res.status(201).send({ "code": 201, message: "Created", source: new SourceSerializer(newSource).serialize() });
}
});
/**
* @api {put} /sources/:source_id Edit a Source
* @apiName Edit a Source
* @apiGroup 6. Source and Over The Air OTA
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [source_id] Source Id
*
* @apiBody {String} content Content of the arduino source
* @apiBody {String} name Name of the file
* @apiBody {String} [password] Password for OTA
*
* @apiUse 200
* @apiUse 400
* @apiUse 401
* @apiUse 403
* @apiUse 404
* @apiUse 405
* @apiUse 429
*/
router.put("/:source_id([0-9a-z\-]+)", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var parent_source_id = req.params.source_id;
sources = dbSources.getCollection("sources");
var query = {
"$and": [
{ "id": parent_source_id },
{ "user_id": req.user.id },
]
};
let parent = sources.findOne( query );
var queryRoot = {
"$and": [
{ "id": parent.root_source_id },
{ "user_id": req.user.id },
]
};
let root = sources.findOne( queryRoot );
if (!req.query.overwrite || req.query.overwrite === "false") {
// By default, this will create a new version instead of overwritting the current id
if (root && parent && parent_source_id) {
var source_id = uuid.v4();
let content;
if ( Array.isArray(req.body.content) ) {
content = req.body.content.join("\n");
} else {
content = req.body.content;
}
var newSource = {
id: source_id,
parent_source_id: parent_source_id,
root_source_id: root.id,
user_id: req.user.id,
name: typeof req.body.name!=="undefined"?req.body.name:parent.name,
content: content,
version: parseInt(root.latest_version+1, 10),
password: typeof req.body.password!=="undefined"?req.body.password:parent.password,
};
var result;
sources.chain().find({ "id": root.id }).update(function(r) {
r.latest_version = parseInt(r.latest_version+1, 10);
r.latest_version_id = source_id;
result = r;
});
if (typeof result!=="undefined") {
dbSources.save();
sources.insert(newSource);
t6events.addStat("t6Api", "source add child", newSource.id, req.user.id);
res.header("Location", "/v"+version+"/sources/"+source_id);
res.status(200).send({ "code": 200, message: "Successfully updated", source: new SourceSerializer(newSource).serialize() });
} else {
res.status(404).send(new ErrorSerializer({"id": 14472, "code": 404, "message": "Not Found"}).serialize());
}
} else {
res.status(404).send(new ErrorSerializer({"id": 14471, "code": 404, "message": "Not Found"}).serialize());
}
} else {
// But we still can force overwrite
var result;
sources.chain().find({ "id": parent_source_id }).update(function(item) {
let content;
if ( Array.isArray(req.body.content) ) {
content = req.body.content.join("\n");
} else {
content = req.body.content;
}
item.name = typeof req.body.name!=="undefined"?req.body.name:item.name;
item.content = typeof req.body.content!=="undefined"?content:item.content;
item.password = typeof req.body.password!=="undefined"?req.body.password:item.password;
item.version = typeof req.body.version!=="undefined"?parseInt(req.body.version, 10):parseInt(item.version, 10);
result = item;
});
if ( typeof result!=="undefined" ) {
dbSources.save();
res.header("Location", "/v"+version+"/sources/"+source_id);
res.status(200).send({ "code": 200, message: "Successfully updated", source: new SourceSerializer(result).serialize() });
} else {
res.status(404).send(new ErrorSerializer({"id": 14473, "code": 404, "message": "Not Found"}).serialize());
}
}
});
/**
* @api {delete} /sources/:source_id Delete a Source
* @apiName Delete a Source
* @apiGroup 6. Source and Over The Air OTA
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [source_id] Source Id
* @apiUse 200
* @apiUse 401
* @apiUse 404
*/
router.delete("/:source_id([0-9a-z\-]+)", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var source_id = req.params.source_id;
if ( !source_id ) {
res.status(405).send(new ErrorSerializer({"id": 14056, "code": 405, "message": "Method Not Allowed"}).serialize());
}
if ( !req.user.id ){
// Not Authorized because token is invalid
res.status(401).send(new ErrorSerializer({"id": 14274, "code": 401, "message": "Not Authorized"}).serialize());
} else if ( source_id ) {
sources = dbSources.getCollection("sources");
var query = {
"$and": [
{ "user_id" : req.user.id },
{ "id" : source_id },
]
};
let source = sources.findOne(query);
if ( source ) {
sources.remove(source);
dbSources.saveDatabase();
t6events.addAudit("t6Api", " delete", req.user.id, source_id, {error_id: null, status: 200});
res.status(200).send({ "code": 200, message: "Successfully deleted", removed_id: source_id }); // TODO: missing serializer
} else {
t6events.addAudit("t6Api", " delete", req.user.id, source_id, {error_id: 14271, status: 404});
res.status(404).send(new ErrorSerializer({"id": 14271, "code": 404, "message": "Not Found"}).serialize());
}
} else {
t6events.addAudit("t6Api", " delete", req.user.id, source_id, {error_id: 14272, status: 401});
res.status(401).send(new ErrorSerializer({"id": 14272, "code": 401, "message": "Forbidden"}).serialize());
}
});
t6console.log(`Route ${path.basename(__filename)} loaded`.padEnd(59));
module.exports = router;