Skip to content

Instantly share code, notes, and snippets.

@vstarck
Created July 11, 2012 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vstarck/3092369 to your computer and use it in GitHub Desktop.
Save vstarck/3092369 to your computer and use it in GitHub Desktop.
base64.proxy.js
var express = require("express");
var request = require("request");
var app = express.createServer(express.logger(), express.bodyParser());
app.get("/batch", function(req, res) {
if(!req.param("data")) return;
var data = req.param("data");
var callback = req.param("callback") || 'callback';
if(!Array.isArray(data)) {
data = [data];
}
var pending = data.length;
var images = Object.create(null);
data.forEach(function(current, index) {
request({ uri: decodeURIComponent(current), encoding: 'binary'}, function(error, response, body) {
var prefix, image;
if(!error && response.statusCode == 200) {
prefix = "data:" + response.headers["content-type"] + ";base64,";
image = new Buffer(body.toString(), "binary").toString("base64");
images[current] = prefix + image;
} else {
images[current] = null;
}
if(!--pending) {
res.send(callback + '(' + JSON.stringify(images) + ');');
}
});
});
});
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment