2016-10-06 15:27:35 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
db = require('../../data/db'),
|
|
|
|
commands = require('../schema').commands,
|
2014-07-19 16:20:16 +04:00
|
|
|
serverUtils = require('../../utils'),
|
2017-01-26 15:12:00 +03:00
|
|
|
ghostVersion = require('../../utils/ghost-version'),
|
2014-07-19 16:20:16 +04:00
|
|
|
errors = require('../../errors'),
|
2016-10-04 18:33:43 +03:00
|
|
|
logging = require('../../logging'),
|
2017-03-08 13:26:57 +03:00
|
|
|
models = require('../../models'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../../i18n'),
|
2016-03-12 21:54:06 +03:00
|
|
|
excludedTables = ['accesstokens', 'refreshtokens', 'clients', 'client_trusted_domains'],
|
|
|
|
modelOptions = {context: {internal: true}},
|
|
|
|
|
|
|
|
// private
|
|
|
|
getVersionAndTables,
|
|
|
|
exportTable,
|
|
|
|
|
|
|
|
// public
|
|
|
|
doExport,
|
2014-07-19 16:20:16 +04:00
|
|
|
exportFileName;
|
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
exportFileName = function exportFileName() {
|
2014-07-19 16:20:16 +04:00
|
|
|
var datetime = (new Date()).toJSON().substring(0, 10),
|
|
|
|
title = '';
|
|
|
|
|
2017-03-08 13:26:57 +03:00
|
|
|
return models.Settings.findOne(_.merge({key: 'title'}, modelOptions)).then(function (result) {
|
2014-07-19 16:20:16 +04:00
|
|
|
if (result) {
|
2017-03-08 13:26:57 +03:00
|
|
|
title = serverUtils.safeString(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) {
|
2016-10-06 15:27:35 +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
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
getVersionAndTables = function getVersionAndTables() {
|
|
|
|
var props = {
|
2017-01-26 15:12:00 +03:00
|
|
|
version: ghostVersion.full,
|
2016-10-06 15:27:35 +03:00
|
|
|
tables: commands.getTables()
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props);
|
|
|
|
};
|
|
|
|
|
|
|
|
exportTable = function exportTable(tableName) {
|
|
|
|
if (excludedTables.indexOf(tableName) < 0) {
|
|
|
|
return db.knex(tableName).select();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
doExport = function doExport() {
|
|
|
|
var tables, version;
|
|
|
|
|
|
|
|
return getVersionAndTables().then(function exportAllTables(result) {
|
|
|
|
tables = result.tables;
|
|
|
|
version = result.version;
|
|
|
|
|
|
|
|
return Promise.mapSeries(tables, exportTable);
|
|
|
|
}).then(function formatData(tableData) {
|
|
|
|
var exportData = {
|
|
|
|
meta: {
|
|
|
|
exported_on: new Date().getTime(),
|
|
|
|
version: version
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
// Filled below
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(tables, function (name, i) {
|
|
|
|
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) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.DataExportError({
|
|
|
|
err: err,
|
|
|
|
context: i18n.t('errors.data.export.errorExportingData')
|
|
|
|
}));
|
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,
|
|
|
|
fileName: exportFileName
|
|
|
|
};
|