Skip to content

Commit

Permalink
Pipeline-ify configuration/themes/upload APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
halfdan authored and hoxoa committed Oct 27, 2015
1 parent ffd7316 commit 010314f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 38 deletions.
2 changes: 1 addition & 1 deletion content/themes/casper
62 changes: 47 additions & 15 deletions core/server/api/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var _ = require('lodash'),
config = require('../config'),
errors = require('../errors'),
Promise = require('bluebird'),
utils = require('./utils'),
pipeline = require('../utils/pipeline'),

configuration;

Expand Down Expand Up @@ -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>} 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);

}
};

Expand Down
58 changes: 37 additions & 21 deletions core/server/api/upload.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
};

Expand Down
2 changes: 1 addition & 1 deletion core/test/integration/api/api_configuration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Configuration API', function () {
done();
}).catch(function (error) {
console.log(JSON.stringify(error));
done();
done(error);
}).catch(done);
});

Expand Down

0 comments on commit 010314f

Please sign in to comment.