set up settings api, site background and avi image uploading cleaned up and working

This commit is contained in:
Ro 2019-11-28 21:50:32 -08:00
parent 4175bb631e
commit ab19d8027e
3 changed files with 192 additions and 7 deletions

181
brain/api/v1/settings.js Normal file
View file

@ -0,0 +1,181 @@
import * as DataEvent from '../../../src/com/events/DataEvent';
import RightsManager, {
TASK_CREATE,
TASK_UPDATE,
OBJECT_POST,
OBJECT_SETTINGS
} from '../../../src/com/utils/RightsManager';
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 rightsManager = new RightsManager();
const settings = require('../../../site/settings.json');
const folks = require('../../../site/folks.json');
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({
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) => {
let payload = req.body;
if (req.handle.user) {
let user = req.session.user;
fs.readJson('site/settings.json')
.then(obj => {
if (user.hande != payload.handle || user.email != payload.email) {
user.update({
handle: payload.handle,
email: payload.email
})
.then(() => {
//console.log('UPDATED');
})
.catch(() => {
//console.log('ERR', err);
});
} else {
//console.log('USER NOT UPDATED');
}
obj.url = payload.url;
obj.title = payload.title;
obj.description = payload.descriptions;
obj.private = payload.private;
obj.theme = payload.theme;
obj.background = payload.background; //TODO: make this url relative
//mail stuff
obj.email.active = payload.mailSettings.activeProtocol;
obj.email.smtp.domain = payload.mailSettings.smtp.domain;
obj.email.smtp.email = payload.mailSettings.smtp.email;
obj.email.smtp.password = payload.mailSettings.smtp.password;
obj.email.mailgun.domain = payload.mailSettings.mailgun.domain;
obj.email.mailgun['api-key'] = payload.mailSettings.mailgun.key;
fs.writeJson('site/settings.json', obj)
.then(() => {
res.json({
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
});
})
.catch(() => {
//console.error(err);
});
})
.catch(() => {
//console.error(err);
});
}
});
router.get('/nav', function(req, res) {
fs.readJson('config/site-settings.json')
.then(obj => {
fs.writeJson('config/site-settings.json', obj)
.then(() => {
res.json(obj.menu);
})
.catch(() => {
//console.error(err);
});
})
.catch(() => {
//console.error(err);
});
});
router.post('/nav-sync', (req, res) => {
let payload = req.body;
if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) {
fs.readJson('config/site-settings.json')
.then(obj => {
obj.menu = payload;
fs.writeJson('config/site-settings.json', obj)
.then(() => {
res.json({
message: DataEvent.SETTINGS_UPDATED
});
})
.catch(() => {
//console.error(err);
});
})
.catch(() => {
//console.error(err);
});
} else {
res.json({
message: "Nah. You can't do that. Talk to the admin, sport."
});
}
});
/***
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);
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;

View file

@ -48,13 +48,13 @@ var settings = require('./routes/dash/settings');
//var navDashboard = require('./routes/dash/nav');
//api
var pages = require('./api/v1/pages');
//var settings = require('./api/content/settings');
var setting = require('./api/v1/settings');
//var mailer = require('./api/mail/mailer');
var auth = require('./api/v1/auth');
// API PATHS
app.use('/api/v1/page', pages);
//app.use('/api/settings', settings);
app.use('/api/v1/settings', setting);
//app.use('/api/mail', mailer);
app.use('/api/v1/auth', auth);
// PAGES

View file

@ -2,13 +2,15 @@ import SettingsActions from '../actions/SettingsActions';
import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_FORM } from '../../../src/com/utils/DataUtils';
import * as DataEvent from '../../../src/com/events/DataEvent';
import Mailer from '../actions/Mailer';
import Notifications from '../ui/Notifications';
const utils = new DataUtils();
const notify = new Notifications();
export default class SettingsIndex {
//--------------------------
// constructor
//--------------------------
constructor() {
this.start();
this.dataUtils = new DataUtils();
this.mailer = new Mailer();
}
//--------------------------
@ -119,8 +121,8 @@ export default class SettingsIndex {
let url = '';
let eventType = '';
type == 'avatar-upload'
? (url = '/api/settings/add-avatar')
: (url = '/api/settings/add-feature-background');
? (url = '/api/v1/settings/add-avatar')
: (url = '/api/v1/settings/add-feature-background');
var imageData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
@ -132,13 +134,15 @@ export default class SettingsIndex {
? imageData.append('avatar_upload', file, file.name)
: imageData.append('background_upload', file, file.name);
}
this.dataUtils
utils
.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData)
.then(response => {
let r = JSON.parse(response.request['response']);
if (r.message == DataEvent.AVATAR_UPLOADED) {
if (r.type == DataEvent.AVATAR_UPLOADED) {
notify.alert(r.message, true);
document.getElementById('avatar').src = r.url;
} else {
notify.alert(r.message, true);
document.getElementById('background').src = r.url;
}
})