-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
536 lines (497 loc) · 17.7 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
var express = require('express');
var multer = require('multer');
var app = express();
var fs = require("fs");
var async = require('async');
var zip = require('express-zip');
const path = require('path');
app.use(express.static(__dirname + '/bower_components'));
app.use(express.static(__dirname + '/client'));
app.use(express.static(__dirname + '/uploads'));
// private
app.post('/add/module', addModule);
app.put('/add/soundmodule', addSoundModule);
// public
app.get('/download', downloadFile);
app.delete('/delete/sound', deleteSound);
app.delete('/delete/text', deleteText);
app.get('/get/json', getJson);
app.get('/get/textjson', getTextJson);
app.post('/add/file', uploadFile);
app.put('/add/langaudio', addLangAudio);
app.put('/add/langtext', addLangText);
app.patch('/patch/langcode', patchLangCode);
app.get('/', home);
var server = app.listen(3000, '0.0.0.0', function() {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
function home(req, res) {
res.sendFile(__dirname + "/index.html");
}
function deleteSound(req, res) {
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
soundData = JSON.parse(jsonString);
var file = "./db/" + soundData.module + ".json";
fs.readFile(file, 'utf8', function(err, data) {
data = JSON.parse(data);
var filePath = "./uploads/" + data[soundData.module][soundData.audio]["lang"][soundData.lang];
delete data[soundData.module][soundData.audio]["lang"][soundData.lang];
console.log(data)
fs.writeFile(file, JSON.stringify(data), 'utf8', function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
return res.end(JSON.stringify({
"error": err
}));
} else {
fs.unlink(filePath, (err) => {
if (err) throw err;
console.log('successfully deleted' + filePath);
res.writeHead(204);
return res.end();
});
}
})
});
});
}
function deleteText(req, res) {
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
textData = JSON.parse(jsonString);
var file = "./textdb/" + textData.module + ".json";
fs.readFile(file, 'utf8', function(err, data) {
data = JSON.parse(data);
delete data[textData.module][textData.text]["lang"][textData.lang];
fs.writeFile(file, JSON.stringify(data), 'utf8', function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
return res.end(JSON.stringify({
"error": err
}));
} else {
console.log('successfully deleted');
res.writeHead(204);
return res.end();
}
});
});
});
}
function addSoundModule(req, res) {
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
soundData = JSON.parse(jsonString);
var file = "./db/" + soundData.module + ".json";
fs.readFile(file, 'utf8', function(err, data) {
data = JSON.parse(data);
for (var sound in soundData.soundModule) {
if (!data[soundData.module][sound]) {
data[soundData.module][sound] = soundData.soundModule[sound];
} else {
res.writeHead(409, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify({
"Conflict": "Sound key already exists"
}));
return;
}
}
console.log(data)
fs.writeFile(file, JSON.stringify(data), 'utf8', function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify({
"error": err
}));
return;
} else {
res.writeHead(201, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify(data));
return;
}
})
});
});
}
function addLangAudio(req, res) {
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
audioData = JSON.parse(jsonString);
var file = "./db/" + audioData.module + ".json";
fs.readFile(file, 'utf8', function(err, data) {
data = JSON.parse(data);
data[audioData.module][audioData.audio]['lang'][audioData.language] = audioData.filename;
fs.writeFile(file, JSON.stringify(data), 'utf8', function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify({
"error": err
}));
return;
} else {
res.writeHead(201, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify(data));
return;
}
})
});
});
}
function addLangText(req, res) {
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
textData = JSON.parse(jsonString);
var file = "./textdb/" + textData.module + ".json";
fs.readFile(file, 'utf8', function(err, data) {
data = JSON.parse(data);
data[textData.module][textData.text]['lang'][textData.language] = textData.type;
fs.writeFile(file, JSON.stringify(data), 'utf8', function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify({
"error": err
}));
return;
} else {
res.writeHead(201, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify(data));
return;
}
})
});
});
}
function addModule(req, res) {
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
audioData = JSON.parse(jsonString);
for (var module in audioData) {
if (audioData.hasOwnProperty(module)) {
fs.open('./db/' + module + '.json', "wx", function(err, fd) {
// handle error
console.log('creating : ', audioData)
if (err) {
if (err.code === "EEXIST") {
res.writeHead(409, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify({
'Conflict': 'The file already exists'
}));
return;
} else {
throw err;
return;
}
}
fs.write(fd, JSON.stringify(audioData), function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify({
"error": err
}));
return;
} else {
res.writeHead(201, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify(audioData));
return;
}
});
fs.close(fd, function(err) {
// handle error
});
});
}
}
});
}
function uploadFile(req, res) {
console.log(req)
upload(req, res, function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
return res.end(JSON.stringify({
"error": err
}));
}
// file details to be used after upload
res.writeHead(201, {
'Content-Type': 'text/json'
});
res.end(JSON.stringify(req.file));
return;
});
}
function getJson(req, res) {
var dir = './db/';
readAllFile(dir, res);
}
function getTextJson(req, res) {
var dir = './textdb/';
readAllTextFile(dir, res);
}
function downloadFile(req, res) {
var dir = './db/';
var upload = './uploads/';
var data = [];
var language = req.query.lang || false;
if(language){
fs.readdir(dir, (err, files) => {
console.log(files)
async.eachSeries(
files,
function(filename, cb) {
fs.readFile(dir + filename, function(err, content) {
if (!err) {
var filecontent = JSON.parse(content);
for (key in filecontent) {
if (filecontent.hasOwnProperty(key)) {
for (var audioModule in filecontent[key]) {
if (filecontent[key].hasOwnProperty(audioModule)) {
for (var prop in filecontent[key]) {
if (filecontent[key][prop].hasOwnProperty("lang")) {
if (filecontent[key][prop]["lang"].hasOwnProperty(language)) {
data.push({
"path" : './uploads/' + filecontent[key][prop]["lang"][language],
"name" : filecontent[key][prop]["lang"][language]
})
}
}
else{
console.log("no lang property")
}
}
}
}
}
}
}
cb(err);
});
},
function(err) {
if (err) {
res.writeHead(400, {
'Content-Type': 'text/json'
});
return res.end(JSON.stringify({
"error": err
}))
} else {
if(data.length)
return res.zip(data);
else{
res.writeHead(404, {
'Content-Type': 'text/json'
});
return res.end(JSON.stringify({"Not found": "no files found for the specified language"}))
}
}
}
)
})
}else{
fs.readdir(upload, (err, files) => {
files.forEach(file => {
data.push({
"path": upload + file,
"name": file
})
})
return res.zip(data);
})
}
}
function patchLangCode( req, res ){
var jsonString = '';
req.on('data', function(data) {
jsonString += data;
});
req.on('end', function() {
patchAllFile('./db/', res, JSON.parse(jsonString))
});
}
function patchAllFile(dir, response, languageObj){
var data = [];
fs.readdir(dir, (err, files) => {
async.eachSeries(
files,
function(filename, cb) {
fs.readFile(dir + filename, function(err, content) {
if (!err) {
var patchedContent;
for (var language in languageObj) {
if (languageObj.hasOwnProperty(language)) {
var contentString = JSON.stringify(JSON.parse(content));
var regex = new RegExp("(\""+language+"\")",'g');
patchedContent = contentString.replace(regex, "\""+languageObj[language]+"\"");
}
}
fs.writeFile(dir + filename, patchedContent, 'utf8', function(err) {
if (err) {
console.log(err)
} else {
data.push(JSON.parse(patchedContent))
}
})
}
cb(err);
});
},
function(err) {
if (err) {
response.writeHead(400, {
'Content-Type': 'text/json'
});
return response.end(JSON.stringify({
"error": err
}))
} else {
response.writeHead(200, {
"Content-Type": "text/json"
});
response.end(JSON.stringify(data))
}
}
)
})
}
// read file
function readAllFile(dir, response) {
var data = {};
fs.readdir(dir, (err, files) => {
async.eachSeries(
files,
function(filename, cb) {
fs.readFile(dir + filename, function(err, content) {
if (!err) {
var filecontent = JSON.parse(content);
for (key in filecontent) {
if (filecontent.hasOwnProperty(key)) {
data[key] = filecontent[key]
}
}
}
cb(err);
});
},
function(err) {
if (err) {
response.writeHead(400, {
'Content-Type': 'text/json'
});
return response.end(JSON.stringify({
"error": err
}))
} else {
response.writeHead(200, {
"Content-Type": "text/json"
});
response.end(JSON.stringify(data))
}
}
)
})
}
function readAllTextFile(dir, response) {
var data = {};
fs.readdir(dir, (err, files) => {
async.eachSeries(
files,
function(filename, cb) {
fs.readFile(dir + filename, function(err, content) {
if (!err) {
var filecontent = JSON.parse(content);
for (key in filecontent) {
if (filecontent.hasOwnProperty(key)) {
data[key] = filecontent[key]
}
}
}
cb(err);
});
},
function(err) {
if (err) {
response.writeHead(400, {
'Content-Type': 'text/json'
});
return response.end(JSON.stringify({
"error": err
}))
} else {
response.writeHead(200, {
"Content-Type": "text/json",
'Content-Type': 'application/xhtml+xml; charset=utf-8'
});
response.end(JSON.stringify(data))
}
}
)
})
}
var storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, './uploads');
},
filename: function(req, file, callback) {
callback(null, Date.now() + '.mp3');
}
});
var upload = multer({
fileFilter: function (req, file, cb) {
var filetypes = /mp3/;
var mimetype = filetypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb("File upload only supports the following filetypes - " + filetypes);
},
storage: storage
}).single('audioFile');