You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/brain/api/v1/pages.js

189 lines
4.6 KiB
JavaScript

import Book from '../../data/Book';
import Auth from '../../data/Auth';
import Settings, { SETTINGS_FILE } from '../../data/Settings';
import * as DataEvent from '../../../src/com/events/DataEvent';
import Render from '../../data/Render';
const express = require('express');
const router = express.Router();
const multer = require('multer');
const fs = require('fs-extra');
const moment = require('moment');
const book = new Book();
const auth = new Auth();
const settings = new Settings();
const render = new Render();
const _ = require('lodash');
const uploadPath =
'./public/assets/images/blog/' + moment().format('YYYY') + '/' + moment().format('MM');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
fs.ensureDir(uploadPath, () => {
// dir has now been created, including the directory it is to be placed in
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');
/**
* Retrieves list of Pages
* @public
*/
router.get('/', (req, res) => {
book.getPage().then(result => {
res.json(result);
});
});
/**
* Add/Update Page
*/
router.post('/write/:task?', feature_upload, (req, res) => {
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;
//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);
});
}
book.editPage(body, body.page_uuid, task, req.session.user)
.then(result => {
if (result.type === DataEvent.PAGE_ADDED) {
settings.updatePageIndex();
}
//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(() => {
console.log();
});
res.json(result);
})
.catch(err => {
res.json(err);
});
})
.catch(err => {
res.json(err);
});
});
/**
* Soft deletes Page
*/
router.post('/delete', (req, res) => {
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);
});
});
/**
* Uploads image from a Page content
*/
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)
});
});
module.exports = router;
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);
});
});
}