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');
|
|
|
|
const book = new Book();
|
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');
|
|
|
|
|
|
|
|
/***
|
|
|
|
Retrieve Pages
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-24 21:23:01 +01:00
|
|
|
/***
|
|
|
|
Update Page
|
|
|
|
*/
|
2019-11-25 22:58:34 +01:00
|
|
|
router.post('/write/:task?', feature_upload, (req, res) => {
|
2019-11-25 01:11:12 +01:00
|
|
|
var feature = '';
|
2019-11-25 22:58:34 +01:00
|
|
|
if (req.files.length > 0) {
|
|
|
|
var path = req.files[0].path;
|
|
|
|
feature = '/' + path.substring(7, path.length);
|
2019-11-25 01:11:12 +01:00
|
|
|
} else {
|
|
|
|
var url = req.body.feature_image;
|
2019-11-25 22:58:34 +01:00
|
|
|
url != null || url != undefined || url != ''
|
|
|
|
? (feature = url.substring(21, url.length))
|
|
|
|
: (feature = '');
|
2019-11-25 01:11:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var pageWrite =
|
|
|
|
'---\n' +
|
|
|
|
'id: ' +
|
|
|
|
req.body.page_id +
|
|
|
|
'\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: ' +
|
2019-11-25 22:58:34 +01:00
|
|
|
moment(req.body.created).format() +
|
2019-11-25 01:11:12 +01:00
|
|
|
'\n' +
|
|
|
|
'updated: ' +
|
|
|
|
moment(Date.now()).format() +
|
|
|
|
'\n' +
|
2019-11-25 22:58:34 +01:00
|
|
|
'menu: ' +
|
|
|
|
req.body.pinToMenu +
|
|
|
|
'\n' +
|
2019-11-25 01:11:12 +01:00
|
|
|
'featured: ' +
|
|
|
|
req.body.featureStatus +
|
|
|
|
'\n' +
|
|
|
|
'published: ' +
|
|
|
|
req.body.publishedStatus +
|
|
|
|
'\n' +
|
|
|
|
'slug: ' +
|
|
|
|
req.body.slug +
|
|
|
|
'\n' +
|
|
|
|
'---\n\n' +
|
|
|
|
req.body.content;
|
2019-11-25 22:58:34 +01:00
|
|
|
fs.writeFile('content/pages/' + req.body.slug + '.md', pageWrite, err => {
|
2019-11-25 01:11:12 +01:00
|
|
|
// 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
|
2019-11-25 22:58:34 +01:00
|
|
|
if (req.params.task === 'new') {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.PAGE_ADDED,
|
|
|
|
message: 'New Page Created',
|
|
|
|
id: req.body.page_uuid
|
|
|
|
});
|
|
|
|
} else {
|
2019-11-26 01:51:35 +01:00
|
|
|
res.json({ type: DataEvent.PAGE_UPDATED, message: 'Page saved, boss' });
|
2019-11-25 22:58:34 +01:00
|
|
|
}
|
2019-11-25 01:11:12 +01:00
|
|
|
});
|
2019-11-24 21:23:01 +01:00
|
|
|
});
|
|
|
|
|
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;
|