2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const db = require('../../data/db');
|
|
|
|
const commands = require('../schema').commands;
|
|
|
|
const ghostVersion = require('../../lib/ghost-version');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const logging = require('../../../shared/logging');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-08-11 16:01:16 +03:00
|
|
|
const security = require('@tryghost/security');
|
2020-04-29 18:44:27 +03:00
|
|
|
const models = require('../../models');
|
2021-01-27 09:39:25 +03:00
|
|
|
const EXCLUDED_TABLES = ['sessions', 'mobiledoc_revisions', 'email_batches', 'email_recipients'];
|
|
|
|
const EXCLUDED_SETTING_KEYS = [
|
|
|
|
'stripe_connect_publishable_key',
|
|
|
|
'stripe_connect_secret_key',
|
|
|
|
'stripe_connect_account_id',
|
|
|
|
'stripe_secret_key',
|
|
|
|
'stripe_publishable_key',
|
|
|
|
'members_stripe_webhook_id',
|
|
|
|
'members_stripe_webhook_secret'
|
|
|
|
];
|
2020-04-29 18:44:27 +03:00
|
|
|
|
|
|
|
const modelOptions = {context: {internal: true}};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const exportFileName = function exportFileName(options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
|
|
|
|
let title = '';
|
2014-07-19 16:20:16 +04:00
|
|
|
|
2018-01-11 18:03:21 +03:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// custom filename
|
|
|
|
if (options.filename) {
|
|
|
|
return Promise.resolve(options.filename + '.json');
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:31:54 +03:00
|
|
|
return models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting'))).then(function (result) {
|
2014-07-19 16:20:16 +04:00
|
|
|
if (result) {
|
2017-12-14 15:26:48 +03:00
|
|
|
title = security.string.safe(result.get('value')) + '.';
|
2014-07-19 16:20:16 +04:00
|
|
|
}
|
2017-03-08 13:26:57 +03:00
|
|
|
|
2014-07-19 16:20:16 +04:00
|
|
|
return title + 'ghost.' + datetime + '.json';
|
|
|
|
}).catch(function (err) {
|
2020-05-22 21:22:20 +03:00
|
|
|
logging.error(new errors.GhostError({err: err}));
|
2014-07-19 16:20:16 +04:00
|
|
|
return 'ghost.' + datetime + '.json';
|
|
|
|
});
|
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getVersionAndTables = function getVersionAndTables(options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const props = {
|
2017-01-26 15:12:00 +03:00
|
|
|
version: ghostVersion.full,
|
2017-08-01 16:27:13 +03:00
|
|
|
tables: commands.getTables(options.transacting)
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props);
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const exportTable = function exportTable(tableName, options) {
|
2018-07-30 18:21:52 +03:00
|
|
|
if (EXCLUDED_TABLES.indexOf(tableName) < 0 ||
|
|
|
|
(options.include && _.isArray(options.include) && options.include.indexOf(tableName) !== -1)) {
|
2018-08-06 18:18:59 +03:00
|
|
|
const query = (options.transacting || db.knex)(tableName);
|
|
|
|
|
|
|
|
return query.select();
|
2016-03-12 21:54:06 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-27 09:39:25 +03:00
|
|
|
const getSettingsTableData = function getSettingsTableData(settingsData) {
|
|
|
|
return settingsData && settingsData.filter((setting) => {
|
|
|
|
return !EXCLUDED_SETTING_KEYS.includes(setting.key);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const doExport = function doExport(options) {
|
2018-07-30 18:21:52 +03:00
|
|
|
options = options || {include: []};
|
2017-08-01 16:27:13 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
let tables;
|
|
|
|
let version;
|
2016-03-12 21:54:06 +03:00
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return getVersionAndTables(options).then(function exportAllTables(result) {
|
2016-03-12 21:54:06 +03:00
|
|
|
tables = result.tables;
|
|
|
|
version = result.version;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return Promise.mapSeries(tables, function (tableName) {
|
|
|
|
return exportTable(tableName, options);
|
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
}).then(function formatData(tableData) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const exportData = {
|
2016-03-12 21:54:06 +03:00
|
|
|
meta: {
|
|
|
|
exported_on: new Date().getTime(),
|
|
|
|
version: version
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
// Filled below
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(tables, function (name, i) {
|
2021-01-27 09:39:25 +03:00
|
|
|
if (name === 'settings') {
|
|
|
|
exportData.data[name] = getSettingsTableData(tableData[i]);
|
|
|
|
} else {
|
|
|
|
exportData.data[name] = tableData[i];
|
|
|
|
}
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
return exportData;
|
|
|
|
}).catch(function (err) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.DataExportError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
2020-05-22 21:22:20 +03:00
|
|
|
context: i18n.t('errors.data.export.errorExportingData')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-09-15 20:04:42 +04:00
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
module.exports = {
|
|
|
|
doExport: doExport,
|
2018-07-30 18:21:52 +03:00
|
|
|
fileName: exportFileName,
|
|
|
|
EXCLUDED_TABLES: EXCLUDED_TABLES
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|