i'm i'm missing obvious, gist of problem i'm receiving png mapbox call intent of writing file system , serving client. i've relayed call, received response of raw data , written file. problem file ends truncated no matter path take, , i've exhausted answers i've found skirting subject. i've dumped raw response log, , it's robust, file make tends chunk's worth of unreadable data.
here's code i've got @ present file making. tried buffer move last ditch after several failed , comparably fruitless iterations. appreciated.
module.exports = function(req, res, cb) { var cartography = function() { return https.get({ hostname: 'api.mapbox.com', path: '/v4/mapbox.wheatpaste/' + req.body[0] + ',' + req.body[1] + ',6/750x350.png?access_token=' + process.env.mapbox_api }, function(res) { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { var mappath = 'map' + req.body[0] + req.body[1] + '.png'; var map = new buffer(body, 'base64'); fs.writefile(__dirname + '/client/images/maps/' + mappath, map, 'base64', function(err) { if (err) throw err; cb(mappath); }) }) }); }; cartography(); };
it possible rewrite code in more compact subroutine:
const fs = require('fs'); const https = require('https'); https.get(url, (response)=> { //request if(response) { let imagename = 'image.png'; // purpose use crypto response.pipe( //pipe response write stream (file) fs.createwritestream( //create write stream './public/' + imagename //create file name image.png ) ); return imagename; //if public folder set default in app.js } else { return false; } })
you original name , extension url, safer generate new name crypto , file extension said url or read-chunk , file-type modules.
Comments
Post a Comment