Last active
November 12, 2017 09:04
-
-
Save epexa/a5d0b71e2bffdbcc8c260ac10079ec72 to your computer and use it in GitHub Desktop.
Replace images links to own domain on Golos Blockchain for Node.js
This file contains 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
// output post | |
res.json({ | |
created: result.created, | |
title: result.title, | |
body: imgUrlReplace(result.body) | |
}); | |
// replace all img links to own domain | |
function imgUrlReplace(url) { | |
/*return url.replace(/https:\/\/images.golos.io\/([a-zA-Z0-9]+)\/([0-9+]).jpg/g, function(match) { | |
return 'https://yoursite.com/images/' + match[0] + '_' + match[1] | |
});*/ | |
return url.replace(/https:\/\/images.golos.io\/([a-zA-Z0-9]+)\/([0-9+]).jpg/g, 'https://yoursite.com/images/$1_$2.jpg'); | |
}; | |
// proxy image to own domain https://yoursite.com/images/$1_$2.jpg | |
app.get('/images/:url', function(req, res) { | |
//req.params.url = 'https://images.golos.io/' + req.params.url.replace('_', '/'); | |
req.params.url = 'https://images.weserv.nl/?url=images.golos.io/' + req.params.url.replace('_', '/'); | |
https.get(req.params.url, function(response) { | |
var imageBuffer = new Buffer(parseInt(response.headers['content-length'])); | |
var bytes = 0; | |
response.setEncoding('binary'); | |
response.on('data', function(chunk) { | |
imageBuffer.write(chunk, bytes, 'binary'); | |
bytes += chunk.length; | |
}); | |
response.on('end', function() { | |
res.setHeader('Content-Type', 'image/jpg'); | |
res.send(imageBuffer); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment