retrieving files and folders

This commit is contained in:
AJ ONeal 2015-01-05 22:05:35 +00:00
parent 9221fd8457
commit 3b19c4e877
5 changed files with 240 additions and 33 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "twitter"]
path = twitter
url = git@github.com:coolaj86/twitter.git

View File

@ -1,28 +1,173 @@
'use strict'; 'use strict';
var PromiseA = require('bluebird').Promise var PromiseA = require('bluebird').Promise
, fs = PromiseA.promisifyAll(require('fs'))
, forEachAsync = require('foreachasync').forEachAsync
, path = require('path') , path = require('path')
, walk = require('walk') , walk = require('walk')
, walker , walker
; ;
function getFs(parent, sub) { function strip(prefix, pathname) {
// TODO safe return pathname.substr(prefix.length + 1);
var trueRoot = path.resolve(parent, sub) }
/*
function walkDir(parent, sub, opts) {
opts = opts || {};
var prefix = path.resolve(parent)
, trueRoot = path.resolve(prefix, sub)
, things = {}
; ;
return new PromiseA(function (resolve) { return fs.lstatAsync(trueRoot).then(function (stat) {
walker = walk.walk('posts'); var name = strip(prefix, trueRoot)
walker.on('directories', function (root, stat, next) { ;
console.log(root, stat);
next(); things[name] = things[name] || {};
}); things[name].name = stat.name;
walker.on('files', function (root, stat, next) { things[name].lastModifiedDate = stat.mtime.toISOString();
//console.log(root, stat); things[name].contents = [];
next();
}); return new PromiseA(function (resolve) {
walker.on('end', function () { walker = walk.walk(trueRoot);
console.log('done');
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;

View File

@ -30,7 +30,11 @@
"homepage": "https://github.com/coolaj86/deardesi", "homepage": "https://github.com/coolaj86/deardesi",
"dependencies": { "dependencies": {
"bluebird": "^2.5.3", "bluebird": "^2.5.3",
"body-parser": "^1.10.1",
"circular-json": "^0.1.6", "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", "escape-string-regexp": "^1.0.2",
"foreachasync": "^5.0.2", "foreachasync": "^5.0.2",
"json2yaml": "^1.0.3", "json2yaml": "^1.0.3",
@ -40,6 +44,7 @@
"require-yaml": "0.0.1", "require-yaml": "0.0.1",
"require-yamljs": "^1.0.1", "require-yamljs": "^1.0.1",
"secret-utils": "^1.0.2", "secret-utils": "^1.0.2",
"serve-static": "^1.7.2",
"walk": "^2.3.5", "walk": "^2.3.5",
"yaml": "^0.2.3", "yaml": "^0.2.3",
"yamljs": "^0.2.1" "yamljs": "^0.2.1"

View File

@ -1,36 +1,93 @@
'use strict'; 'use strict';
var connect = require('connect') require('require-yaml');
var PromiseA = require('bluebird').Promise
, connect = require('connect')
, query = require('connect-query') , query = require('connect-query')
, bodyParser = require('body-parser') , bodyParser = require('body-parser')
, serveStatic = require('serve-static') , serveStatic = require('serve-static')
, forEachAsync = require('foreachasync').forEachAsync
, send = require('connect-send-json')
, app = connect() , 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 app
.use('/api/fs', query()) .use(send.json())
.use('/api/fs', bodyParser.json()) .use(query())
.use('/api/fs', function (req, res, next) { .use(bodyParser.json())
.use('/api/fs/files', function (req, res, next) {
if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) { if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) {
next(); next();
return; return;
} }
/* var filepaths = req.query.path && [req.query.path] || req.body.paths
return forEachAsync(collectiondirs, function (collection) { , files = []
return fs.lstatAsync(collection.path).then(function (stat) { ;
if (!stat.isDirectory()) {
//return;
}
}).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 () { }).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) { .use('/api/fs', function (req, res, next) {
next(); next();
return; return;
@ -39,3 +96,7 @@ app
; ;
module.exports = app; module.exports = app;
require('http').createServer(app).listen(8080, function () {
console.log('listening 8080');
});

@ -1 +0,0 @@
Subproject commit 65af77ee6666044c2d0d5b5c4de719a37397daba