Skip to content

Instantly share code, notes, and snippets.

@nshalman
Last active December 24, 2015 16:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nshalman/6827498 to your computer and use it in GitHub Desktop.
Save nshalman/6827498 to your computer and use it in GitHub Desktop.
Manta Explorer
#!/usr/bin/env node
var restify = require('restify');
var path = require('path');
var fs = require('fs');
var assert = require('assert');
var manta = require('manta');
var client = manta.createClient({
sign: manta.privateKeySigner({
key: fs.readFileSync(process.env.HOME + '/.ssh/id_rsa', 'utf8'),
keyId: process.env.MANTA_KEY_ID,
user: process.env.MANTA_USER
}),
user: process.env.MANTA_USER,
url: process.env.MANTA_URL
});
assert.ok(client);
console.log('client setup: %s', client.toString());
function respond(req, to_client, next) {
var the_path = req.params[0] + '/' ;
client.ls(the_path, function (err, res) {
assert.ifError(err);
to_client.write('<html>\n<head><title>Manta Explorer</title></head>\n<body>\n');
to_client.write('<table>\n');
res.on('object', function (obj) {
to_client.write("<tr><td>o</td><td><a href=" + process.env.MANTA_URL + the_path + obj.name + ">" + obj.name + "</a></td><td>" + obj.mtime + "</td><td>" + obj.size + "</td></tr>\n");
});
res.on('directory', function (dir) {
to_client.write("<tr><td>d</td><td><a href=" + the_path + dir.name + ">" + dir.name + "/</a></td><td>" + dir.mtime + "</td></tr>\n");
});
res.once('error', function (err) {
to_client.write("<pre>" + err.stack);
to_client.write('</pre>\n</body>\n</html>\n');
to_client.end();
return next();
});
res.once('end', function () {
to_client.write('</table>\n</body>\n</html>\n');
to_client.end();
return next();
});
});
}
function favicon(req, res, next) {
res.header('Location', 'http://www.joyent.com/favicon.ico');
res.send(302);
return next(false);
}
server = restify.createServer();
server.get('/favicon.ico', favicon);
server.head('/favicon.ico', favicon);
server.get(/(.*)/, respond);
server.head(/(.*)/, respond);
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
@mcavage
Copy link

mcavage commented Oct 4, 2013

Nice!

Slight tweak to sign urls (so you can click objects):

#!/usr/bin/env node

var restify = require('restify');
var    path = require('path');
var      fs = require('fs');
var assert  = require('assert');
var manta   = require('manta');

var client = manta.createClient({
    sign: manta.privateKeySigner({
        key: fs.readFileSync(process.env.HOME + '/.ssh/id_rsa', 'utf8'),
        keyId: process.env.MANTA_KEY_ID,
        user: process.env.MANTA_USER
    }),
    user: process.env.MANTA_USER,
    url: process.env.MANTA_URL
});
assert.ok(client);

console.log('client setup: %s', client.toString());

function respond(req, to_client, next) {
    var the_path = req.params[0] + '/' ;
    client.ls(the_path, function (err, res) {
        assert.ifError(err);

        to_client.write('<html>\n<head><title>Manta Explorer</title></head>\n<body>\n');
        to_client.write('<table>\n');

        var pending = 0;
        res.on('object', function (obj) {
            pending++;
            client.signURL({
                path: the_path + obj.name
            }, function (err2, sig) {
                assert.ifError(err2);
                var link = process.env.MANTA_URL + sig;
                to_client.write("<tr><td>o</td><td><a href=" + link + ">" + obj.name + "</a></td><td>" + obj.mtime + "</td><td>"  + obj.size + "</td></tr>\n");
            });
        });

        res.on('directory', function (dir) {
            to_client.write("<tr><td>d</td><td><a href=" + the_path + dir.name + ">" + dir.name + "/</a></td><td>" + dir.mtime + "</td></tr>\n");
        });

        res.once('error', function (err) {
            to_client.write("<pre>" + err.stack);
            to_client.write('</pre>\n</body>\n</html>\n');
            to_client.end();
            next();
        });

        res.once('end', function () {
            function done() {
                to_client.write('</table>\n</body>\n</html>\n');
                to_client.end();
                next();
            }

            (function poll() {
                if (pending === 0) {
                    done();
                } else {
                    setTimeout(poll, 50);
                }
            })();
        });

    });
}

function favicon(req, res, next) {
    res.header('Location', 'http://www.joyent.com/favicon.ico');
    res.send(302);
    return next(false);
}

server = restify.createServer();
server.get('/favicon.ico', favicon);
server.head('/favicon.ico', favicon);
server.get(/(.*)/, respond);
server.head(/(.*)/, respond);

server.listen(8080, function() {
    console.log('%s listening at %s', server.name, server.url);
});

@nshalman
Copy link
Author

nshalman commented Oct 4, 2013

That one should probably only be allowed to listen on 127.0.0.1 so that you're not exposing you /stor to the world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment