fipamo/brain/api/v1/backup.js

108 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-08-24 22:02:50 +02:00
import * as DataEvent from '../../../src/com/events/DataEvent';
import Auth from '../../data/Auth';
import Utils from '../../data/Utils';
const express = require('express');
const router = express.Router();
const multer = require('multer');
const auth = new Auth();
const utils = new Utils();
var backup_upload = multer().array('backup_upload');
var backup_restore = multer().any();
/***
CREATE BACK UP
*/
router.post('/create', (req, res) => {
auth.authCheck(req)
.then(() => {
utils
.createBackup()
.then(() => {
res.json({
type: DataEvent.API_BACKUP_CREATE,
message: "You're backed up. Hi fives"
});
})
.catch(err => {
res.json({
type: err.type,
message: err.message
});
});
})
.catch(err => {
res.json({
type: err.type,
message: err.message
});
});
});
/***
RETRIEVE BACKUP
*/
router.get('/download', (req, res) => {
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);
} else {
res.json({
type: DataEvent.REQUEST_LAME,
message: "You're not logged in, champ"
});
}
//Move to route?
});
/***
RESTORE BACKUP
*/
router.post('/restore', backup_upload, (req, res) => {
auth.authCheck(req)
.then(() => {
utils
.restoreBackup(req.files[0])
.then(() => {
res.json({
type: DataEvent.API_BACKUP_RESTORE,
message: 'Settings, files and pages restored. Nice work.'
});
})
.catch(err => {
res.json({
type: err.type,
message: 'Backup not restored. Uh oh.'
});
});
})
.catch(err => {
res.json({
type: err.type,
message: err.message
});
});
});
router.post('/init-restore', backup_restore, (req, res) => {
utils
.verifyBackup(req.files[0], req.body)
.then(response => {
res.json({
type: response.type,
message: response.message
});
})
.catch(err => {
res.json({
type: err.type,
message: err.message
});
});
});
module.exports = router;