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-19 22:51:04 +02:00
|
|
|
import Render from '../../data/Render';
|
2020-06-21 03:13:23 +02:00
|
|
|
import Settings, { SETTINGS_FILE, SETTINGS_FOLKS } from '../../data/Settings';
|
2020-05-23 22:55:25 +02:00
|
|
|
import Navigation from '../../data/Navigation';
|
2020-05-21 03:14:19 +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');
|
2020-05-05 22:44:45 +02:00
|
|
|
const auth = new Auth();
|
2020-05-21 03:14:19 +02:00
|
|
|
const render = new Render();
|
|
|
|
const book = new Book();
|
2020-06-21 03:13:23 +02:00
|
|
|
const settings = new Settings();
|
2020-05-23 22:55:25 +02:00
|
|
|
const nav = new Navigation();
|
2019-11-29 06:50:32 +01:00
|
|
|
const uploadPath =
|
|
|
|
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
|
2020-06-21 05:53:45 +02:00
|
|
|
|
2019-11-29 06:50:32 +01:00
|
|
|
var storage = multer.diskStorage({
|
2020-05-05 21:31:32 +02:00
|
|
|
destination: function (req, file, cb) {
|
2020-06-21 05:53:45 +02:00
|
|
|
fs.ensureDir(uploadPath, () => {
|
|
|
|
// dir has now been created, including the directory it is to be placed in
|
|
|
|
cb(null, uploadPath);
|
|
|
|
});
|
2019-11-29 06:50:32 +01:00
|
|
|
},
|
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) => {
|
2020-05-23 21:40:01 +02:00
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
2020-06-21 03:13:23 +02:00
|
|
|
settings
|
2020-05-23 21:40:01 +02:00
|
|
|
.sync(req, res)
|
2019-11-29 20:12:15 +01:00
|
|
|
.then(() => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.SETTINGS_UPDATED,
|
|
|
|
message: 'Settings Saved'
|
|
|
|
});
|
|
|
|
})
|
2020-07-07 01:12:32 +02:00
|
|
|
.catch(err => {
|
2020-05-23 21:40:01 +02:00
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
2020-08-14 21:57:16 +02:00
|
|
|
error: err.message,
|
2020-07-07 01:12:32 +02:00
|
|
|
message: "Uh oh. Settings didn't take, sport"
|
2020-05-23 21:40:01 +02:00
|
|
|
});
|
2019-11-29 20:12:15 +01:00
|
|
|
});
|
2020-05-23 21:40:01 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: err.type,
|
|
|
|
message: err.message
|
|
|
|
});
|
|
|
|
});
|
2019-11-29 06:50:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/nav-sync', (req, res) => {
|
2020-05-23 22:55:25 +02:00
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
2020-06-28 01:21:42 +02:00
|
|
|
// find removoed menu item page and set menu to false
|
|
|
|
book.getPage(req.body.remove).then(page => {
|
|
|
|
let body = page.metadata;
|
|
|
|
body.content = page.content;
|
|
|
|
body.menu = false;
|
|
|
|
book.editPage(body, body.uuid, DataEvent.API_PAGE_WRITE, req.session.user);
|
|
|
|
});
|
2020-06-22 04:01:22 +02:00
|
|
|
nav.sync(req.body)
|
2020-05-23 22:55:25 +02:00
|
|
|
.then(response => {
|
|
|
|
res.json({
|
|
|
|
type: response.type,
|
|
|
|
message: response.message
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: err
|
|
|
|
});
|
2019-11-29 22:43:55 +01:00
|
|
|
});
|
2020-05-23 22:55:25 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: err.type,
|
|
|
|
message: err.message
|
2019-11-29 06:50:32 +01:00
|
|
|
});
|
2020-05-23 22:55:25 +02:00
|
|
|
});
|
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-06-21 03:13:23 +02:00
|
|
|
getBookData()
|
|
|
|
.then(result => {
|
2020-05-21 03:14:19 +02:00
|
|
|
render
|
2020-06-26 05:49:12 +02:00
|
|
|
.publishAll(
|
|
|
|
result.pages,
|
|
|
|
result.settings.global.theme,
|
|
|
|
req.session.user.handle
|
|
|
|
)
|
2020-05-21 03:14:19 +02:00
|
|
|
.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-06 23:43:53 +02:00
|
|
|
})
|
|
|
|
.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;
|
2020-06-21 03:13:23 +02:00
|
|
|
settings
|
|
|
|
.load(SETTINGS_FOLKS)
|
|
|
|
.then(folks => {
|
|
|
|
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);
|
|
|
|
user.avi = '/' + image;
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.AVATAR_UPLOADED,
|
|
|
|
message: 'Changed avi. You look great.',
|
|
|
|
url: '/' + image
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: 'Members Not found'
|
|
|
|
});
|
2019-11-29 06:50:32 +01:00
|
|
|
});
|
|
|
|
} 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) {
|
2020-06-21 03:13:23 +02:00
|
|
|
settings
|
|
|
|
.load(SETTINGS_FILE)
|
|
|
|
.then(settings => {
|
|
|
|
var path = req.files[0].path;
|
|
|
|
var image = path.substr(7, path.length);
|
2020-06-26 23:05:20 +02:00
|
|
|
settings.global.background = '/' + image;
|
2020-06-21 03:13:23 +02:00
|
|
|
fs.writeJson('site/settings.json', settings);
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.SITE_BACKGROUND_UPLOADED,
|
|
|
|
message: 'Background Uploaded',
|
|
|
|
url: '/' + image
|
|
|
|
});
|
|
|
|
})
|
2020-06-28 01:21:42 +02:00
|
|
|
.catch(() => {
|
|
|
|
//console.log('ERROR', err);
|
2020-06-21 03:13:23 +02:00
|
|
|
});
|
2019-11-29 06:50:32 +01:00
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: "You're not logged in, champ"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-08-13 21:53:51 +02:00
|
|
|
|
2019-11-29 06:50:32 +01:00
|
|
|
module.exports = router;
|
2020-06-21 03:13:23 +02:00
|
|
|
|
|
|
|
function getBookData() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let getSettings = settings.load(SETTINGS_FILE);
|
|
|
|
let getBook = book.getPage();
|
|
|
|
Promise.all([getSettings, getBook])
|
|
|
|
.then(result => {
|
|
|
|
const [settings, pages] = result;
|
|
|
|
let data = { settings: settings, pages: pages };
|
|
|
|
resolve(data);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|