From 1aea87faf0fcd6ed41a9e84cf21b1229063877c5 Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 20 Jun 2020 20:53:45 -0700 Subject: [PATCH] fixed upload dirs creation when not present, added error pages for routes --- brain/api/v1/pages.js | 6 +++- brain/api/v1/settings.js | 10 +++--- brain/routes/dash/index.js | 3 +- brain/routes/dash/nav.js | 5 ++- brain/routes/dash/pages.js | 45 +++++++++++++-------------- brain/routes/dash/settings.js | 58 +++++++++++++++++++---------------- 6 files changed, 65 insertions(+), 62 deletions(-) diff --git a/brain/api/v1/pages.js b/brain/api/v1/pages.js index 7fa9be1..a66c4cc 100644 --- a/brain/api/v1/pages.js +++ b/brain/api/v1/pages.js @@ -13,9 +13,13 @@ const settings = new Settings(); const _ = require('lodash'); const uploadPath = './public/assets/images/blog/' + moment().format('YYYY') + '/' + moment().format('MM'); + var storage = multer.diskStorage({ destination: function (req, file, cb) { - cb(null, uploadPath); + fs.ensureDir(uploadPath, () => { + // dir has now been created, including the directory it is to be placed in + cb(null, uploadPath); + }); }, filename: function (req, file, cb) { var splice = file.originalname.split(':'); diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index c4f6651..37aee4a 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -10,7 +10,6 @@ const multer = require('multer'); const fs = require('fs-extra'); const moment = require('moment'); const _ = require('lodash'); -//const folks = require('../../../site/folks.json'); const auth = new Auth(); const render = new Render(); const book = new Book(); @@ -18,12 +17,13 @@ const settings = new Settings(); 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); + fs.ensureDir(uploadPath, () => { + // dir has now been created, including the directory it is to be placed in + cb(null, uploadPath); + }); }, filename: function (req, file, cb) { var splice = file.originalname.split(':'); diff --git a/brain/routes/dash/index.js b/brain/routes/dash/index.js index c64a0b9..1b582c8 100644 --- a/brain/routes/dash/index.js +++ b/brain/routes/dash/index.js @@ -3,7 +3,6 @@ import Settings, { SETTINGS_FILE } from '../../data/Settings'; const express = require('express'); const moment = require('moment'); const router = express.Router(); -//const config = require('../../../site/settings.json'); const book = new Book(); const settings = new Settings(); const indexLimit = 5; @@ -47,7 +46,7 @@ router.get('/', function (req, res) { let setupData = { title: 'Fipamo Set up' }; res.render('init', setupData); } else { - //render error page + res.render('error', { error: err }); } }); }); diff --git a/brain/routes/dash/nav.js b/brain/routes/dash/nav.js index 8a90950..56bbbc2 100644 --- a/brain/routes/dash/nav.js +++ b/brain/routes/dash/nav.js @@ -66,12 +66,11 @@ router.get('/', function (req, res) { }); }) .catch(err => { - //console.log('ERROR', err); - //render error pages + res.render('error', { error: err }); }); }) .catch(err => { - console.log('ERROR', err); + res.render('error', { error: err }); }); } else { res.redirect('/@/dashboard'); diff --git a/brain/routes/dash/pages.js b/brain/routes/dash/pages.js index 557a33d..e2fe339 100644 --- a/brain/routes/dash/pages.js +++ b/brain/routes/dash/pages.js @@ -78,8 +78,8 @@ router.get('/list/:filter?/:page?', function (req, res) { .then(() => { //console.log(value); }) - .catch(() => { - //next(err); + .catch(err => { + res.render('error', { error: err }); }); } else { res.redirect('/@/dashboard'); @@ -92,27 +92,24 @@ router.get('/list/:filter?/:page?', function (req, res) { router.get('/add/new', function (req, res) { if (req.session.user) { //need to grab a few copy of settings for the lastest index - fs.ensureDir( - './public/assets/images/blog/' + moment().format('YYYY') + '/' + moment().format('MM'), - () => { - fs.readJSON('site/settings.json') - .then(config => { - res.render('page-edit', { - id: config.library_stats.current_index, - uuid: uuidv4(), - title: 'Add New Page', - user_status: true, - welcome: 'Add New Page', - date: moment(Date.now()).format('YYYY MMM DD'), - page: [], - rawDate: moment(Date.now()).format(), - status: ['false', 'false', 'false'], - edit: false - }); - }) - .catch(err => {}); //render error page - } - ); + fs.readJSON('site/settings.jso') + .then(config => { + res.render('page-edit', { + id: config.library_stats.current_index, + uuid: uuidv4(), + title: 'Add New Page', + user_status: true, + welcome: 'Add New Page', + date: moment(Date.now()).format('YYYY MMM DD'), + page: [], + rawDate: moment(Date.now()).format(), + status: ['false', 'false', 'false'], + edit: false + }); + }) + .catch(err => { + res.render('error', { error: err }); + }); } else { res.redirect('/@/dashboard'); } @@ -145,7 +142,7 @@ router.get('/edit/:id', function (req, res) { }); }) .catch(err => { - res.json({ type: err }); + res.render('error', { error: err }); }); } else { res.redirect('/@/dashboard'); diff --git a/brain/routes/dash/settings.js b/brain/routes/dash/settings.js index fb21303..e4fe38c 100644 --- a/brain/routes/dash/settings.js +++ b/brain/routes/dash/settings.js @@ -15,35 +15,39 @@ router.get('/', function (req, res) { config = obj; }) .catch(err => { - console.log('SETTINGS ERROR', err); + res.render('error', { 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; - }); + 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: config, - member: memberInfo[0] - }); - } else { - res.redirect('/@/dashboard'); - } - }); + res.render('settings', { + title: 'Dashboard | Settings', + welcome: 'Your Settings', + status: true, + themes: themes, + settings: config, + member: memberInfo[0] + }); + } else { + res.redirect('/@/dashboard'); + } + }) + .catch(err => { + res.render('error', { error: err }); + }); }); module.exports = router;