Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Created February 24, 2017 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chtzvt/b0a840921506c45fc6dc0eb065a8bd9c to your computer and use it in GitHub Desktop.
Save chtzvt/b0a840921506c45fc6dc0eb065a8bd9c to your computer and use it in GitHub Desktop.
Google Cloud Function which, when given an Imgur album ID, this will serve a proxied version via imgurProxy.js.
var request = require('request');
var IMGUR_API_KEY = ' ';
var PROXY_URL = 'us-central1-[...].cloudfunctions.net';
exports.album = function album (req, res) {
if (req.path === undefined || req.path.length <= 1) {
res.status(400).send('{"error":"No Data"}');
return;
}
var params = {
url: 'https://api.imgur.com/3/album/'+req.path,
headers: {
"Authorization": 'Client-ID ' + IMGUR_API_KEY
}
};
request.get(params, function(err, response, data){
if(err){
res.status(500).send('{"error":"Couldn\'t connect to Imgur"}');
throw new Error("Failed to read Imgur API response: " + JSON.parse(err));
}
var imgurRes = JSON.parse(response.body);
var images = imgurRes.data.images;
if(imgurRes.data.error || typeof images[0].link === 'undefined' || images[0].link.indexOf('http') === -1)
res.status(500).send('{"error":"API error or image not found"}');
var responseHTML = "<head><title>Imgur Album - "+req.path+"</title><link href='https://www.ctis.me/projects/imgurProxy/style.css' rel='stylesheet' type='text/css'></head><body><div class='imagesContainer'>";
for(var i = 0; i < images.length; i++){
responseHTML += "<div id='image"+i+"' class='imagesContainer image'><img src='https://" + PROXY_URL + "/fetchImage/"+images[i].id+"'/></div>";
}
responseHTML += "</div></body></html>";
res.send(responseHTML);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment