2019-11-16 18:10:05 +01:00
|
|
|
import DateUtils from '../../utils/tools/DateUtils';
|
|
|
|
import * as DataEvent from '../../utils/events/DataEvent';
|
2019-02-27 17:17:51 +01:00
|
|
|
import RightsManager, {
|
|
|
|
TASK_CREATE,
|
|
|
|
TASK_UPDATE,
|
|
|
|
OBJECT_POST,
|
|
|
|
OBJECT_SETTINGS
|
2019-11-16 18:10:05 +01:00
|
|
|
} from '../../utils/tools/RightsManager';
|
2018-12-18 18:52:59 +01:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const multer = require('multer');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const dateUtils = new DateUtils();
|
|
|
|
const rightsManager = new RightsManager();
|
2019-02-27 17:17:51 +01:00
|
|
|
const uploadPath =
|
2019-11-20 02:17:39 +01:00
|
|
|
'./public/assets/images/user/' +
|
2019-02-27 17:17:51 +01:00
|
|
|
dateUtils.getDate('year', new Date()) +
|
|
|
|
'/' +
|
|
|
|
dateUtils.getDate('month', new Date());
|
|
|
|
fs.ensureDir(uploadPath, () => {
|
|
|
|
//console.log(err) // => null
|
|
|
|
// dir has now been created, including the directory it is to be placed in
|
2018-12-18 18:52:59 +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]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var avatar_upload = multer({
|
|
|
|
storage: storage
|
2018-12-20 19:50:28 +01:00
|
|
|
}).array('avatar_upload');
|
2019-02-27 17:17:51 +01:00
|
|
|
var background_upload = multer({
|
|
|
|
storage: storage
|
2018-12-20 19:50:28 +01:00
|
|
|
}).array('background_upload');
|
2018-12-18 18:52:59 +01:00
|
|
|
//** SYNC POSTS */
|
2019-02-27 17:17:51 +01:00
|
|
|
router.post('/sync', (req, res) => {
|
|
|
|
let payload = req.body;
|
2019-11-16 18:10:05 +01:00
|
|
|
if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) {
|
|
|
|
fs.readJson('config/site-settings.json')
|
|
|
|
.then(obj => {
|
|
|
|
if (user.hande != payload.handle || user.email != payload.email) {
|
|
|
|
user.update({
|
|
|
|
handle: payload.handle,
|
|
|
|
email: payload.email
|
|
|
|
})
|
2019-02-27 17:17:51 +01:00
|
|
|
.then(() => {
|
2019-11-16 18:10:05 +01:00
|
|
|
//console.log('UPDATED');
|
2019-02-27 17:17:51 +01:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2019-11-16 18:10:05 +01:00
|
|
|
//console.log('ERR', err);
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
} else {
|
|
|
|
//console.log('USER NOT UPDATED');
|
|
|
|
}
|
|
|
|
obj.url = payload.url;
|
|
|
|
obj.title = payload.title;
|
|
|
|
obj.description = payload.descriptions;
|
|
|
|
obj.private = payload.private;
|
|
|
|
obj.theme = payload.theme;
|
|
|
|
obj.background = payload.background; //TODO: make this url relative
|
|
|
|
//mail stuff
|
|
|
|
obj.email.active = payload.mailSettings.activeProtocol;
|
|
|
|
obj.email.smtp.domain = payload.mailSettings.smtp.domain;
|
|
|
|
obj.email.smtp.email = payload.mailSettings.smtp.email;
|
|
|
|
obj.email.smtp.password = payload.mailSettings.smtp.password;
|
|
|
|
obj.email.mailgun.domain = payload.mailSettings.mailgun.domain;
|
|
|
|
obj.email.mailgun['api-key'] = payload.mailSettings.mailgun.key;
|
|
|
|
fs.writeJson('config/site-settings.json', obj)
|
|
|
|
.then(() => {
|
|
|
|
res.json({
|
|
|
|
message: DataEvent.SETTINGS_UPDATED
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-01-14 17:39:32 +01:00
|
|
|
|
2019-02-27 17:17:51 +01:00
|
|
|
router.get('/nav', function(req, res) {
|
|
|
|
fs.readJson('config/site-settings.json')
|
|
|
|
.then(obj => {
|
|
|
|
fs.writeJson('config/site-settings.json', obj)
|
|
|
|
.then(() => {
|
|
|
|
res.json(obj.menu);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
|
|
|
});
|
|
|
|
});
|
2019-01-12 20:07:08 +01:00
|
|
|
|
2019-02-27 17:17:51 +01:00
|
|
|
router.post('/nav-sync', (req, res) => {
|
|
|
|
let payload = req.body;
|
2019-11-16 18:10:05 +01:00
|
|
|
if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) {
|
|
|
|
fs.readJson('config/site-settings.json')
|
|
|
|
.then(obj => {
|
|
|
|
obj.menu = payload;
|
|
|
|
fs.writeJson('config/site-settings.json', obj)
|
|
|
|
.then(() => {
|
|
|
|
res.json({
|
|
|
|
message: DataEvent.SETTINGS_UPDATED
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-01-12 20:07:08 +01:00
|
|
|
|
2018-12-18 18:52:59 +01:00
|
|
|
/***
|
|
|
|
UPLOAD AVATAR
|
|
|
|
*/
|
2019-02-27 17:17:51 +01:00
|
|
|
router.post('/add-avatar', function(req, res) {
|
|
|
|
//console.log(req.body);
|
|
|
|
if (!req.session.user)
|
|
|
|
return res.json({
|
|
|
|
message: 'You need to be logged in, champ.'
|
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) {
|
|
|
|
avatar_upload(req, res, function(err) {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
message: err
|
|
|
|
});
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
var avatar = req.files[0].path;
|
|
|
|
user.update({
|
|
|
|
avatar: avatar.substr(7, avatar.length)
|
|
|
|
})
|
|
|
|
.then(updated => {
|
|
|
|
req.session.user = updated;
|
2019-02-27 17:17:51 +01:00
|
|
|
})
|
2019-11-16 18:10:05 +01:00
|
|
|
.catch(() => {
|
|
|
|
//console.log('ERR', err);
|
2019-02-27 17:17:51 +01:00
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
return res.json({
|
|
|
|
message: DataEvent.AVATAR_UPLOADED,
|
|
|
|
url: avatar.substr(7, avatar.length)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
2018-12-18 18:52:59 +01:00
|
|
|
});
|
|
|
|
/***
|
|
|
|
UPLOAD FEATURE BACKGROUND
|
|
|
|
*/
|
2019-02-27 17:17:51 +01:00
|
|
|
router.post('/add-feature-background', function(req, res) {
|
|
|
|
//console.log(req.body);
|
|
|
|
if (!req.session.user)
|
|
|
|
return res.json({
|
|
|
|
message: 'You need to be logged in, champ.'
|
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) {
|
|
|
|
background_upload(req, res, function(err) {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
message: err
|
|
|
|
});
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
var bgImage = req.files[0].path;
|
|
|
|
return res.json({
|
|
|
|
message: DataEvent.SITE_BACKGROUND_UPLOADED,
|
|
|
|
url: bgImage.substr(7, bgImage.length)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
|
|
|
});
|
|
|
|
}
|
2018-12-18 18:52:59 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
module.exports = router;
|