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-03-05 14:38:45 +03:00
|
|
|
const EXCLUDED_TABLES = [
|
|
|
|
'sessions',
|
|
|
|
'mobiledoc_revisions',
|
|
|
|
'email_batches',
|
|
|
|
'email_recipients',
|
|
|
|
'members_payment_events',
|
|
|
|
'members_login_events',
|
|
|
|
'members_email_change_events',
|
|
|
|
'members_status_events',
|
|
|
|
'members_paid_subscription_events',
|
|
|
|
'members_subscribe_events'
|
|
|
|
];
|
2021-01-27 09:39:25 +03:00
|
|
|
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}};
|
|
|
|
|
2021-03-25 04:55:11 +03:00
|
|
|
const exportFileName = async 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) {
|
2021-03-25 04:55:11 +03:00
|
|
|
return options.filename + '.json';
|
2018-01-11 18:03:21 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 04:55:11 +03:00
|
|
|
try {
|
|
|
|
const settingsTitle = await models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting')));
|
|
|
|
|
|
|
|
if (settingsTitle) {
|
|
|
|
title = security.string.safe(settingsTitle.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';
|
2021-03-25 04:55:11 +03:00
|
|
|
} catch (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';
|
2021-03-25 04:55:11 +03:00
|
|
|
}
|
2014-07-19 16:20:16 +04:00
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-25 04:55:11 +03:00
|
|
|
const doExport = async function doExport(options) {
|
2018-07-30 18:21:52 +03:00
|
|
|
options = options || {include: []};
|
2017-08-01 16:27:13 +03:00
|
|
|
|
2021-03-25 04:55:11 +03:00
|
|
|
try {
|
2021-03-25 05:01:54 +03:00
|
|
|
const tables = await commands.getTables(options.transacting);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
2021-03-25 04:55:11 +03:00
|
|
|
const tableData = await Promise.mapSeries(tables, function (tableName) {
|
2017-08-01 16:27:13 +03:00
|
|
|
return exportTable(tableName, options);
|
|
|
|
});
|
2021-03-25 04:55:11 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const exportData = {
|
2016-03-12 21:54:06 +03:00
|
|
|
meta: {
|
|
|
|
exported_on: new Date().getTime(),
|
2021-03-25 05:01:54 +03:00
|
|
|
version: ghostVersion.full
|
2016-03-12 21:54:06 +03:00
|
|
|
},
|
|
|
|
data: {
|
|
|
|
// Filled below
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-25 05:01:54 +03:00
|
|
|
tables.forEach((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;
|
2021-03-25 04:55:11 +03:00
|
|
|
} catch (err) {
|
|
|
|
throw 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')
|
2021-03-25 04:55:11 +03: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
|
|
|
};
|