From 3b19c4e87781f0df5cd9fd7085c45dfeb7ce9b89 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 5 Jan 2015 22:05:35 +0000 Subject: [PATCH] retrieving files and folders --- .gitmodules | 3 - lib/walk.js | 175 ++++++++++++++++++++++++++++++++++++++++++++++----- package.json | 5 ++ server.js | 89 +++++++++++++++++++++----- twitter | 1 - 5 files changed, 240 insertions(+), 33 deletions(-) delete mode 160000 twitter diff --git a/.gitmodules b/.gitmodules index c3a8144..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "twitter"] - path = twitter - url = git@github.com:coolaj86/twitter.git diff --git a/lib/walk.js b/lib/walk.js index be6943b..f13800f 100644 --- a/lib/walk.js +++ b/lib/walk.js @@ -1,28 +1,173 @@ 'use strict'; var PromiseA = require('bluebird').Promise + , fs = PromiseA.promisifyAll(require('fs')) + , forEachAsync = require('foreachasync').forEachAsync , path = require('path') , walk = require('walk') , walker ; -function getFs(parent, sub) { - // TODO safe - var trueRoot = path.resolve(parent, sub) +function strip(prefix, pathname) { + return pathname.substr(prefix.length + 1); +} + +/* +function walkDir(parent, sub, opts) { + opts = opts || {}; + + var prefix = path.resolve(parent) + , trueRoot = path.resolve(prefix, sub) + , things = {} ; - return new PromiseA(function (resolve) { - walker = walk.walk('posts'); - walker.on('directories', function (root, stat, next) { - console.log(root, stat); - next(); - }); - walker.on('files', function (root, stat, next) { - //console.log(root, stat); - next(); - }); - walker.on('end', function () { - console.log('done'); + return fs.lstatAsync(trueRoot).then(function (stat) { + var name = strip(prefix, trueRoot) + ; + + things[name] = things[name] || {}; + things[name].name = stat.name; + things[name].lastModifiedDate = stat.mtime.toISOString(); + things[name].contents = []; + + return new PromiseA(function (resolve) { + walker = walk.walk(trueRoot); + + walker.on('directories', function (root, stats, next) { + var dirname = strip(prefix, root) + ; + + stats.forEach(function (stat) { + var cdirname = path.join(dirname, stat.name) + ; + + things[cdirname] = things[cdirname] || {}; + things[cdirname].name = stat.name; + things[cdirname].lastModifiedDate = stat.mtime.toISOString(); + things[cdirname].contents = things[cdirname].contents || []; + }); + + next(); + }); + walker.on('directory', function (root, stat, next) { + var dirname = strip(prefix, path.join(root, stat.name)) + ; + + things[dirname] = things[dirname] || {}; + things[dirname].name = stat.name; + things[dirname].lastModifiedDate = stat.mtime.toISOString(); + things[dirname].contents = things[dirname].contents || []; + + next(); + }); + + walker.on('files', function (root, stats, next) { + var dirname = strip(prefix, root) + ; + + function eachFile(stat) { + var file + ; + + file = { + name: stat.name + , lastModifiedDate: stat.mtime.toISOString() + }; + + things[dirname].contents.push(file); + + if (opts.contents) { + return fs.readFileAsync(path.join(root, stat.name), 'utf8').then(function (contents) { + file.contents = contents; + }); + } + } + + if (!opts.contents) { + stats.forEach(eachFile); + next(); + } else { + forEachAsync(stats, eachFile).then(next); + } + }); + + walker.on('end', function () { + resolve(things); + }); }); }); } +*/ + +function walkDir(parent, sub, opts) { + opts = opts || {}; + + var prefix = path.resolve(parent) + , trueRoot = path.resolve(prefix, sub) + , files = [] + ; + + return new PromiseA(function (resolve) { + walker = walk.walk(trueRoot); + + walker.on('files', function (root, stats, next) { + var dirname = strip(prefix, root) + ; + + function eachFile(stat) { + var file + ; + + file = { + name: stat.name + , lastModifiedDate: stat.mtime.toISOString() + , size: stat.size + , path: dirname + }; + files.push(file); + + if (opts.contents) { + return fs.readFileAsync(path.join(root, stat.name), 'utf8').then(function (contents) { + file.contents = contents; + }); + } + } + + if (!opts.contents) { + stats.forEach(eachFile); + next(); + } else { + forEachAsync(stats, eachFile).then(next); + } + }); + + walker.on('end', function () { + resolve(files); + }); + }); +} + +function walkDirs(parent, subs, opts) { + opts = opts || {}; + + var collections = {} + ; + + return forEachAsync(subs, function (sub) { + return walkDir(parent, sub, opts).then(function (results) { + collections[sub] = results; + }); + }).then(function () { + return collections; + }); +} + + +/* +walkDirs('blog', ['posts'], { contents: false }).then(function (stats) { + console.log(JSON.stringify(stats, null, ' ')); +}); +*/ + +module.exports.walkDir = walkDir; +module.exports.walkDirs = walkDirs; diff --git a/package.json b/package.json index 783acb9..d1d502f 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,11 @@ "homepage": "https://github.com/coolaj86/deardesi", "dependencies": { "bluebird": "^2.5.3", + "body-parser": "^1.10.1", "circular-json": "^0.1.6", + "connect": "^3.3.3", + "connect-query": "^0.2.0", + "connect-send-json": "^1.0.0", "escape-string-regexp": "^1.0.2", "foreachasync": "^5.0.2", "json2yaml": "^1.0.3", @@ -40,6 +44,7 @@ "require-yaml": "0.0.1", "require-yamljs": "^1.0.1", "secret-utils": "^1.0.2", + "serve-static": "^1.7.2", "walk": "^2.3.5", "yaml": "^0.2.3", "yamljs": "^0.2.1" diff --git a/server.js b/server.js index e33f3b8..e8058b7 100644 --- a/server.js +++ b/server.js @@ -1,36 +1,93 @@ 'use strict'; -var connect = require('connect') +require('require-yaml'); + +var PromiseA = require('bluebird').Promise + , connect = require('connect') , query = require('connect-query') , bodyParser = require('body-parser') , serveStatic = require('serve-static') + , forEachAsync = require('foreachasync').forEachAsync + , send = require('connect-send-json') + , app = connect() + , walk = require('./lib/walk') + + , config = require('./config.yml') + , safeResolve = require('./lib/deardesi-utils').safeResolve + , path = require('path') + , blogdir = path.resolve(config.blogdir || __dirname) + , sha1sum = function (str) { return require('secret-utils').hashsum('sha1', str); } + , fs = PromiseA.promisifyAll(require('fs')) ; + app - .use('/api/fs', query()) - .use('/api/fs', bodyParser.json()) - .use('/api/fs', function (req, res, next) { + .use(send.json()) + .use(query()) + .use(bodyParser.json()) + + .use('/api/fs/files', function (req, res, next) { if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) { next(); return; } - /* - return forEachAsync(collectiondirs, function (collection) { - return fs.lstatAsync(collection.path).then(function (stat) { - if (!stat.isDirectory()) { - //return; - } + var filepaths = req.query.path && [req.query.path] || req.body.paths + , files = [] + ; - }).error(function () { + if (!filepaths || !filepaths.length) { + res.json({ error: "please specify req.query.path or req.body.paths" }); + return; + } + + return forEachAsync(filepaths, function (filepath) { + var pathname = safeResolve(blogdir, filepath) + ; + + return fs.lstatAsync(pathname).then(function (stat) { + return fs.readFileAsync(pathname, null).then(function (buffer) { + files.push({ + path: filepath + , size: buffer.byteLength + , lastModifiedDate: stat.mtime.toISOString() + , contents: buffer.toString('utf8') + , sha1: sha1sum(buffer) + , + }); + }); + }).catch(function (e) { + files.push({ path: filepath, error: e.message }); }); }).then(function () { + res.send(files); }); - */ - - res.end('not implemented'); }) + + .use('/api/fs/walk', function (req, res, next) { + if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) { + next(); + return; + } + + var dirnames = req.query.dir && [req.query.dir] || req.body.dirs + ; + + if (!dirnames || !dirnames.length) { + res.json({ error: "please specify req.query.dir or req.body.dirs" }); + return; + } + + walk.walkDirs(blogdir, dirnames, { contents: false }).then(function (stats) { + if (!req.body.dirs) { + res.json(stats[dirnames[0]]); + } else { + res.json(stats); + } + }); + }) + .use('/api/fs', function (req, res, next) { next(); return; @@ -39,3 +96,7 @@ app ; module.exports = app; + +require('http').createServer(app).listen(8080, function () { + console.log('listening 8080'); +}); diff --git a/twitter b/twitter deleted file mode 160000 index 65af77e..0000000 --- a/twitter +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 65af77ee6666044c2d0d5b5c4de719a37397daba