From 010314f1e5a7b78d35cd7f212092a80777c1021c Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Fri, 3 Jul 2015 19:32:25 +0200 Subject: [PATCH] Pipeline-ify configuration/themes/upload APIs refs #5508 --- content/themes/casper | 2 +- core/server/api/configuration.js | 62 ++++++++++++++----- core/server/api/upload.js | 58 ++++++++++------- .../integration/api/api_configuration_spec.js | 2 +- 4 files changed, 86 insertions(+), 38 deletions(-) diff --git a/content/themes/casper b/content/themes/casper index ede6b03afad..c1443a6e04d 160000 --- a/content/themes/casper +++ b/content/themes/casper @@ -1 +1 @@ -Subproject commit ede6b03afad148eadba701bce9e65401d9e034e8 +Subproject commit c1443a6e04dccdbbe8d138af4cb36410f29759e6 diff --git a/core/server/api/configuration.js b/core/server/api/configuration.js index 3b1067db6ff..c8beb7573c9 100644 --- a/core/server/api/configuration.js +++ b/core/server/api/configuration.js @@ -4,6 +4,8 @@ var _ = require('lodash'), config = require('../config'), errors = require('../errors'), Promise = require('bluebird'), + utils = require('./utils'), + pipeline = require('../utils/pipeline'), configuration; @@ -36,29 +38,59 @@ configuration = { * @returns {Promise(Configurations)} */ browse: function browse() { - return Promise.resolve({configuration: _.map(getValidKeys(), function (value, key) { - return { - key: key, - value: value - }; - })}); + function formatResponse(keys) { + return {configuration: _.map(keys, function (value, key) { + return { + key: key, + value: value + }; + })}; + } + + // Pipeline calls each task passing the result of one to be the arguments for the next + return pipeline([getValidKeys, formatResponse]); }, /** * ### Read - * + * Fetch a single configuration by key + * @param {{key}} options + * @returns {Promise} Configuration */ read: function read(options) { - var data = getValidKeys(); + var tasks, + attrs = ['key']; + + function fetchData(options) { + options.config = getValidKeys(); + return options; + } + + function validateOptions(options) { + if (_.has(options.config, options.data.key)) { + return options; + } else { + return Promise.reject(new errors.NotFoundError('Invalid key')); + } + } - if (_.has(data, options.key)) { - return Promise.resolve({configuration: [{ - key: options.key, - value: data[options.key] - }]}); - } else { - return Promise.reject(new errors.NotFoundError('Invalid key')); + function formatResponse(options) { + return {configuration: [{ + key: options.data.key, + value: options.config[options.data.key] + }]}; } + + + tasks = [ + utils.validate('configuration', {attrs: attrs}), + fetchData, + validateOptions, + formatResponse + ]; + + return pipeline(tasks, options); + } }; diff --git a/core/server/api/upload.js b/core/server/api/upload.js index acac1e2ebae..c21b457173c 100644 --- a/core/server/api/upload.js +++ b/core/server/api/upload.js @@ -1,9 +1,10 @@ -var config = require('../config'), - Promise = require('bluebird'), - fs = require('fs-extra'), - storage = require('../storage'), - errors = require('../errors'), - utils = require('./utils'), +var config = require('../config'), + Promise = require('bluebird'), + fs = require('fs-extra'), + storage = require('../storage'), + errors = require('../errors'), + utils = require('./utils'), + pipeline = require('../utils/pipeline'), upload; @@ -22,27 +23,42 @@ upload = { * @returns {Promise} Success */ add: function (options) { - var store = storage.getStorage(), - filepath; + var tasks, + attrs = ['uploadimage']; - // Check if a file was provided - if (!utils.checkFileExists(options, 'uploadimage')) { - return Promise.reject(new errors.NoPermissionError('Please select an image.')); + function validate(options) { + // Check if a file was provided + if (!utils.checkFileExists(options.data, 'uploadimage')) { + return Promise.reject(new errors.NoPermissionError('Please select an image.')); + } + + // Check if the file is valid + if (!utils.checkFileIsValid(options.data.uploadimage, config.uploads.contentTypes, config.uploads.extensions)) { + return Promise.reject(new errors.UnsupportedMediaTypeError('Please select a valid image.')); + } + + return options; } - // Check if the file is valid - if (!utils.checkFileIsValid(options.uploadimage, config.uploads.contentTypes, config.uploads.extensions)) { - return Promise.reject(new errors.UnsupportedMediaTypeError('Please select a valid image.')); + function storeUpload(options) { + var store = storage.getStorage(), + filepath = options.data.uploadimage.path; + + return store.save(options.data.uploadimage).then(function (url) { + return url; + }).finally(function () { + // Remove uploaded file from tmp location + return Promise.promisify(fs.unlink)(filepath); + }); } - filepath = options.uploadimage.path; + tasks = [ + utils.validate('upload', {attrs: attrs}), + validate, + storeUpload + ]; - return store.save(options.uploadimage).then(function (url) { - return url; - }).finally(function () { - // Remove uploaded file from tmp location - return Promise.promisify(fs.unlink)(filepath); - }); + return pipeline(tasks, options); } }; diff --git a/core/test/integration/api/api_configuration_spec.js b/core/test/integration/api/api_configuration_spec.js index a6b3dc7f965..86420f64f78 100644 --- a/core/test/integration/api/api_configuration_spec.js +++ b/core/test/integration/api/api_configuration_spec.js @@ -44,7 +44,7 @@ describe('Configuration API', function () { done(); }).catch(function (error) { console.log(JSON.stringify(error)); - done(); + done(error); }).catch(done); });