2013-12-25 04:05:20 +04:00
|
|
|
var dataExport = require('../data/export'),
|
|
|
|
dataImport = require('../data/import'),
|
|
|
|
dataProvider = require('../models'),
|
|
|
|
fs = require('fs-extra'),
|
|
|
|
when = require('when'),
|
|
|
|
nodefn = require('when/node/function'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2014-02-19 21:32:23 +04:00
|
|
|
validation = require('../data/validation'),
|
2014-01-29 08:18:00 +04:00
|
|
|
errors = require('../../server/errorHandling'),
|
2013-12-30 03:52:55 +04:00
|
|
|
api = {},
|
2013-10-09 12:52:58 +04:00
|
|
|
db;
|
|
|
|
|
2013-12-30 03:52:55 +04:00
|
|
|
api.notifications = require('./notifications');
|
|
|
|
api.settings = require('./settings');
|
|
|
|
|
2013-10-09 12:52:58 +04:00
|
|
|
db = {
|
2014-02-27 19:48:38 +04:00
|
|
|
'exportContent': function () {
|
|
|
|
// Export data, otherwise send error 500
|
|
|
|
return dataExport().otherwise(function (error) {
|
|
|
|
return when.reject({errorCode: 500, message: error.message || error});
|
2013-10-09 12:52:58 +04:00
|
|
|
});
|
|
|
|
},
|
2014-01-06 18:05:31 +04:00
|
|
|
'importContent': function (options) {
|
|
|
|
var databaseVersion;
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2014-01-06 18:05:31 +04:00
|
|
|
if (!options.importfile || !options.importfile.path || options.importfile.name.indexOf('json') === -1) {
|
2013-10-09 12:52:58 +04:00
|
|
|
/**
|
|
|
|
* Notify of an error if it occurs
|
|
|
|
*
|
|
|
|
* - If there's no file (although if you don't select anything, the input is still submitted, so
|
|
|
|
* !req.files.importfile will always be false)
|
2013-12-04 06:47:39 +04:00
|
|
|
* - If there is no path
|
2013-10-09 12:52:58 +04:00
|
|
|
* - If the name doesn't have json in it
|
|
|
|
*/
|
2014-02-26 21:51:01 +04:00
|
|
|
return when.reject({code: 500, message: 'Please select a .json file to import.'});
|
2013-10-09 12:52:58 +04:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:05:31 +04:00
|
|
|
return api.settings.read({ key: 'databaseVersion' }).then(function (setting) {
|
2013-12-04 06:47:39 +04:00
|
|
|
return when(setting.value);
|
|
|
|
}, function () {
|
2014-03-24 12:29:31 +04:00
|
|
|
return when('002');
|
2013-12-04 06:47:39 +04:00
|
|
|
}).then(function (version) {
|
|
|
|
databaseVersion = version;
|
|
|
|
// Read the file contents
|
2014-01-06 18:05:31 +04:00
|
|
|
return nodefn.call(fs.readFile, options.importfile.path);
|
2013-12-04 06:47:39 +04:00
|
|
|
}).then(function (fileContents) {
|
|
|
|
var importData,
|
2014-02-19 21:32:23 +04:00
|
|
|
error = '';
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-12-04 06:47:39 +04:00
|
|
|
// Parse the json data
|
|
|
|
try {
|
|
|
|
importData = JSON.parse(fileContents);
|
|
|
|
} catch (e) {
|
2014-01-29 08:18:00 +04:00
|
|
|
errors.logError(e, "API DB import content", "check that the import file is valid JSON.");
|
|
|
|
return when.reject(new Error("Failed to parse the import JSON file"));
|
2013-12-04 06:47:39 +04:00
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-12-04 06:47:39 +04:00
|
|
|
if (!importData.meta || !importData.meta.version) {
|
|
|
|
return when.reject(new Error("Import data does not specify version"));
|
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2014-02-19 21:32:23 +04:00
|
|
|
_.each(_.keys(importData.data), function (tableName) {
|
|
|
|
_.each(importData.data[tableName], function (importValues) {
|
|
|
|
try {
|
|
|
|
validation.validateSchema(tableName, importValues);
|
|
|
|
} catch (err) {
|
|
|
|
error += error !== "" ? "<br>" : "";
|
|
|
|
error += err.message;
|
2013-12-04 06:47:39 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error !== "") {
|
|
|
|
return when.reject(new Error(error));
|
|
|
|
}
|
|
|
|
// Import for the current version
|
|
|
|
return dataImport(databaseVersion, importData);
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-12-04 06:47:39 +04:00
|
|
|
}).then(function importSuccess() {
|
2013-12-30 03:52:55 +04:00
|
|
|
return api.settings.updateSettingsCache();
|
2013-12-29 00:13:47 +04:00
|
|
|
}).then(function () {
|
2014-01-06 18:05:31 +04:00
|
|
|
return when.resolve({message: 'Posts, tags and other data successfully imported'});
|
2013-12-04 06:47:39 +04:00
|
|
|
}).otherwise(function importFailure(error) {
|
2014-02-26 21:51:01 +04:00
|
|
|
return when.reject({code: 500, message: error.message || error});
|
2014-03-24 12:29:31 +04:00
|
|
|
}).finally(function () {
|
|
|
|
// Unlink the file after import
|
|
|
|
return nodefn.call(fs.unlink, options.importfile.path);
|
2013-12-04 06:47:39 +04:00
|
|
|
});
|
2013-12-25 04:05:20 +04:00
|
|
|
},
|
|
|
|
'deleteAllContent': function () {
|
|
|
|
return when(dataProvider.deleteAllContent())
|
|
|
|
.then(function () {
|
|
|
|
return when.resolve({message: 'Successfully deleted all content from your blog.'});
|
|
|
|
}, function (error) {
|
2014-02-26 21:51:01 +04:00
|
|
|
return when.reject({code: 500, message: error.message || error});
|
2013-12-25 04:05:20 +04:00
|
|
|
});
|
2013-11-01 16:12:01 +04:00
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
};
|
|
|
|
|
2014-01-29 08:18:00 +04:00
|
|
|
module.exports = db;
|