Mat Fluor
Mat Fluor

Reputation: 466

Meteor: Downloading a video file via an API and save it to disk

I'm trying to achieve the following:

But I'm helplessly stuck - the video I'm trying to download is always invalid (and the wrong size).

This is my Server method:

getVideo: function() {
  var fs = Npm.require('fs');
  var response = HTTP.get('http://webserver.com/getvideo');
  fs.writeFile('file.mp4',response.content, function (err) {
  if (err) throw err;
    console.log('It\'s saved!');
  });
}

Other things I've tried and render the basically same results:

With createWriteStream:

var file = fs.createWriteStream('file.mp4');
var response = HTTP.get('http://webserver.com/getvideo');
response.content.pipe(file);

with Buffer():

var buffer = new Buffer(response.content)
fs.writeFile('file.mp4', buffer)

And also with different encodings: base64, binary

The Webserver (written in flask) responds like this:

{
  content: ........00+ilst\u0000#�too\u001bdata\u0000\u0000\u0000\u0000Lavf52.48.0',
  headers: 
    { 'content-disposition': 'attachment; filename=file.mp4',
      'content-length': '632310',
      'content-type': 'video/mp4',
      'last-modified': 'Fri, 08 Jan 2016 00:45:49 GMT',
      'cache-control': 'public, max-age=43200',
      expires: 'Fri, 08 Jan 2016 20:21:08 GMT',
      etag: '"flask-1452213949.65-632310-220532295"',
      server: 'Werkzeug/0.11.3 Python/2.7.11',
      date: 'Fri, 08 Jan 2016 08:21:08 GMT' },
   data: null }

Tools like curl or a webbrowser although can download (and put to disk) the file correctly, what am I missing?

Upvotes: 1

Views: 906

Answers (1)

Mat Fluor
Mat Fluor

Reputation: 466

I managed to make it work.

As far as I've seen the content get's encoded, so I need to remove that encoding.

As stated on [meteor][0.6.*] With meteorjs, how to download file with Meteor.http? on the far bottom:

The encoding option of meteor HTTP is a sub option of npmRequestOptions, so HTTP.get(url, {npmRequestOptions: {encoding:null}} which finally allowed me to get binary files like jpg – Bob Siefkes Nov 7 '15 at 18:48

Thanks to lanmower on #meteor for helping me.

Upvotes: 3

Related Questions