Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Insert from a Buffer on the Server

Philipp edited this page Mar 1, 2015 · 3 revisions

In certain circumstances, it might be necessary to perform inserts on the server using data from a Buffer object. Here's an example:

var request = Meteor.require('request');

request.get({url: url, encoding: null}, Meteor.bindEnvironment(function(e, r, buffer){
  var newFile = new FS.File();
  newFile.attachData(buffer, {type: 'image/png'}, function(error){
    if(error) throw error;
    newFile.name('myGraphic.png');
    
    Images.insert(newFile);
  });
})).auth(null, null, true, accessToken);

When calling attachData with a Buffer, you must provide the MIME type of the file through the second argument as shown above. Omitting this argument yields an exception.

Further examples can be found here.

Caveats:

  • Downloading large files into a Buffer could potentially lead to memory issues. Use the method described in "Insert from a Stream on the Server" if possible.
  • Use of external HTTP library to make authenticated requests will be unnecessary once issue 350 is resolved.