2019-03-13 19:10:13 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-15 16:18:33 +01:00
|
|
|
const mime = require('mime');
|
2019-03-13 19:10:13 +01:00
|
|
|
const YAML = require('js-yaml');
|
|
|
|
const moment = require('moment');
|
|
|
|
const multer = require('multer');
|
2020-04-14 22:29:11 +02:00
|
|
|
const Controller = require('../controller');
|
|
|
|
const { ADMIN } = require('../../permissions');
|
|
|
|
const extractUser = require('../../extract-user');
|
|
|
|
const { handleErrors } = require('./util');
|
|
|
|
|
2019-03-13 19:10:13 +01:00
|
|
|
const upload = multer({ limits: { fileSize: 5242880 } });
|
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
class StateController extends Controller {
|
2019-03-13 19:10:13 +01:00
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/state.js');
|
2019-03-13 19:10:13 +01:00
|
|
|
this.fileupload('/import', upload.single('file'), this.import, ADMIN);
|
|
|
|
this.get('/export', this.export, ADMIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
async import(req, res) {
|
|
|
|
const userName = extractUser(req);
|
|
|
|
const { drop } = req.query;
|
|
|
|
|
2019-03-15 16:18:33 +01:00
|
|
|
try {
|
|
|
|
let data;
|
|
|
|
if (req.file) {
|
2019-04-26 10:36:17 +02:00
|
|
|
if (mime.getType(req.file.originalname) === 'text/yaml') {
|
2019-03-15 16:18:33 +01:00
|
|
|
data = YAML.safeLoad(req.file.buffer);
|
|
|
|
} else {
|
|
|
|
data = JSON.parse(req.file.buffer);
|
|
|
|
}
|
2019-03-13 19:10:13 +01:00
|
|
|
} else {
|
2019-03-15 16:18:33 +01:00
|
|
|
data = req.body;
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.config.stateService.import({
|
|
|
|
data,
|
|
|
|
userName,
|
|
|
|
dropBeforeImport: drop,
|
|
|
|
});
|
|
|
|
res.sendStatus(202);
|
|
|
|
} catch (err) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, err);
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async export(req, res) {
|
|
|
|
const { format } = req.query;
|
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
const downloadFile = Boolean(req.query.download);
|
|
|
|
let includeStrategies = Boolean(req.query.strategies);
|
|
|
|
let includeFeatureToggles = Boolean(req.query.featureToggles);
|
2019-03-13 19:10:13 +01:00
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
// if neither is passed as query argument, export both
|
|
|
|
if (!includeStrategies && !includeFeatureToggles) {
|
|
|
|
includeStrategies = true;
|
|
|
|
includeFeatureToggles = true;
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = await this.config.stateService.export({
|
2019-03-14 17:56:02 +01:00
|
|
|
includeStrategies,
|
|
|
|
includeFeatureToggles,
|
2019-03-13 19:10:13 +01:00
|
|
|
});
|
|
|
|
const timestamp = moment().format('YYYY-MM-DD_HH-mm-ss');
|
|
|
|
if (format === 'yaml') {
|
2019-03-14 17:56:02 +01:00
|
|
|
if (downloadFile) {
|
2019-03-13 19:10:13 +01:00
|
|
|
res.attachment(`export-${timestamp}.yml`);
|
|
|
|
}
|
2020-07-30 23:57:13 +02:00
|
|
|
res.type('yaml').send(
|
|
|
|
YAML.safeDump(data, { skipInvalid: true }),
|
|
|
|
);
|
2019-03-13 19:10:13 +01:00
|
|
|
} else {
|
2019-03-14 17:56:02 +01:00
|
|
|
if (downloadFile) {
|
2019-03-13 19:10:13 +01:00
|
|
|
res.attachment(`export-${timestamp}.json`);
|
|
|
|
}
|
|
|
|
res.json(data);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, err);
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
module.exports = StateController;
|