diff --git a/app.js b/app.js index bd880e4..312b505 100644 --- a/app.js +++ b/app.js @@ -56,6 +56,7 @@ function initApp (config, callback) { require('./route/task/index')(app); require('./route/task/delete')(app); require('./route/result/index')(app); + require('./route/result/download')(app); // Error handling app.express.get('*', function (req, res) { diff --git a/route/result/download.js b/route/result/download.js new file mode 100644 index 0000000..36b8c0e --- /dev/null +++ b/route/result/download.js @@ -0,0 +1,42 @@ +'use strict'; + +var moment = require('moment'); + +module.exports = route; + +// Route definition +function route (app) { + + app.express.get('/:id/:rid.csv', function (req, res, next) { + app.webservice.task(req.params.id).get({}, function (err, task) { + if (err) { + return next(); + } + app.webservice + .task(req.params.id) + .result(req.params.rid) + .get({full: true}, function (err, result) { + if (err) { + return next(); + } + var rows = ['"code", "message", "type"']; + result.results.forEach(function (msg) { + rows.push([ + JSON.stringify(msg.code), + JSON.stringify(msg.message), + JSON.stringify(msg.type) + ].join(', ')); + }); + res.attachment([ + 'pa11y--', + task.url.replace(/^https?:\/\//i, '').replace(/\/$/, '').replace(/[^a-z0-9\.\-\_]+/gi, '-') + '--', + task.standard.toLowerCase() + '--', + moment(result.date).format('YYYY-MM-DD'), + '.csv' + ].join('')); + res.send(rows.join('\n')); + }); + }); + }); + +} diff --git a/view/task/index.html b/view/task/index.html index dec3ed2..093a3a3 100644 --- a/view/task/index.html +++ b/view/task/index.html @@ -87,7 +87,7 @@
- Download CSV + Download CSV