-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiManager.js
511 lines (448 loc) · 19.5 KB
/
apiManager.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
var express = require('express');
var router = express.Router()
const news_scraper = require('./news_scraper.js')
const { v4: uuidv4 } = require('uuid');
const validator = require('validator');
const assert = require('assert');
router.use((req, res, next) => {
next()
})
////////////////////////////////// IP MAC Test /////////////////////////////////////
router.post('/ipmac/student/create', (req, res, next) => {
const postData = req.body;
if (postData.name == '' || postData.status == '' || postData.name == null || postData.status == null) {
res.status(500).json({ 'status': 'wrong input' });
} else {
let dbcollection = dbClient.db("sample_airbnb").collection("ipmac");
//check if user already exist in database
dbcollection.find({ HoTen: postData.name }).toArray(function (err, docs) {
//test to see if the error is null or not. Null means no error.
assert.equal(err, null);
console.log('Found the following records');
console.log(docs.length);
if (docs.length > 0)
res.status(500).json({ 'status': 'user already exist' });
else {
//insert user to database
dbcollection.insertOne({ HoTen: postData.name, TrangThai: postData.status, HinhNho: null, HinhLon: null }).then(result => {
console.log(result)
console.log('Lưu dữ liệu thành công')
res.status(200).json({ 'status': 'insert ok' });
})
.catch(err => {
console.log(err)
res.status(500).json({ 'status': 'unknown' });
})
}
});
}
})
router.get('/ipmac/student/getall', (req, res, next) => {
const collection = dbClient.db("sample_airbnb").collection("ipmac");
// perform actions on the collection object
collection.find({}).toArray(function (err, docs) {
//test to see if the error is null or not. Null means no error.
assert.equal(err, null);
console.log('Found the following records');
console.log(docs);
res.status(200).json(docs);
});
})
router.post('/ipmac/uploadfile/:studentname/:imagesize', function (req, res) {
if (req.params.imagesize != 'small' && req.params.imagesize != 'large')
res.status(500).send('Invalid image size')
else {
//check if student exist
let dbcollection = dbClient.db("sample_airbnb").collection("ipmac");
dbcollection.find({ HoTen: req.params.studentname }).toArray(function (err, docs) {
//test to see if the error is null or not. Null means no error.
assert.equal(err, null);
console.log('Found the following records');
console.log(docs.length);
if (docs.length > 0) {
//cho phép upload
//Limit file size to 6MB only and the maximum number of files is only one at a time
var busboy = new Busboy({
headers: req.headers, limits: {
fileSize: 6 * 1024 * 1024,
files: 1
}
});
var limit_reach = false;
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
console.log('got file', filename, mimetype, encoding);
//This is where you want to check filetype to make sure before going any further
if (mimetype != 'image/png' && mimetype != 'image/jpeg' && mimetype != 'image/jpg')
res.status(500).json({ 'status': 'file type is not supported' });
else {
// If the file is larger than the set limit, then destroy the streaming
//There is a bug in gridfs regarding Destroy method, workaround's here: https://github.com/aheckmann/gridfs-stream/issues/153
file.on('limit', function () {
writeStream.destroy(new Error('Destroyed the stream cause File was too large'))
limit_reach = true;
res.status(500).send('Destroyed the stream cause File was too large');
});
var writeStream
let nameAfterProcessed
try {
//Remove Vietnamese characters and spaces
nameAfterProcessed = new Date().getTime() + removeAccents(filename).replace(/\s/g, "")
console.log('name after processed', nameAfterProcessed)
writeStream = gfs.createWriteStream({
filename: nameAfterProcessed,
content_type: mimetype,
});
} catch (error) {
console.log(error)
}
if (writeStream != null) {
writeStream.on('error', (err) => {
//This event will be fired after the stream has been destroyed and before emitting Close event below
console.log(err)
});
writeStream.on('close', (file) => {
//All the info of the uploaded file has been return (both in success or fail)
//Storing the fileID to your data model for later use.
console.log(file)
//check if File has been sucessfully uploaded or the stream was canceled by any reason.
if (limit_reach) {
//Delete all unessary chunks in grid fs chunks using fileID
deleteUnfinishedUploadedFile(file._id)
}
storeImageToStudentRecord(req.params.studentname, '/downloadFileByFileName/' + nameAfterProcessed, req.params.imagesize)
});
file.pipe(writeStream);
}
}
}).on('finish', function () {
// show a link to the uploaded file
if (!limit_reach)
res.status(200).send('uploaded successfully');
});
req.pipe(busboy);
}
else {
res.status(500).json({ 'status': 'no student found' });
}
});
}
});
function storeImageToStudentRecord(studentname, url, type) {
let dbcollection = dbClient.db("sample_airbnb").collection("ipmac");
var myquery = { HoTen: studentname };
var newvalues
if (type == 'small')
newvalues = { $set: { HinhNho: url } };
else
newvalues = { $set: { HinhLon: url } };
dbcollection.updateOne(myquery, newvalues, function (err, res) {
if (err) throw err;
console.log("1 document updated");
});
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
router.get('/news', (req, res, next) => {
news_scraper.getNews('https://tuoitre.vn/tin-moi-nhat.htm').then(result => {
res.send(result)
}).catch(err => {
console.log(err)
res.send('Failed to get news')
})
})
router.post('/thaibinhnews/:type', (req, res, next) => {
if (req.params.type == 'general' || req.params.type == 'medic' || req.params.type == 'edu') {
news_scraper.getThaibinhNews(req.params.type).then(result => {
res.status(200).json({ data: result })
}).catch(err => {
console.log(err)
res.status(500).send('Failed to get news')
})
}
else
res.status(500).send('News type is not valid')
})
router.post('/songcongnews/:type', (req, res, next) => {
if (req.params.type == 'general' || req.params.type == 'medic' || req.params.type == 'edu') {
news_scraper.getSongCongNews(req.params.type).then(result => {
res.status(200).json({ data: result })
}).catch(err => {
console.log(err)
res.status(500).send('Failed to get news')
})
}
else
res.status(500).send('News type is not valid')
})
router.post('/caobangnews/:type', (req, res, next) => {
if (req.params.type == 'general') {
news_scraper.getCaoBangNews(req.params.type).then(result => {
res.status(200).json({ data: result })
}).catch(err => {
console.log(err)
res.status(500).send('Failed to get news')
})
}
else
res.status(500).send('News type is not valid')
})
router.get('/news/:page', (req, res, next) => {
console.log(req.params.page)
if (Number.isInteger(parseInt(req.params.page))) {
news_scraper.getMoreNews('https://tuoitre.vn/timeline/0/trang-' + req.params.page + '.htm').then(result => {
res.send(result)
}).catch(err => {
console.log(err)
res.send('Failed to get more news')
})
}
else {
res.send('Page number is not valid')
}
})
router.post('/emailValidate', async (req, res, next) => {
const postData = req.body;
if (postData.email) {
console.info('/emailValidate call success ');
//Validator only takes string as an input param, so remember to convert everything to string first
res.json({ 'status': validator.isEmail(postData.email + '') });
} else {
console.warn('/emailValidate wrong input ');
res.status(500).json({ 'status': 'wrong input' });
}
});
/////////////////////////////////////////////// CronJob scheduler////////////////////////////////
const { ToadScheduler } = require('toad-scheduler')
const cronJobsManager = require('./cronjobsMangers.js')
// A Middleware to handle cronjib
const scheduler = new ToadScheduler()
router.post('/setAutoNewsScrapping', async (req, res, next) => {
const postData = req.body;
postData['jobID'] = 'news' + uuidv4();
console.log(postData.jobID)
cronJobsManager.autoNewsScrappingtoDBEvery(postData, scheduler).then(result => {
res.status(200).send('Set cron job ' + postData.jobID + ' OK')
})
.catch(err => {
res.status(500).send('Set cron job ' + postData.jobID + ' Fail')
})
});
router.post('/setEMAWatcher', async (req, res, next) => {
const postData = req.body;
postData['jobID'] = 'ema' + uuidv4();
console.log(postData.jobID)
cronJobsManager.emaWatcher(postData, scheduler).then(result => {
res.status(200).send('Set cron job ' + postData.jobID + ' OK')
})
.catch(err => {
res.status(500).send('Set cron job ' + postData.jobID + ' Fail')
})
});
router.get('/removeCronJob/:jobID', (req, res, next) => {
if (cronJobsManager.cancelCronJob(req.params.jobID, scheduler))
res.status(200).send('Remove cron job ' + req.params.jobID + ' OK')
else
res.status(500).send('Remove cron job ' + req.params.jobID + ' Fail')
})
//create another express app just for proxy processing
// const appProxy = express()
// const { createProxyMiddleware } = require('http-proxy-middleware');
// appProxy.use('*', createProxyMiddleware({ target: 'http://thainguyen.edu.vn/', changeOrigin: true }));
// appProxy.listen(3500);
// const proxyTable = {
// 'edu.localhost:3500': 'http://thainguyen.edu.vn',
// 'general.localhost:3500': 'http://songcong.thainguyen.gov.vn',
// 'medic.localhost:3500': 'http://soytethainguyen.gov.vn',
// };
// const options = {
// target: 'http://localhost:3500',
// router: proxyTable,
// changeOrigin: true
// };
// const proxyserver = createProxyMiddleware(options);
// appProxy.use(proxyserver)
// appProxy.listen(3500)
///////////////////////////////MongoDBtest///////////////////////////////////////
const dbManager = require('./mongoDB/connectionManager.js')
//busboy is a middleware to handle parsing data sent through multipart form-data
const Busboy = require('busboy');
const { ObjectID } = require('bson');
global.gfs = null
global.dbClient = null
global.startDBConnection = () => {
dbManager.dbConnectionInit().then(client => {
// res is DB client
dbClient = client
dbManager.gridFsInit(client).then(res => {
gfs = res
}).catch(err => { console.log(err) })
})
.catch(err => {
DBError = err
console.log(err)
});
}
//////////////////////////////////////////GRID Fs operations///////////////////////////////
router.post('/uploadfile', function (req, res) {
//Limit file size to 12MB only and the maximum number of files is only one at a time
var busboy = new Busboy({
headers: req.headers, limits: {
fileSize: 12 * 1024 * 1024,
files: 1
}
});
var limit_reach = false;
let nameAfterProcessed
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
console.log('got file', filename, mimetype, encoding);
//This is where you want to check filetype to make sure before going any further
// If the file is larger than the set limit, then destroy the streaming
//There is a bug in gridfs regarding Destroy method, workaround's here: https://github.com/aheckmann/gridfs-stream/issues/153
file.on('limit', function () {
writeStream.destroy(new Error('Destroyed the stream cause File was too large'))
limit_reach = true;
res.status(500).send('Destroyed the stream cause File was too large');
});
var writeStream
try {
//Remove Vietnamese characters and spaces
nameAfterProcessed = removeAccents(filename).replace(/\s/g, "") + new Date().toTimeString()
console.log('name after processed', nameAfterProcessed)
writeStream = gfs.createWriteStream({
filename: nameAfterProcessed,
content_type: mimetype,
});
} catch (error) {
console.log(error)
}
if (writeStream != null) {
writeStream.on('error', (err) => {
//This event will be fired after the stream has been destroyed and before emitting Close event below
console.log(err)
});
writeStream.on('close', (file) => {
//All the info of the uploaded file has been return (both in success or fail)
//Storing the fileID to your data model for later use.
console.log(file)
//check if File has been sucessfully uploaded or the stream was canceled by any reason.
if (limit_reach) {
//Delete all unessary chunks in grid fs chunks using fileID
deleteUnfinishedUploadedFile(file._id)
}
else {
//save file to DB
}
});
file.pipe(writeStream);
}
}).on('finish', function () {
// show a link to the uploaded file
if (!limit_reach)
res.status(200).send({message: 'uploaded successfully', data:{fileName:nameAfterProcessed}});
});
req.pipe(busboy);
});
router.get('/downloadFileByFileName/:filename', function (req, res) {
//download file using file name.
let filename = req.params.filename
console.log('filename:' + filename)
gfs.exist({ filename: filename }, (err, file) => {
if (err || !file) {
res.status(404).send('File Not Found');
return
}
var readstream = gfs.createReadStream({ filename: filename });
readstream.pipe(res);
});
});
router.get('/downloadFileByFileID/:fileID', function (req, res) {
//download file using file ID.
var file_id = req.params.fileID;
gfs.files.find({ _id: new ObjectID(file_id) }).toArray(function (err, files) {
if (err) {
res.json(err);
}
if (files.length > 0) {
var mime = files[0].contentType;
var filename = files[0].filename;
res.set('Content-Type', mime);
// res.set('Content-Disposition', "inline; filename=" + filename);
var read_stream = gfs.createReadStream({ _id: file_id });
read_stream.pipe(res);
} else {
res.status(404).json('File Not Found');
}
});
});
router.get('/deleteFileByFileName/:filename', function (req, res) {
let condition = { "filename": req.params.filename }
gfs.remove(condition, (err, gridStore) => {
if (err) {
res.status(404).send('File Not Found');
return
}
else {
res.status(200).send('File:' + req.params.filename + 'has been removed from Database');
}
});
});
router.get('/deleteFileByFileID/:fileID', function (req, res) {
//delete file by FileID
var file_id = req.params.fileID;
gfs.remove({ _id: new ObjectID(file_id) }, (err, gridStore) => {
if (err) {
res.status(404).send('File Not Found');
return
}
else {
res.status(200).send('File ID:' + file_id + 'has been removed from Database');
}
});
});
router.get('/manuallyTriggerDatabaseConnection', function (req, res) {
// This api is used in cased you need to start or restart connection to mongoDB. And even a sleeping cloud server.
dbManager.dbConnectionInit().then(client => {
dbClient = client;
dbManager.gridFsInit(client).then(res => {
gfs = res
}).catch(err => { console.log(err) })
res.status(200).send('DB kết nối ổn')
})
.catch(err => {
console.log(err)
res.status(500).json({ errorMessage: 'Fail' })
});
});
function removeAccents(str) {
var AccentsMap = [
"aàảãáạăằẳẵắặâầẩẫấậ",
"AÀẢÃÁẠĂẰẲẴẮẶÂẦẨẪẤẬ",
"dđ", "DĐ",
"eèẻẽéẹêềểễếệ",
"EÈẺẼÉẸÊỀỂỄẾỆ",
"iìỉĩíị",
"IÌỈĨÍỊ",
"oòỏõóọôồổỗốộơờởỡớợ",
"OÒỎÕÓỌÔỒỔỖỐỘƠỜỞỠỚỢ",
"uùủũúụưừửữứự",
"UÙỦŨÚỤƯỪỬỮỨỰ",
"yỳỷỹýỵ",
"YỲỶỸÝỴ"
];
for (var i = 0; i < AccentsMap.length; i++) {
var re = new RegExp('[' + AccentsMap[i].substr(1) + ']', 'g');
var char = AccentsMap[i][0];
str = str.replace(re, char);
}
return str;
}
function deleteUnfinishedUploadedFile(file_id) {
gfs.remove({ _id: new ObjectID(file_id) }, (err, gridStore) => {
if (err) {
console.log(`File ID: ${file_id} UnusedFile has been removed`)
}
else {
console.log(`File ID: ${file_id} UnusedFile has been removed unsucessfully`)
}
});
}
module.exports = router