deardesi.js/desirae-http-api.js

134 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-01-13 21:50:17 +00:00
'use strict';
2015-01-15 06:41:52 +00:00
var fsapi = require('desirae/lib/node-adapters').fsapi
;
2015-01-13 21:50:17 +00:00
module.exports.create = function (options) {
var restful = {}
;
//
// Required for desirae
//
restful.walk = function (req, res, next) {
if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) {
next();
return;
}
var opts = {}
, dirnames = req.query.dir && [req.query.dir] || (req.query.dirs && req.query.dirs.split(/,/g)) || req.body.dirs
;
if (!dirnames || !dirnames.length) {
res.send({ error: "please specify GET w/ req.query.dir or POST w/ _method=GET&dirs=path/to/thing,..." });
2015-01-13 21:50:17 +00:00
return;
}
if (!dirnames.every(function (dirname) {
return 'string' === typeof dirname;
})) {
res.send({ error: "malformed request: " + JSON.stringify(dirnames) });
2015-01-13 21:50:17 +00:00
return;
}
/*
if (req.query.excludes) {
opts.excludes = req.query.excludes.split(',');
}
*/
if (req.query.extensions) {
opts.extensions = req.query.extensions.split(/,/g);
}
if ('true' === req.query.dotfiles) {
opts.dotfiles = true;
}
if ('false' === req.query.sha1sum) {
opts.sha1sum = false;
}
if ('true' === req.query.contents) {
opts.contents = true;
}
// TODO opts.contents?
fsapi.walk.walkDirs(options.blogdir, dirnames, opts).then(function (stats) {
if (!req.body.dirs && !req.query.dirs) {
res.send(stats[dirnames[0]]);
2015-01-13 21:50:17 +00:00
} else {
res.send(stats);
2015-01-13 21:50:17 +00:00
}
});
};
restful.getFiles = function (req, res, next) {
if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) {
next();
return;
}
var filepaths = req.query.path && [req.query.path] || (req.query.paths && req.query.paths.split(/,/g)) || req.body.paths
;
if (!filepaths || !filepaths.length) {
res.send({ error: "please specify GET w/ req.query.path or POST _method=GET&paths=path/to/thing,..." });
2015-01-13 21:50:17 +00:00
return;
}
return fsapi.getfs(options.blogdir, filepaths).then(function (files) {
if (!req.body.paths && !req.query.paths) {
res.send(files[0]);
2015-01-13 21:50:17 +00:00
} else {
res.send(files);
}
});
};
restful.putFiles = function (req, res, next) {
if (!(/^POST|PUT$/i.test(req.method) || /^POST|PUT$/i.test(req.query._method))) {
next();
return;
}
var opts = {}
, files = req.body.files
;
if (!files || !files.length) {
res.send({ error: "please specify POST w/ req.body.files" });
2015-01-13 21:50:17 +00:00
return;
}
opts.tmpdir = options.tmpdir;
return fsapi.putfs(options.blogdir, files, opts).then(function (results) {
res.send(results);
2015-01-13 21:50:17 +00:00
});
};
restful.copy = function (req, res, next) {
if (!(/^POST|PUT$/i.test(req.method) || /^POST|PUT$/i.test(req.query._method))) {
next();
return;
}
var opts = {}
, files = req.body.files
;
if ('object' !== typeof files || !Object.keys(files).length) {
res.send({ error: "please specify POST w/ req.body.files" });
2015-01-13 21:50:17 +00:00
return;
}
return fsapi.copyfs(options.blogdir, files, opts).then(function (results) {
res.send(results);
2015-01-13 21:50:17 +00:00
});
};
//
// end Desirae API
//
return restful;
};