diff --git a/lib/cartodb/cartodb_windshaft.js b/lib/cartodb/cartodb_windshaft.js index b8063748..92746067 100644 --- a/lib/cartodb/cartodb_windshaft.js +++ b/lib/cartodb/cartodb_windshaft.js @@ -208,66 +208,10 @@ var CartodbWindshaft = function(serverOptions) { var TemplateMapsController = require('./controllers/template_maps'), templateMapsController = new TemplateMapsController( - ws, templateMaps, serverOptions.signedMaps, cartoData, template_baseurl + ws, serverOptions, templateMaps, serverOptions.signedMaps, cartoData, template_baseurl ); templateMapsController.register(ws); - // Update a template - ws.put(template_baseurl + '/:template_id', function(req, res) { - ws.doCORS(res); - var that = this; - var response = {}; - var cdbuser = ws.userByReq(req); - var template; - var tpl_id; - Step( - function checkPerms(){ - serverOptions.authorizedByAPIKey(req, this); - }, - function updateTemplate(err, authenticated) { - if ( err ) throw err; - if (authenticated !== 1) { - err = new Error("Only authenticated user can list templated maps"); - err.http_status = 403; - throw err; - } - if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' ) - throw new Error('template PUT data must be of type application/json'); - template = req.body; - tpl_id = req.params.template_id.split('@'); - if ( tpl_id.length > 1 ) { - if ( tpl_id[0] != cdbuser ) { - err = new Error("Invalid template id '" - + req.params.template_id + "' for user '" + cdbuser + "'"); - err.http_status = 404; - throw err; - } - tpl_id = tpl_id[1]; - } - templateMaps.updTemplate(cdbuser, tpl_id, template, this); - }, - function prepareResponse(err){ - if ( err ) throw err; - return { template_id: cdbuser + '@' + tpl_id }; - }, - function finish(err, response){ - if ( req.profiler ) { - res.header('X-Tiler-Profiler', req.profiler.toJSONString()); - } - if (err){ - var statusCode = 400; - response = { error: ''+err }; - if ( ! _.isUndefined(err.http_status) ) { - statusCode = err.http_status; - } - ws.sendError(res, response, statusCode, 'PUT TEMPLATE', err); - } else { - ws.sendResponse(res, [response, 200]); - } - } - ); - }); - // Get a specific template ws.get(template_baseurl + '/:template_id', function(req, res) { if ( req.profiler && req.profiler.statsd_client ) { diff --git a/lib/cartodb/controllers/template_maps.js b/lib/cartodb/controllers/template_maps.js index 937bcc3e..27b70be7 100644 --- a/lib/cartodb/controllers/template_maps.js +++ b/lib/cartodb/controllers/template_maps.js @@ -1,8 +1,9 @@ var Step = require('step'); var _ = require('underscore'); -function TemplateMapsController(app, templateMaps, signedMaps, metadataBackend, templateBaseUrl) { +function TemplateMapsController(app, serverOptions, templateMaps, signedMaps, metadataBackend, templateBaseUrl) { this.app = app; + this.serverOptions = serverOptions; this.templateMaps = templateMaps; this.signedMaps = signedMaps; this.metadataBackend = metadataBackend; @@ -15,6 +16,7 @@ module.exports = TemplateMapsController; TemplateMapsController.prototype.register = function(app) { app.get(this.templateBaseUrl + '/:template_id/jsonp', this.jsonp.bind(this)); app.post(this.templateBaseUrl, this.create.bind(this)); + app.put(this.templateBaseUrl + '/:template_id', this.update.bind(this)); }; @@ -28,7 +30,7 @@ TemplateMapsController.prototype.create = function(req, res) { Step( function checkPerms(){ - serverOptions.authorizedByAPIKey(req, this); + self.serverOptions.authorizedByAPIKey(req, this); }, function addTemplate(err, authenticated) { if ( err ) throw err; @@ -65,6 +67,63 @@ TemplateMapsController.prototype.create = function(req, res) { ); }; +// Update a template +TemplateMapsController.prototype.update = function(req, res) { + var self = this; + + this.app.doCORS(res); + + var cdbuser = this.app.userByReq(req); + var template; + var tpl_id; + Step( + function checkPerms(){ + self.serverOptions.authorizedByAPIKey(req, this); + }, + function updateTemplate(err, authenticated) { + if ( err ) throw err; + if (authenticated !== 1) { + err = new Error("Only authenticated user can list templated maps"); + err.http_status = 403; + throw err; + } + if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' ) + throw new Error('template PUT data must be of type application/json'); + template = req.body; + tpl_id = req.params.template_id.split('@'); + if ( tpl_id.length > 1 ) { + if ( tpl_id[0] != cdbuser ) { + err = new Error("Invalid template id '" + + req.params.template_id + "' for user '" + cdbuser + "'"); + err.http_status = 404; + throw err; + } + tpl_id = tpl_id[1]; + } + self.templateMaps.updTemplate(cdbuser, tpl_id, template, this); + }, + function prepareResponse(err){ + if ( err ) throw err; + return { template_id: cdbuser + '@' + tpl_id }; + }, + function finish(err, response){ + if ( req.profiler ) { + res.header('X-Tiler-Profiler', req.profiler.toJSONString()); + } + if (err){ + var statusCode = 400; + response = { error: ''+err }; + if ( ! _.isUndefined(err.http_status) ) { + statusCode = err.http_status; + } + self.app.sendError(res, response, statusCode, 'PUT TEMPLATE', err); + } else { + self.app.sendResponse(res, [response, 200]); + } + } + ); +}; + /** * jsonp endpoint, allows to instantiate a template with a json call. * callback query argument is mandatory