2019-11-29 06:50:32 +01:00
|
|
|
import * as DataEvent from '../../../src/com/events/DataEvent';
|
2020-05-05 22:44:45 +02:00
|
|
|
import Auth from '../../data/Auth';
|
2020-05-06 23:43:53 +02:00
|
|
|
import Book from '../../data/Book';
|
2019-11-29 06:50:32 +01:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const multer = require('multer');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const moment = require('moment');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const settings = require('../../../site/settings.json');
|
|
|
|
const folks = require('../../../site/folks.json');
|
2020-05-05 22:44:45 +02:00
|
|
|
const auth = new Auth();
|
2019-11-29 06:50:32 +01:00
|
|
|
const uploadPath =
|
|
|
|
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
|
|
|
|
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({
|
2020-05-05 21:31:32 +02:00
|
|
|
destination: function (req, file, cb) {
|
2019-11-29 06:50:32 +01:00
|
|
|
cb(null, uploadPath);
|
|
|
|
},
|
2020-05-05 21:31:32 +02:00
|
|
|
filename: function (req, file, cb) {
|
2019-11-29 06:50:32 +01:00
|
|
|
var splice = file.originalname.split(':');
|
|
|
|
cb(null, splice[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var avatar_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
}).array('avatar_upload');
|
|
|
|
var background_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
}).array('background_upload');
|
|
|
|
//** SYNC POSTS */
|
|
|
|
router.post('/sync', (req, res) => {
|
|
|
|
let payload = req.body;
|
2019-11-29 20:12:15 +01:00
|
|
|
if (req.session.user) {
|
2019-11-29 06:50:32 +01:00
|
|
|
let user = req.session.user;
|
2019-11-29 20:12:15 +01:00
|
|
|
let found = _.find(folks, { id: user.id });
|
|
|
|
let needToUpdate = false;
|
|
|
|
if (found) {
|
|
|
|
let index = found.id - 1;
|
|
|
|
if (
|
|
|
|
folks[index].handle != payload.member.handle ||
|
|
|
|
folks[index].email != payload.member.email
|
|
|
|
) {
|
|
|
|
folks[index].handle = payload.member.handle;
|
|
|
|
folks[index].email = payload.member.email;
|
|
|
|
fs.writeJson('site/folks.json', folks);
|
|
|
|
} else {
|
|
|
|
//no need to save
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: "You're not logged in, champ"
|
2019-11-29 06:50:32 +01:00
|
|
|
});
|
2019-11-29 20:12:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEqual(settings.global, payload.global)) {
|
|
|
|
let bg = payload.global.background;
|
|
|
|
payload.global.background = bg.substr(21, bg.length);
|
|
|
|
settings.global = payload.global;
|
|
|
|
needToUpdate = true;
|
|
|
|
} else {
|
|
|
|
//no need to save
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEqual(settings.email, payload.email)) {
|
|
|
|
settings.email = payload.email;
|
|
|
|
needToUpdate = true;
|
|
|
|
} else {
|
|
|
|
//no need to save
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needToUpdate) {
|
|
|
|
fs.writeJson('site/settings.json', settings)
|
|
|
|
.then(() => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.SETTINGS_UPDATED,
|
|
|
|
message: 'Settings Saved'
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.error(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//no need to update
|
|
|
|
}
|
2019-11-29 06:50:32 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/nav-sync', (req, res) => {
|
|
|
|
let payload = req.body;
|
2019-11-29 20:12:15 +01:00
|
|
|
if (req.session.user) {
|
2019-11-29 22:43:55 +01:00
|
|
|
settings.menu = payload;
|
|
|
|
fs.writeJson('site/settings.json', settings)
|
|
|
|
.then(() => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.SETTINGS_UPDATED,
|
|
|
|
message: 'Menu order saved, champ'
|
|
|
|
});
|
2019-11-29 06:50:32 +01:00
|
|
|
})
|
2019-11-29 22:43:55 +01:00
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: err
|
|
|
|
});
|
2019-11-29 06:50:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-05 21:31:32 +02:00
|
|
|
router.post('/publish-pages', (req, res) => {
|
2020-05-05 22:44:45 +02:00
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
2020-05-06 23:43:53 +02:00
|
|
|
new Book()
|
|
|
|
.publish(settings.global.theme)
|
|
|
|
.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
|
|
|
|
});
|
|
|
|
});
|
2020-05-05 22:44:45 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: err.type,
|
|
|
|
message: err.message
|
|
|
|
});
|
2020-05-05 21:31:32 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-29 06:50:32 +01:00
|
|
|
/***
|
|
|
|
UPLOAD AVATAR
|
|
|
|
*/
|
|
|
|
|
|
|
|
router.post('/add-avatar', avatar_upload, (req, res) => {
|
|
|
|
if (req.session.user) {
|
|
|
|
let user = req.session.user;
|
|
|
|
let found = _.find(folks, { handle: user.handle });
|
|
|
|
if (found) {
|
|
|
|
var index = found.id - 1;
|
|
|
|
var path = req.files[0].path;
|
|
|
|
var image = path.substr(7, path.length);
|
|
|
|
folks[index].avi = '/' + image;
|
|
|
|
fs.writeJson('site/folks.json', folks);
|
2020-05-06 23:43:53 +02:00
|
|
|
user.avi = '/' + image;
|
2019-11-29 06:50:32 +01:00
|
|
|
res.json({
|
|
|
|
type: DataEvent.AVATAR_UPLOADED,
|
|
|
|
message: 'Changed avi. You look great.',
|
|
|
|
url: '/' + image
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: "You're not logged in, champ"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/***
|
|
|
|
UPLOAD FEATURE BACKGROUND
|
|
|
|
*/
|
|
|
|
|
|
|
|
router.post('/add-feature-background', background_upload, (req, res) => {
|
|
|
|
if (req.session.user) {
|
|
|
|
var path = req.files[0].path;
|
|
|
|
var image = path.substr(7, path.length);
|
|
|
|
settings.background = '/' + image;
|
|
|
|
fs.writeJson('site/settings.json', settings);
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.SITE_BACKGROUND_UPLOADED,
|
|
|
|
message: 'Background Uploaded',
|
|
|
|
url: '/' + image
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: "You're not logged in, champ"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = router;
|