2015-01-05 19:23:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-05 22:05:35 +00:00
|
|
|
require('require-yaml');
|
|
|
|
|
2015-01-06 06:07:14 +00:00
|
|
|
var connect = require('connect')
|
|
|
|
//, PromiseA = require('bluebird').Promise
|
|
|
|
, query = require('connect-query')
|
|
|
|
, bodyParser = require('body-parser')
|
2015-01-05 19:23:26 +00:00
|
|
|
, serveStatic = require('serve-static')
|
2015-01-06 06:07:14 +00:00
|
|
|
, send = require('connect-send-json')
|
|
|
|
|
|
|
|
, app = connect()
|
2015-01-09 08:49:14 +00:00
|
|
|
, fsapi = require('./lib/fsapi')
|
2015-01-06 06:07:14 +00:00
|
|
|
, walk = require('./lib/fsapi').walk
|
|
|
|
, getfs = require('./lib/fsapi').getfs
|
2015-01-07 07:58:09 +00:00
|
|
|
, putfs = require('./lib/fsapi').putfs
|
2015-01-06 06:07:14 +00:00
|
|
|
|
|
|
|
, config = require('./config.yml')
|
|
|
|
, path = require('path')
|
|
|
|
, blogdir = path.resolve(config.blogdir || __dirname)
|
2015-01-05 19:23:26 +00:00
|
|
|
;
|
|
|
|
|
2015-01-05 22:05:35 +00:00
|
|
|
|
2015-01-05 19:23:26 +00:00
|
|
|
app
|
2015-01-05 22:05:35 +00:00
|
|
|
.use(send.json())
|
|
|
|
.use(query())
|
2015-01-08 22:09:22 +00:00
|
|
|
.use(bodyParser.json({ limit: 10 * 1024 * 1024 })) // 10mb
|
|
|
|
.use(require('compression')())
|
2015-01-05 22:05:35 +00:00
|
|
|
.use('/api/fs/walk', function (req, res, next) {
|
|
|
|
if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-07 07:58:09 +00:00
|
|
|
var opts = {}
|
|
|
|
, dirnames = req.query.dir && [req.query.dir] || (req.query.dirs && req.query.dirs.split(/,/g)) || req.body.dirs
|
2015-01-05 22:05:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
if (!dirnames || !dirnames.length) {
|
2015-01-06 06:07:14 +00:00
|
|
|
res.json({ error: "please specify GET w/ req.query.dir or POST w/ _method=GET&dirs=path/to/thing,..." });
|
2015-01-05 22:05:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-06 06:07:14 +00:00
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO opts.contents?
|
|
|
|
walk.walkDirs(blogdir, dirnames, opts).then(function (stats) {
|
|
|
|
if (!req.body.dirs && !req.query.dirs) {
|
2015-01-05 22:05:35 +00:00
|
|
|
res.json(stats[dirnames[0]]);
|
|
|
|
} else {
|
|
|
|
res.json(stats);
|
|
|
|
}
|
|
|
|
});
|
2015-01-05 19:23:26 +00:00
|
|
|
})
|
2015-01-07 07:58:09 +00:00
|
|
|
.use('/api/fs/files', 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.json({ error: "please specify GET w/ req.query.path or POST _method=GET&paths=path/to/thing,..." });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getfs(blogdir, filepaths).then(function (files) {
|
|
|
|
if (!req.body.paths && !req.query.paths) {
|
|
|
|
res.json(files[0]);
|
|
|
|
} else {
|
|
|
|
res.send(files);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.use('/api/fs/files', 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.json({ error: "please specify POST w/ req.body.files" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return putfs(blogdir, files, opts).then(function (results) {
|
|
|
|
res.json(results);
|
|
|
|
});
|
|
|
|
})
|
2015-01-09 08:49:14 +00:00
|
|
|
.use('/api/fs/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.json({ error: "please specify POST w/ req.body.files" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fsapi.copyfs(blogdir, files, opts).then(function (results) {
|
|
|
|
res.json(results);
|
|
|
|
});
|
|
|
|
})
|
2015-01-05 22:05:35 +00:00
|
|
|
|
2015-01-05 19:23:26 +00:00
|
|
|
.use('/api/fs', function (req, res, next) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
})
|
2015-01-06 06:07:14 +00:00
|
|
|
.use('/api/fs/static', serveStatic('.'))
|
|
|
|
|
2015-01-05 19:23:26 +00:00
|
|
|
.use(serveStatic('.'))
|
2015-01-09 08:49:14 +00:00
|
|
|
.use(serveStatic(blogdir))
|
2015-01-05 19:23:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
module.exports = app;
|
2015-01-05 22:05:35 +00:00
|
|
|
|
2015-01-09 08:49:14 +00:00
|
|
|
require('http').createServer().on('request', app).listen(process.argv[2] || 65080, function () {
|
|
|
|
console.log('listening ' + (process.argv[2] || 65080));
|
2015-01-05 22:05:35 +00:00
|
|
|
});
|