2013-12-25 04:05:20 +04:00
|
|
|
var dataExport = require('../data/export'),
|
|
|
|
dataImport = require('../data/import'),
|
|
|
|
dataProvider = require('../models'),
|
2013-12-29 00:13:47 +04:00
|
|
|
notifications = require('./notifications'),
|
|
|
|
settings = require('./settings'),
|
2013-12-25 04:05:20 +04:00
|
|
|
fs = require('fs-extra'),
|
|
|
|
path = require('path'),
|
|
|
|
when = require('when'),
|
|
|
|
nodefn = require('when/node/function'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
schema = require('../data/schema'),
|
|
|
|
config = require('../config'),
|
2013-12-28 20:01:08 +04:00
|
|
|
debugPath = config.paths().subdir + '/ghost/debug/',
|
2013-10-09 12:52:58 +04:00
|
|
|
|
|
|
|
db;
|
|
|
|
|
|
|
|
db = {
|
2013-11-01 16:12:01 +04:00
|
|
|
'export': function (req, res) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-10-09 12:52:58 +04:00
|
|
|
return dataExport().then(function (exportedData) {
|
|
|
|
// Save the exported data to the file system for download
|
|
|
|
var fileName = path.resolve(__dirname + '/../../server/data/export/exported-' + (new Date().getTime()) + '.json');
|
|
|
|
|
|
|
|
return nodefn.call(fs.writeFile, fileName, JSON.stringify(exportedData)).then(function () {
|
|
|
|
return when(fileName);
|
|
|
|
});
|
|
|
|
}).then(function (exportedFilePath) {
|
|
|
|
// Send the exported data file
|
|
|
|
res.download(exportedFilePath, 'GhostData.json');
|
|
|
|
}).otherwise(function (error) {
|
|
|
|
// Notify of an error if it occurs
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.browse().then(function (notifications) {
|
2013-12-06 12:51:35 +04:00
|
|
|
var notification = {
|
|
|
|
type: 'error',
|
|
|
|
message: error.message || error,
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'per-' + (notifications.length + 1)
|
|
|
|
};
|
2013-12-04 06:47:39 +04:00
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.add(notification).then(function () {
|
2013-12-09 04:49:02 +04:00
|
|
|
res.redirect(debugPath);
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2013-10-09 12:52:58 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2013-11-01 16:12:01 +04:00
|
|
|
'import': function (req, res) {
|
2013-12-04 06:47:39 +04:00
|
|
|
var notification,
|
|
|
|
databaseVersion;
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-12-04 06:47:39 +04:00
|
|
|
if (!req.files.importfile || !req.files.importfile.path || req.files.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
|
|
|
|
*/
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.browse().then(function (notifications) {
|
2013-12-06 12:51:35 +04:00
|
|
|
notification = {
|
|
|
|
type: 'error',
|
|
|
|
message: "Must select a .json file to import",
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'per-' + (notifications.length + 1)
|
|
|
|
};
|
2013-12-04 06:47:39 +04:00
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.add(notification).then(function () {
|
2013-12-09 04:49:02 +04:00
|
|
|
res.redirect(debugPath);
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2013-10-09 12:52:58 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
settings.read({ key: 'databaseVersion' }).then(function (setting) {
|
2013-12-04 06:47:39 +04:00
|
|
|
return when(setting.value);
|
|
|
|
}, function () {
|
|
|
|
return when('001');
|
|
|
|
}).then(function (version) {
|
|
|
|
databaseVersion = version;
|
|
|
|
// Read the file contents
|
|
|
|
return nodefn.call(fs.readFile, req.files.importfile.path);
|
|
|
|
}).then(function (fileContents) {
|
|
|
|
var importData,
|
|
|
|
error = '',
|
|
|
|
tableKeys = _.keys(schema);
|
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) {
|
|
|
|
return when.reject(new Error("Failed to parse the import file"));
|
|
|
|
}
|
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
|
|
|
|
2013-12-04 06:47:39 +04:00
|
|
|
_.each(tableKeys, function (constkey) {
|
|
|
|
_.each(importData.data[constkey], function (elem) {
|
|
|
|
var prop;
|
|
|
|
for (prop in elem) {
|
|
|
|
if (elem.hasOwnProperty(prop)) {
|
|
|
|
if (schema[constkey].hasOwnProperty(prop)) {
|
2013-12-29 00:13:47 +04:00
|
|
|
if (!_.isNull(elem[prop])) {
|
|
|
|
if (elem[prop].length > schema[constkey][prop].maxlength) {
|
|
|
|
error += error !== "" ? "<br>" : "";
|
|
|
|
error += "Property '" + prop + "' exceeds maximum length of " + schema[constkey][prop].maxlength + " (element:" + constkey + " / id:" + elem.id + ")";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!schema[constkey][prop].nullable) {
|
|
|
|
error += error !== "" ? "<br>" : "";
|
|
|
|
error += "Property '" + prop + "' is not nullable (element:" + constkey + " / id:" + elem.id + ")";
|
2013-10-09 12:52:58 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-04 06:47:39 +04:00
|
|
|
} else {
|
|
|
|
error += error !== "" ? "<br>" : "";
|
|
|
|
error += "Property '" + prop + "' is not allowed (element:" + constkey + " / id:" + elem.id + ")";
|
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
}
|
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-29 00:13:47 +04:00
|
|
|
return settings.updateSettingsCache();
|
|
|
|
}).then(function () {
|
|
|
|
return notifications.browse();
|
2013-12-04 06:47:39 +04:00
|
|
|
}).then(function (notifications) {
|
|
|
|
notification = {
|
|
|
|
type: 'success',
|
2013-12-29 00:13:47 +04:00
|
|
|
message: "Posts, tags and other data successfully imported",
|
2013-12-04 06:47:39 +04:00
|
|
|
status: 'persistent',
|
|
|
|
id: 'per-' + (notifications.length + 1)
|
|
|
|
};
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.add(notification).then(function () {
|
|
|
|
res.redirect(debugPath);
|
2013-12-04 06:47:39 +04:00
|
|
|
});
|
|
|
|
}).otherwise(function importFailure(error) {
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.browse().then(function (notifications) {
|
2013-12-04 06:47:39 +04:00
|
|
|
// Notify of an error if it occurs
|
|
|
|
notification = {
|
|
|
|
type: 'error',
|
|
|
|
message: error.message || error,
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'per-' + (notifications.length + 1)
|
|
|
|
};
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
return notifications.add(notification).then(function () {
|
2013-12-04 06:47:39 +04:00
|
|
|
res.redirect(debugPath);
|
2013-10-09 12:52:58 +04:00
|
|
|
});
|
|
|
|
});
|
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) {
|
|
|
|
return when.reject({errorCode: 500, message: error.message || error});
|
|
|
|
});
|
2013-11-01 16:12:01 +04:00
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
};
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
module.exports = db;
|