2019-11-16 18:10:05 +01:00
|
|
|
import DateUtils from '../../utils/tools/DateUtils';
|
|
|
|
import StringUtils from '../../utils/tools/StringUtils';
|
|
|
|
import * as DataEvent from '../../utils/events/DataEvent';
|
2018-12-06 22:49:49 +01:00
|
|
|
import sanitize from 'sanitize-html';
|
2019-02-27 17:17:51 +01:00
|
|
|
import RightsManager, {
|
|
|
|
TASK_CREATE,
|
|
|
|
TASK_UPDATE,
|
|
|
|
OBJECT_POST
|
2019-11-16 18:10:05 +01:00
|
|
|
} from '../../utils/tools/RightsManager';
|
2018-12-11 18:35:29 +01:00
|
|
|
const express = require('express');
|
2018-12-06 22:49:49 +01:00
|
|
|
const router = express.Router();
|
|
|
|
const multer = require('multer');
|
|
|
|
const md = require('markdown-it')('commonmark');
|
|
|
|
const fs = require('fs-extra');
|
2018-11-11 20:49:26 +01:00
|
|
|
const dateUtils = new DateUtils();
|
|
|
|
const rightsManager = new RightsManager();
|
2019-02-27 17:17:51 +01:00
|
|
|
const uploadPath =
|
|
|
|
'./content/blog-images/' +
|
|
|
|
dateUtils.getDate('year', new Date()) +
|
|
|
|
'/' +
|
|
|
|
dateUtils.getDate('month', new Date());
|
2018-12-06 22:49:49 +01:00
|
|
|
const Sequelize = require('sequelize');
|
2018-11-21 01:42:52 +01:00
|
|
|
const Op = Sequelize.Op;
|
2018-12-06 22:49:49 +01:00
|
|
|
const _ = require('lodash');
|
2019-02-27 17:17:51 +01:00
|
|
|
fs.ensureDir(uploadPath, () => {
|
|
|
|
//console.log(err) // => null
|
|
|
|
// dir has now been created, including the directory it is to be placed in
|
2018-11-11 20:49:26 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
|
2019-02-27 17:17:51 +01:00
|
|
|
var feature_upload = multer({
|
|
|
|
storage: storage
|
2018-11-11 20:49:26 +01:00
|
|
|
}).array('feature_image');
|
2019-02-27 17:17:51 +01:00
|
|
|
var post_upload = multer({
|
|
|
|
storage: storage
|
2018-11-11 20:49:26 +01:00
|
|
|
}).array('post_image');
|
2018-11-22 02:08:53 +01:00
|
|
|
//** SYNC POSTS */
|
2019-02-27 17:17:51 +01:00
|
|
|
router.post('/sync', (req, res) => {
|
|
|
|
let payload = req.body;
|
|
|
|
});
|
2019-02-27 22:09:39 +01:00
|
|
|
router.get('/json/:filter?', function(req, res) {
|
|
|
|
var filter = req.params.filter;
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2018-11-11 20:49:26 +01:00
|
|
|
/***
|
|
|
|
POST IMAGE
|
|
|
|
*/
|
2019-02-27 17:17:51 +01:00
|
|
|
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."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-11-11 20:49:26 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
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."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-11-26 23:14:13 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
module.exports = router;
|