fipamo/brain/api/content/posts.js

116 lines
3 KiB
JavaScript

import DateUtils from '../../utils/tools/DateUtils';
import StringUtils from '../../utils/tools/StringUtils';
import * as DataEvent from '../../utils/events/DataEvent';
import sanitize from 'sanitize-html';
import RightsManager, {
TASK_CREATE,
TASK_UPDATE,
OBJECT_POST
} from '../../utils/tools/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 dateUtils = new DateUtils();
const rightsManager = new RightsManager();
const uploadPath =
'./public/assets/images/blog/' +
dateUtils.getDate('year', new Date()) +
'/' +
dateUtils.getDate('month', new Date());
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const _ = require('lodash');
fs.ensureDir(uploadPath, () => {
//console.log(err) // => null
// 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');
//** SYNC POSTS */
router.post('/sync', (req, res) => {
let payload = req.body;
});
router.get('/json/:filter?', function(req, res) {
var filter = req.params.filter;
});
/***
POST IMAGE
*/
router.post('/add-post-image', function(req, res) {
//console.log(req.body);
if (!req.session.user)
return res.json({
message: 'You need to be logged in, champ.'
});
Models.User.findById(req.session.user.id).then(user => {
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) {
post_upload(req, res, function(err) {
if (err) {
//console.log('Error in Saving Entry: ' + err);
res.json({
message: err
});
throw err;
} else {
var postImage = req.files[0].path;
return res.json({
message: DataEvent.POST_IMAGE_ADDED,
url: postImage.substr(7, postImage.length)
});
}
});
} else {
res.json({
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
router.post('/add-feature-image', function(req, res) {
//console.log(req.body);
if (!req.session.user)
return res.json({
message: 'You need to be logged in, champ.'
});
Models.User.findById(req.session.user.id).then(user => {
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) {
feature_upload(req, res, function(err) {
if (err) {
//console.log('Error in Saving Entry: ' + err);
res.json({
message: err
});
throw err;
} else {
var postImage = req.files[0].path;
return res.json({
message: DataEvent.FEATURE_IMAGE_ADDED,
url: postImage.substr(7, postImage.length)
});
}
});
} else {
res.json({
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
});
module.exports = router;