Remove res.redirect from db.exportContent

closes #1654
- added frontend route /ghost/export/
- removed request handling from API
This commit is contained in:
Sebastian Gierlinger 2014-02-27 16:48:38 +01:00
parent 7a6f4811f6
commit 67611045e7
5 changed files with 30 additions and 28 deletions

View File

@ -16,32 +16,10 @@ api.notifications = require('./notifications');
api.settings = require('./settings');
db = {
'exportContent': function (req, res) {
/*jslint unparam:true*/
return dataExport().then(function (exportedData) {
// Save the exported data to the file system for download
var fileName = path.join(config().paths.exportPath, '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
return api.notifications.browse().then(function (notifications) {
var notification = {
type: 'error',
message: error.message || error,
status: 'persistent',
id: 'per-' + (notifications.length + 1)
};
return api.notifications.add(notification).then(function () {
res.redirect(config().paths.debugPath);
});
});
'exportContent': function () {
// Export data, otherwise send error 500
return dataExport().otherwise(function (error) {
return when.reject({errorCode: 500, message: error.message || error});
});
},
'importContent': function (options) {

View File

@ -111,6 +111,28 @@ adminControllers = {
bodyClass: 'settings',
adminNav: setSelected(adminNavbar, 'settings')
});
},
// frontend route for downloading a file
exportContent: function (req, res) {
/*jslint unparam:true*/
api.db.exportContent().then(function (exportData) {
// send a file to the client
res.set('Content-Disposition', 'attachment; filename="GhostData.json"');
res.json(exportData);
}).otherwise(function (err) {
var notification = {
type: 'error',
message: 'Your export file could not be generated.',
status: 'persistent',
id: 'errorexport'
};
errors.logError(err, 'admin.js', "Your export file could not be generated.");
return api.notifications.add(notification).then(function () {
res.redirect(config().paths.subdir + '/ghost/debug');
});
});
}
},
// Route: upload

View File

@ -43,6 +43,8 @@ module.exports = function (server) {
server.get('/ghost/settings*', admin.settings);
server.get('/ghost/debug/', admin.debug.index);
server.get('/ghost/export/', admin.debug.exportContent);
server.post('/ghost/upload/', middleware.busboy, admin.upload);
// redirect to /ghost and let that do the authentication to prevent redirects to /ghost//admin etc.

View File

@ -24,7 +24,7 @@ module.exports = function (server) {
server.del('/ghost/api/v0.1/notifications/:id', api.requestHandler(api.notifications.destroy));
server.post('/ghost/api/v0.1/notifications/', api.requestHandler(api.notifications.add));
// #### Import/Export
server.get('/ghost/api/v0.1/db/', api.db.exportContent);
server.get('/ghost/api/v0.1/db/', api.requestHandler(api.db.exportContent));
server.post('/ghost/api/v0.1/db/', middleware.busboy, api.requestHandler(api.db.importContent));
server.del('/ghost/api/v0.1/db/', api.requestHandler(api.db.deleteAllContent));
};

View File

@ -20,7 +20,7 @@
<fieldset>
<div class="form-group">
<label>Export</label>
<a href="{{admin_url}}/api/v0.1/db/" class="button-save">Export</a>
<a href="{{admin_url}}/export/" class="button-save">Export</a>
<p>Export the blog settings and data.</p>
</div>
</fieldset>