|
|
|
import * as DataEvent from '../../../src/com/events/DataEvent';
|
|
|
|
import Auth from '../../data/Auth';
|
|
|
|
import Render from '../../data/Render';
|
|
|
|
import SettingsData from '../../data/Settings';
|
|
|
|
import Navigation from '../../data/Navigation';
|
|
|
|
import Book from '../../data/Book';
|
|
|
|
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');
|
|
|
|
const auth = new Auth();
|
|
|
|
const render = new Render();
|
|
|
|
const book = new Book();
|
|
|
|
const settingsData = new SettingsData();
|
|
|
|
const nav = new Navigation();
|
|
|
|
const uploadPath =
|
|
|
|
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
|
|
|
|
fs.ensureDir(uploadPath, () => {
|
|
|
|
// 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 avatar_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
}).array('avatar_upload');
|
|
|
|
var background_upload = multer({
|
|
|
|
storage: storage
|
|
|
|
}).array('background_upload');
|
|
|
|
//** SYNC POSTS */
|
|
|
|
router.post('/sync', (req, res) => {
|
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
settingsData
|
|
|
|
.sync(req, res)
|
|
|
|
.then(() => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.SETTINGS_UPDATED,
|
|
|
|
message: 'Settings Saved'
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: "You're not logged in, champ"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: err.type,
|
|
|
|
message: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/nav-sync', (req, res) => {
|
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
nav.sync(req)
|
|
|
|
.then(response => {
|
|
|
|
res.json({
|
|
|
|
type: response.type,
|
|
|
|
message: response.message
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: err.type,
|
|
|
|
message: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/publish-pages', (req, res) => {
|
|
|
|
auth.authCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
book.getPage()
|
|
|
|
.then(pages => {
|
|
|
|
render
|
|
|
|
.publishAll(pages, 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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.PAGES_NOT_RENDERED,
|
|
|
|
message: 'Uh oh. Pages not rendered, sport',
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({
|
|
|
|
type: err.type,
|
|
|
|
message: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/***
|
|
|
|
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);
|
|
|
|
user.avi = '/' + image;
|
|
|
|
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;
|