|
|
@ -31,8 +31,9 @@ var post_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
storage: storage
|
|
|
|
}).array('post_image');
|
|
|
|
}).array('post_image');
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/**
|
|
|
|
Retrieve Pages
|
|
|
|
* Retrives list of Pages
|
|
|
|
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
router.get('/', (req, res) => {
|
|
|
|
router.get('/', (req, res) => {
|
|
|
|
book.getPage().then(result => {
|
|
|
|
book.getPage().then(result => {
|
|
|
@ -40,107 +41,78 @@ router.get('/', (req, res) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/**
|
|
|
|
Update Page
|
|
|
|
* Add/Update Page
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
router.post('/write/:task?', feature_upload, (req, res) => {
|
|
|
|
router.post('/write/:task?', feature_upload, (req, res) => {
|
|
|
|
if (req.session.user) {
|
|
|
|
if (req.session.user) {
|
|
|
|
//Get enctrypted hashed token from header request
|
|
|
|
authCheck(req)
|
|
|
|
let hash = req.headers['x-access-token'];
|
|
|
|
.then(() => {
|
|
|
|
//Checks if token is a proper hash, if not reject
|
|
|
|
let body = _.mapValues(req.body);
|
|
|
|
if (!isTokenValid(req.session.token, hash)) {
|
|
|
|
let feature = '';
|
|
|
|
res.json({
|
|
|
|
let task = '';
|
|
|
|
type: DataEvent.API_REQUEST_LAME,
|
|
|
|
req.params.task === 'new'
|
|
|
|
message: 'Invalid Token. Auth Blocked'
|
|
|
|
? (task = DataEvent.API_PAGE_CREATE)
|
|
|
|
});
|
|
|
|
: (task = DataEvent.API_PAGE_WRITE);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//console.log('TOKEN IS GOOD');
|
|
|
|
|
|
|
|
var member = req.session.user;
|
|
|
|
|
|
|
|
jwt.verify(req.session.token, member.key, function(err, decoded) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
console('NOPE', err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('YUP', decoded);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var feature = '';
|
|
|
|
|
|
|
|
if (req.files.length > 0) {
|
|
|
|
if (req.files.length > 0) {
|
|
|
|
var path = req.files[0].path;
|
|
|
|
var path = req.files[0].path;
|
|
|
|
feature = '/' + path.substring(7, path.length);
|
|
|
|
feature = '/' + path.substring(7, path.length);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var url = req.body.feature_image;
|
|
|
|
var url = body.feature_image;
|
|
|
|
url != null || url != undefined || url != ''
|
|
|
|
url != null || url != undefined || url != ''
|
|
|
|
? (feature = url.substring(21, url.length))
|
|
|
|
? (feature = url.substring(21, url.length))
|
|
|
|
: (feature = '');
|
|
|
|
: (feature = '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body.feature = feature;
|
|
|
|
var pageWrite =
|
|
|
|
body.deleted = false;
|
|
|
|
'---\n' +
|
|
|
|
book.editPage(body, body.page_uuid, task, req.session.user)
|
|
|
|
'id: ' +
|
|
|
|
.then(result => {
|
|
|
|
req.body.page_id +
|
|
|
|
if (result.type === DataEvent.PAGE_CREATE) {
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'uuid: ' +
|
|
|
|
|
|
|
|
req.body.page_uuid +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'title: ' +
|
|
|
|
|
|
|
|
req.body.title +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'feature: ' +
|
|
|
|
|
|
|
|
feature +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'layout: ' +
|
|
|
|
|
|
|
|
'page' +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'tags: ' +
|
|
|
|
|
|
|
|
req.body.tags +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'author: ' +
|
|
|
|
|
|
|
|
req.session.user.handle +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'created: ' +
|
|
|
|
|
|
|
|
moment(req.body.created).format() +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'updated: ' +
|
|
|
|
|
|
|
|
moment(Date.now()).format() +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'menu: ' +
|
|
|
|
|
|
|
|
req.body.pinToMenu +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'featured: ' +
|
|
|
|
|
|
|
|
req.body.featureStatus +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'published: ' +
|
|
|
|
|
|
|
|
req.body.publishedStatus +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'slug: ' +
|
|
|
|
|
|
|
|
req.body.slug +
|
|
|
|
|
|
|
|
'\n' +
|
|
|
|
|
|
|
|
'---\n\n' +
|
|
|
|
|
|
|
|
req.body.content;
|
|
|
|
|
|
|
|
fs.writeFile('content/pages/' + req.body.slug + '.md', pageWrite, err => {
|
|
|
|
|
|
|
|
// throws an error, you could also catch it here
|
|
|
|
|
|
|
|
if (err) res.json({ type: DataEvent.PAGE_ERROR, message: err });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// success case, the file was saved
|
|
|
|
|
|
|
|
if (req.params.task === 'new') {
|
|
|
|
|
|
|
|
// if new file, update settings index and page count
|
|
|
|
|
|
|
|
fs.readJSON('site/settings.json').then(settings => {
|
|
|
|
fs.readJSON('site/settings.json').then(settings => {
|
|
|
|
settings.library_stats.current_index = ++settings.library_stats.current_index;
|
|
|
|
settings.library_stats.current_index = ++settings.library_stats
|
|
|
|
settings.library_stats.total_pages = ++settings.library_stats.total_pages;
|
|
|
|
.current_index;
|
|
|
|
|
|
|
|
settings.library_stats.total_pages = ++settings.library_stats
|
|
|
|
|
|
|
|
.total_pages;
|
|
|
|
fs.writeJSON('site/settings.json', settings);
|
|
|
|
fs.writeJSON('site/settings.json', settings);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
res.json({
|
|
|
|
}
|
|
|
|
type: DataEvent.PAGE_ADDED,
|
|
|
|
res.json(result);
|
|
|
|
message: 'New Page Created',
|
|
|
|
})
|
|
|
|
id: req.body.page_uuid
|
|
|
|
.catch(err => {
|
|
|
|
|
|
|
|
res.json(err);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch(err => {
|
|
|
|
|
|
|
|
res.json(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
|
|
|
res.json({ type: DataEvent.PAGE_UPDATED, message: 'Page saved, boss' });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Soft deletes Page
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Uploads image from a Page content
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/add-post-image', post_upload, function(req, res) {
|
|
|
|
router.post('/add-post-image', post_upload, function(req, res) {
|
|
|
|
//console.log(req.body);
|
|
|
|
//console.log(req.body);
|
|
|
@ -157,3 +129,39 @@ module.exports = router;
|
|
|
|
function isTokenValid(token, hashedToken) {
|
|
|
|
function isTokenValid(token, hashedToken) {
|
|
|
|
return bCrypt.compareSync(token, hashedToken);
|
|
|
|
return bCrypt.compareSync(token, hashedToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|