2019-11-24 01:09:37 +01:00
|
|
|
import Book from '../../data/Book';
|
2020-04-28 23:07:21 +02:00
|
|
|
import Auth from '../../data/Auth';
|
2020-07-06 23:04:29 +02:00
|
|
|
import Settings, { SETTINGS_FILE } from '../../data/Settings';
|
2019-11-23 17:40:51 +01:00
|
|
|
import * as DataEvent from '../../../src/com/events/DataEvent';
|
2020-07-06 23:04:29 +02:00
|
|
|
import Render from '../../data/Render';
|
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();
|
2020-05-05 22:44:45 +02:00
|
|
|
const auth = new Auth();
|
2020-05-21 23:32:19 +02:00
|
|
|
const settings = new Settings();
|
2020-07-06 23:04:29 +02:00
|
|
|
const render = new Render();
|
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');
|
2020-06-21 05:53:45 +02:00
|
|
|
|
2019-11-20 02:17:39 +01:00
|
|
|
var storage = multer.diskStorage({
|
2020-04-28 23:07:21 +02:00
|
|
|
destination: function (req, file, cb) {
|
2020-06-21 05:53:45 +02:00
|
|
|
fs.ensureDir(uploadPath, () => {
|
|
|
|
// dir has now been created, including the directory it is to be placed in
|
|
|
|
cb(null, uploadPath);
|
|
|
|
});
|
2019-11-20 02:17:39 +01:00
|
|
|
},
|
2020-04-28 23:07:21 +02:00
|
|
|
filename: function (req, file, cb) {
|
2019-11-20 02:17:39 +01:00
|
|
|
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
|
|
|
/**
|
2020-07-12 23:25:35 +02:00
|
|
|
* Retrieves a page of a published entries
|
2020-03-01 06:01:34 +01:00
|
|
|
* @public
|
|
|
|
*/
|
2020-07-12 23:25:35 +02:00
|
|
|
router.get('/published/:pageNum?', (req, res) => {
|
|
|
|
//console.log('PAGE NUM', req.params.pageNum);
|
|
|
|
let pageNum = req.params.pageNum;
|
|
|
|
if (pageNum === null || pageNum === '' || !pageNum) pageNum = 1;
|
|
|
|
let pages = [];
|
2019-11-24 01:09:37 +01:00
|
|
|
book.getPage().then(result => {
|
2020-07-12 23:25:35 +02:00
|
|
|
result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id));
|
|
|
|
let displayed = _.filter(result, page => {
|
|
|
|
return (
|
|
|
|
page.metadata.deleted === false &&
|
|
|
|
page.metadata.published === true &&
|
|
|
|
page.metadata.layout != 'index'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
var pageLimit = 6;
|
|
|
|
var count = Math.ceil(displayed.length / pageLimit);
|
|
|
|
if (pageNum > count || isNaN(pageNum))
|
|
|
|
res.json({ type: DataEvent.REQUEST_LAME, message: "That page doesn't exist, champ." });
|
|
|
|
var rangeIndex = pageNum * pageLimit - pageLimit;
|
|
|
|
|
|
|
|
let meta = [];
|
|
|
|
|
|
|
|
for (let index = 0; index < pageLimit; index++) {
|
|
|
|
const page = displayed[index + rangeIndex];
|
|
|
|
try {
|
|
|
|
if (
|
|
|
|
page.metadata.id != null &&
|
|
|
|
page.metadata.deleted === false &&
|
|
|
|
page.metadata.published === true
|
|
|
|
) {
|
|
|
|
let entry = page.metadata;
|
|
|
|
entry.content = page.content;
|
|
|
|
//console.log('ENTRY', entry);
|
|
|
|
pages.push({
|
|
|
|
page: entry,
|
|
|
|
displayDate: moment(page.metadata.created).fromNow()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
//console.log("NO POST", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
meta.push({ currentPage: pageNum, totalPages: count });
|
|
|
|
let data = { pages: pages, meta: meta };
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_GOOD,
|
|
|
|
message: 'This is Page ' + pageNum + ' of ' + count,
|
|
|
|
data: data
|
|
|
|
});
|
2019-11-20 02:17:39 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-12 23:25:35 +02:00
|
|
|
/**
|
|
|
|
* Retrieves single entry
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
|
|
|
|
router.get('/single/:id', (req, res) => {
|
|
|
|
let id = req.params.id;
|
|
|
|
if (id === null || id === '')
|
|
|
|
res.json({ type: DataEvent.REQUEST_LAME, message: " Nah, this isn't here." });
|
|
|
|
book.getPage(id)
|
|
|
|
.then(page => {
|
|
|
|
let entry = page.metadata;
|
|
|
|
entry.content = page.content;
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_GOOD,
|
|
|
|
message: 'Found it. Here you go.',
|
|
|
|
data: entry
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: "This doesn't seem to be here, homie.",
|
|
|
|
err: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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) => {
|
2020-05-05 22:44:45 +02:00
|
|
|
auth.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 = '');
|
|
|
|
}
|
|
|
|
body.feature = feature;
|
|
|
|
body.deleted = false;
|
2020-06-29 22:45:35 +02:00
|
|
|
//if title changes, get rid of a pages with old title
|
|
|
|
if (body.current_title !== body.slug) {
|
|
|
|
let path =
|
|
|
|
moment(body.created).format('YYYY') + '/' + moment(body.created).format('MM');
|
|
|
|
|
|
|
|
//remove html page
|
|
|
|
fs.unlink('public/' + path + '/' + body.current_title + '.html')
|
|
|
|
.then()
|
|
|
|
.catch(() => {
|
|
|
|
//console.log('HTML ERROR', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
//remove markdown
|
|
|
|
fs.unlink('content/pages/' + path + '/' + body.current_title + '.md')
|
|
|
|
.then()
|
|
|
|
.catch(() => {
|
|
|
|
//console.log('MD ERROR', err);
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 22:44:45 +02:00
|
|
|
book.editPage(body, body.page_uuid, task, req.session.user)
|
|
|
|
.then(result => {
|
2020-05-21 23:32:19 +02:00
|
|
|
if (result.type === DataEvent.PAGE_ADDED) {
|
|
|
|
settings.updatePageIndex();
|
2020-05-05 22:44:45 +02:00
|
|
|
}
|
2020-07-06 23:04:29 +02:00
|
|
|
//load all page data and render if render on save flag is set in settings file
|
|
|
|
getBookData()
|
|
|
|
.then(result => {
|
|
|
|
if (result.settings.global.renderOnSave === 'true') {
|
|
|
|
render
|
|
|
|
.publishAll(
|
|
|
|
result.pages,
|
|
|
|
result.settings.global.theme,
|
|
|
|
req.session.user.handle
|
|
|
|
)
|
|
|
|
.then(response => {
|
|
|
|
res.json({
|
|
|
|
type: response.type,
|
|
|
|
message: response.message
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.PAGES_NOT_RENDERED,
|
|
|
|
message: 'Uh oh. Pages not rendered, sport',
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//console.log('DONT RENDER PAGES');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2020-07-12 23:25:35 +02:00
|
|
|
//console.log();
|
2020-07-06 23:04:29 +02:00
|
|
|
});
|
2020-05-05 22:44:45 +02:00
|
|
|
res.json(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
|
|
|
});
|
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) => {
|
2020-05-05 22:44:45 +02:00
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
book.editPage([], req.body.id, DataEvent.API_PAGE_DELETE, req.session.user)
|
|
|
|
.then(result => {
|
|
|
|
//remove item from menu in settings
|
|
|
|
res.json(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json(err);
|
|
|
|
});
|
2019-11-24 21:23:01 +01:00
|
|
|
});
|
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Uploads image from a Page content
|
|
|
|
*/
|
|
|
|
|
2020-04-28 23:07:21 +02:00
|
|
|
router.post('/add-post-image', post_upload, function (req, res) {
|
2019-11-26 01:51:35 +01:00
|
|
|
//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;
|
2020-07-06 23:04:29 +02:00
|
|
|
|
|
|
|
function getBookData() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let getSettings = settings.load(SETTINGS_FILE);
|
|
|
|
let getBook = book.getPage();
|
|
|
|
Promise.all([getSettings, getBook])
|
|
|
|
.then(result => {
|
|
|
|
const [settings, pages] = result;
|
|
|
|
let data = { settings: settings, pages: pages };
|
|
|
|
resolve(data);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|