From 1fa21322fb22db493e420feedf5f3ffcf5658133 Mon Sep 17 00:00:00 2001 From: Ro Date: Fri, 14 Aug 2020 12:57:16 -0700 Subject: [PATCH] cleaned up backups on front end and backend --- brain/api/v1/settings.js | 28 ++++++++++++---------------- brain/views/settings.pug | 10 +++++----- src/com/controllers/SettingsIndex.js | 8 ++++++-- src/com/events/DataEvent.js | 1 + src/libraries/FipamoAdminAPI.js | 20 ++++++++++++-------- src/styles/main/_settings.styl | 6 ++++++ 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 825d517..4e98eb0 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -5,7 +5,6 @@ import Settings, { SETTINGS_FILE, SETTINGS_FOLKS } from '../../data/Settings'; import Navigation from '../../data/Navigation'; import Book from '../../data/Book'; import Utils from '../../data/Utils'; -import { util } from 'prettier'; const express = require('express'); const router = express.Router(); const multer = require('multer'); @@ -54,6 +53,7 @@ router.post('/sync', (req, res) => { .catch(err => { res.json({ type: DataEvent.REQUEST_LAME, + error: err.message, message: "Uh oh. Settings didn't take, sport" }); }); @@ -238,23 +238,19 @@ router.post('/create-backup', (req, res) => { }); router.get('/download-backup', (req, res) => { - var filePath = 'content/backup.zip'; // Or format the path using the `id` rest param - var fileName = 'backup.zip'; // The default name the browser will use + if (req.session.user) { + var filePath = 'content/backup.zip'; // Or format the path using the `id` rest param + var fileName = 'backup.zip'; // The default name the browser will use - res.download(filePath, fileName); - //Make secure - /* - auth.authCheck(req) - .then(() => { - - }) - .catch(err => { - res.json({ - type: err.type, - message: err.message - }); + res.download(filePath, fileName); + } else { + res.json({ + type: DataEvent.REQUEST_LAME, + message: "You're not logged in, champ" }); - */ + } + + //Move to route? }); module.exports = router; diff --git a/brain/views/settings.pug b/brain/views/settings.pug index 7b49fb9..0d8286c 100644 --- a/brain/views/settings.pug +++ b/brain/views/settings.pug @@ -41,19 +41,19 @@ block main-content =settings.global.descriptions #member-utils.columns #util-1.column - label MEMBER UTILS + label BACKUP TOOLS br button#create-backup CREATE BACKUP br -if(settings.global.last_backup != null) - br - | The last back up was - a(href='/api/v1/settings/download-backup')= last_backup + .backup-meta + | The last back up was created + a(href='/api/v1/settings/download-backup')= last_backup -else br span No back ups. Frowny face. #util-2.column - SOMETHING + label MAINTENANCE #option-settings.columns #theme-settings.column label THEMES diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index 74842a3..a2afefd 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -81,6 +81,7 @@ export default class SettingsIndex { document .getElementById('create-backup') .addEventListener('click', e => this.handleBackup(e)); + document.getElementById('get-backup').addEventListener('click', e => this.handleBackup(e)); } //-------------------------- // event handlers @@ -176,9 +177,12 @@ export default class SettingsIndex { handleBackup(e) { e.preventDefault(); e.stopPropagation(); - let task = { task: 'create_backup' }; + let task; + e.target.id === 'create-backup' + ? (task = { task: 'create_backup' }) + : (task = { task: 'get_backup' }); admin - .createBackup(task) + .handleBackup(task) .then(r => { notify.alert(r.message, true); }) diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index 1b86762..2fe83f9 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -28,6 +28,7 @@ export const API_PAGE_CREATE = 'writingNewEntry'; export const API_PAGE_DELETE = 'erasingPage'; export const API_SETTINGS_WRITE = 'savingSettings'; export const API_BACKUP_CREATE = 'createBackup'; +export const API_BACKUP_DOWNLOAD = 'downloadBackup'; export const API_IMAGES_UPLOAD = 'uploadProfileImages'; export const API_RENDER_PAGES = 'renderPages'; export const API_INIT = 'blogInit'; diff --git a/src/libraries/FipamoAdminAPI.js b/src/libraries/FipamoAdminAPI.js index 80f33a4..1745e7c 100644 --- a/src/libraries/FipamoAdminAPI.js +++ b/src/libraries/FipamoAdminAPI.js @@ -190,15 +190,19 @@ export default class APIUtils { }); }); } - createBackup(data) { + handleBackup(data) { return new Promise((resolve, reject) => { - this._request( - API_CREATE_BACKUP, - DataEvent.API_BACKUP_CREATE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ) + var url, event, method; + if (data.task === 'create_backup') { + url = API_CREATE_BACKUP; + event = DataEvent.API_BACKUP_CREATE; + method = REQUEST_TYPE_POST; + } else { + url = API_DOWNLOAD_BACKUP; + event = DataEvent.API_BACKUP_DOWNLOAD; + method = REQUEST_TYPE_GET; + } + this._request(url, event, method, CONTENT_TYPE_JSON, data) .then(result => { resolve(result); }) diff --git a/src/styles/main/_settings.styl b/src/styles/main/_settings.styl index cefd5e4..a862ad7 100644 --- a/src/styles/main/_settings.styl +++ b/src/styles/main/_settings.styl @@ -74,6 +74,12 @@ margin 0 5px 10px 0 height 30px padding 10px + .backup-meta + background $primary - 60% + color $white + padding 8px + border-radius 3px + margin 5px 0 0 0 width 100% margin 20px auto