import Book from '../../data/Book'; import StringUtils from '../../../src/com/utils/StringUtils'; import * as DataEvent from '../../../src/com/events/DataEvent'; import sanitize from 'sanitize-html'; import RightsManager, { TASK_CREATE, TASK_UPDATE, OBJECT_POST } from '../../../src/com/utils/RightsManager'; const express = require('express'); const router = express.Router(); const multer = require('multer'); const md = require('markdown-it')('commonmark'); const fs = require('fs-extra'); const moment = require('moment'); const rightsManager = new RightsManager(); const book = new Book(); const uploadPath = './public/assets/images/blog/' + moment().format('YYYY') + '/' + moment().format('MM'); const _ = require('lodash'); 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) => { book.getPage().then(result => { res.json(result); }); }); /*** Update Page */ router.post('/write', feature_upload, (req, res) => { var feature = ''; if (req.files.lengh > 0) { } else { var url = req.body.feature_image; feature = url.substring(21, url.length); } 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: ' + req.body.created + '\n' + 'updated: ' + moment(Date.now()).format() + '\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/test.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 res.json({ type: DataEvent.PAGE_UPDATED, message: 'Page Has been saved' }); }); }); module.exports = router;