From 67611045e7cb6f90fb3015046adaac8737b20b69 Mon Sep 17 00:00:00 2001 From: Sebastian Gierlinger Date: Thu, 27 Feb 2014 16:48:38 +0100 Subject: [PATCH] Remove res.redirect from db.exportContent closes #1654 - added frontend route /ghost/export/ - removed request handling from API --- core/server/api/db.js | 30 ++++-------------------------- core/server/controllers/admin.js | 22 ++++++++++++++++++++++ core/server/routes/admin.js | 2 ++ core/server/routes/api.js | 2 +- core/server/views/debug.hbs | 2 +- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/core/server/api/db.js b/core/server/api/db.js index ef717d7642..3d1bb83d09 100644 --- a/core/server/api/db.js +++ b/core/server/api/db.js @@ -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) { diff --git a/core/server/controllers/admin.js b/core/server/controllers/admin.js index e8f2d2f19c..bee933dedc 100644 --- a/core/server/controllers/admin.js +++ b/core/server/controllers/admin.js @@ -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 diff --git a/core/server/routes/admin.js b/core/server/routes/admin.js index f9fe4d8470..cb955c16bf 100644 --- a/core/server/routes/admin.js +++ b/core/server/routes/admin.js @@ -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. diff --git a/core/server/routes/api.js b/core/server/routes/api.js index ed2742fa3e..b5b3cb8bb8 100644 --- a/core/server/routes/api.js +++ b/core/server/routes/api.js @@ -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)); }; \ No newline at end of file diff --git a/core/server/views/debug.hbs b/core/server/views/debug.hbs index 8116277eaf..a960018e47 100644 --- a/core/server/views/debug.hbs +++ b/core/server/views/debug.hbs @@ -20,7 +20,7 @@
- Export + Export

Export the blog settings and data.