fipamo/brain/routes/dash/settings.js

78 lines
1.7 KiB
JavaScript

const express = require('express');
const router = express.Router();
const FileHound = require('filehound');
const fs = require('fs-extra');
var settings = [];
//--------------------------
// SETTINGS
//--------------------------
router.get('/', function(req, res) {
fs.readJson('site/settings.json')
.then(obj => {
settings = obj;
})
.catch(() => {
//console.error(err)
});
loadThemes().then(themes => {
if (req.session.user) {
let memberInfo = [];
let user = req.session.user;
memberInfo.push({
handle: user.handle,
email: user.email,
avi: user.avi
});
themes.sort(function(a, b) {
var textA = a.theme.name.toUpperCase();
var textB = b.theme.name.toUpperCase();
return textA < textB ? -1 : textA > textB ? 1 : 0;
});
res.render('settings', {
title: 'Dashboard | Settings',
welcome: 'Your Settings',
status: true,
themes: themes,
settings: settings,
member: memberInfo[0]
});
} else {
res.redirect('/@/dashboard');
}
});
});
module.exports = router;
function loadThemes() {
return new Promise((resolve, reject) => {
FileHound.create()
.paths('content/themes')
.ext('json')
.find()
.then(files => {
let themes = [];
for (let index = 0; index < files.length; index++) {
fs.readJSON(files[index], (err, theme) => {
if (theme.name == settings.global.theme) {
themes.push({
theme: theme,
current: 'true'
});
} else {
themes.push({
theme: theme,
current: 'false'
});
}
});
}
setTimeout(() => {
resolve(themes);
}, 200);
})
.catch(err => {
reject(err);
});
});
}