Created
July 11, 2012 18:57
-
-
Save vstarck/3092369 to your computer and use it in GitHub Desktop.
base64.proxy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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