2019-11-24 01:09:37 +01:00
|
|
|
import Book from '../../data/Book';
|
2019-11-23 17:40:51 +01:00
|
|
|
import * as DataEvent from '../../../src/com/events/DataEvent';
|
2019-11-20 02:17:39 +01:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const multer = require('multer');
|
|
|
|
const fs = require('fs-extra');
|
2019-11-24 01:09:37 +01:00
|
|
|
const moment = require('moment');
|
2019-12-02 22:07:16 +01:00
|
|
|
const jwt = require('jsonwebtoken');
|
2019-12-03 02:59:04 +01:00
|
|
|
const bCrypt = require('bcrypt-nodejs');
|
2019-11-24 01:09:37 +01:00
|
|
|
const book = new Book();
|
2019-12-03 02:59:04 +01:00
|
|
|
const _ = require('lodash');
|
2019-11-24 21:23:01 +01:00
|
|
|
const uploadPath =
|
|
|
|
'./public/assets/images/blog/' + moment().format('YYYY') + '/' + moment().format('MM');
|
2019-11-20 02:17:39 +01:00
|
|
|
fs.ensureDir(uploadPath, () => {
|
|
|
|
// dir has now been created, including the directory it is to be placed in
|
|
|
|
});
|
|
|
|
var storage = multer.diskStorage({
|
|
|
|
destination: function(req, file, cb) {
|
|
|
|
cb(null, uploadPath);
|
|
|
|
},
|
|
|
|
filename: function(req, file, cb) {
|
|
|
|
var splice = file.originalname.split(':');
|
|
|
|
cb(null, splice[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var feature_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
}).array('feature_image');
|
|
|
|
var post_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
}).array('post_image');
|
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Retrives list of Pages
|
|
|
|
* @public
|
|
|
|
*/
|
2019-11-20 02:17:39 +01:00
|
|
|
router.get('/', (req, res) => {
|
2019-11-24 01:09:37 +01:00
|
|
|
book.getPage().then(result => {
|
2019-11-20 02:17:39 +01:00
|
|
|
res.json(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Add/Update Page
|
|
|
|
*/
|
2019-11-25 22:58:34 +01:00
|
|
|
router.post('/write/:task?', feature_upload, (req, res) => {
|
2019-12-02 22:07:16 +01:00
|
|
|
if (req.session.user) {
|
2020-03-01 06:01:34 +01:00
|
|
|
authCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
let body = _.mapValues(req.body);
|
|
|
|
let feature = '';
|
|
|
|
let task = '';
|
|
|
|
req.params.task === 'new'
|
|
|
|
? (task = DataEvent.API_PAGE_CREATE)
|
|
|
|
: (task = DataEvent.API_PAGE_WRITE);
|
|
|
|
if (req.files.length > 0) {
|
|
|
|
var path = req.files[0].path;
|
|
|
|
feature = '/' + path.substring(7, path.length);
|
|
|
|
} else {
|
|
|
|
var url = body.feature_image;
|
|
|
|
url != null || url != undefined || url != ''
|
|
|
|
? (feature = url.substring(21, url.length))
|
|
|
|
: (feature = '');
|
2019-12-03 02:59:04 +01:00
|
|
|
}
|
2020-03-01 06:01:34 +01:00
|
|
|
body.feature = feature;
|
|
|
|
body.deleted = false;
|
|
|
|
book.editPage(body, body.page_uuid, task, req.session.user)
|
|
|
|
.then(result => {
|
|
|
|
if (result.type === DataEvent.PAGE_CREATE) {
|
|
|
|
fs.readJSON('site/settings.json').then(settings => {
|
|
|
|
settings.library_stats.current_index = ++settings.library_stats
|
|
|
|
.current_index;
|
|
|
|
settings.library_stats.total_pages = ++settings.library_stats
|
|
|
|
.total_pages;
|
|
|
|
fs.writeJSON('site/settings.json', settings);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
res.json(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
2019-12-03 02:59:04 +01:00
|
|
|
});
|
2019-11-25 01:11:12 +01:00
|
|
|
}
|
2020-03-01 06:01:34 +01:00
|
|
|
});
|
2019-11-25 01:11:12 +01:00
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Soft deletes Page
|
|
|
|
*/
|
2019-11-25 01:11:12 +01:00
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
router.post('/delete', (req, res) => {
|
|
|
|
if (req.session.user) {
|
|
|
|
authCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
book.editPage([], req.body.id, DataEvent.API_PAGE_DELETE, req.session.user)
|
|
|
|
.then(result => {
|
|
|
|
res.json(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
2019-11-25 22:58:34 +01:00
|
|
|
});
|
2020-03-01 06:01:34 +01:00
|
|
|
}
|
2019-11-24 21:23:01 +01:00
|
|
|
});
|
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Uploads image from a Page content
|
|
|
|
*/
|
|
|
|
|
2019-11-26 01:51:35 +01:00
|
|
|
router.post('/add-post-image', post_upload, function(req, res) {
|
|
|
|
//console.log(req.body);
|
|
|
|
var image = req.files[0].path;
|
|
|
|
return res.json({
|
|
|
|
type: DataEvent.POST_IMAGE_ADDED,
|
|
|
|
message: 'Added Image',
|
|
|
|
url: '/' + image.substr(7, image.length)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-20 02:17:39 +01:00
|
|
|
module.exports = router;
|
2019-12-03 02:59:04 +01:00
|
|
|
|
|
|
|
function isTokenValid(token, hashedToken) {
|
|
|
|
return bCrypt.compareSync(token, hashedToken);
|
|
|
|
}
|
2020-03-01 06:01:34 +01:00
|
|
|
|
|
|
|
function authCheck(req) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let hash = req.headers['x-access-token'];
|
|
|
|
let response = [];
|
|
|
|
//Checks if token is a proper hash, if not reject
|
|
|
|
if (!isTokenValid(req.session.token, hash)) {
|
|
|
|
response = {
|
|
|
|
status: false,
|
|
|
|
type: DataEvent.API_REQUEST_LAME,
|
|
|
|
message: 'No Token Present. Auth Blocked'
|
|
|
|
};
|
|
|
|
reject(response);
|
|
|
|
//res.json();
|
|
|
|
} else {
|
|
|
|
var member = req.session.user;
|
|
|
|
jwt.verify(req.session.token, member.key, function(err, decoded) {
|
|
|
|
if (err) {
|
|
|
|
response = {
|
|
|
|
status: false,
|
|
|
|
type: DataEvent.API_REQUEST_LAME,
|
|
|
|
message: 'Invalid Token. Auth Blocked'
|
|
|
|
};
|
|
|
|
reject(response);
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
status: true,
|
|
|
|
type: DataEvent.API_REQUEST_GOOD,
|
|
|
|
message: 'Token Verified',
|
|
|
|
token: decoded
|
|
|
|
};
|
|
|
|
resolve(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|