vsivsi / meteor-file-collection

Extends Meteor Collections to handle file data using MongoDB gridFS.
http://atmospherejs.com/vsivsi/file-collection
Other
159 stars 37 forks source link

Loading image on server from URL #140

Closed manuscript80 closed 7 years ago

manuscript80 commented 7 years ago

I haven't been able to find any examples of loading an image from a URL while on the server. I've seen the recommendations to use a PUT HTTP request, but it seems really wasteful to download the file from a URL and then send it to Resumable.js. Here's what's I've been trying...

Meteor.http.get(media.url, {npmRequestOptions: { encoding: 'base64' }}, function(e, res) {
  var stream = MediaDocs.upsertStream(
      {
         _id: new Meteor.Collection.ObjectID(),
         filename: media.filename,
         contentType: 'image/' + media.extension
      },
      function (err, fileObj) {
         if (err) {
             console.log(err);
         } else {
             console.log("FileObj: ", fileObj);
         }
      }
   );
   stream.end(res.content);
});

My console output always shows a length of 765031 for fileObj, regardless of the actual content size. Should I be using a different encoding? Or is there a better way to save an image file from the server?

vsivsi commented 7 years ago

Hi, rather than use the Meteor HTTP calls, I'd just use the npm request package, which will return a readable stream that you can pipe to the result of fc.upsertStream()

So something more like the code below will enable you to efficiently handle retrieving large files and writing them into the fileCollection:

var request = require('request');

var stream = MediaDocs.upsertStream(
      {
         _id: new Meteor.Collection.ObjectID(),
         filename: media.filename,
         contentType: 'image/' + media.extension
      },
      function (err, fileObj) {
         if (err) {
             console.log(err);
         } else {
             console.log("FileObj: ", fileObj);
         }
      }
   );

stream.on('finish', function () { /* Perform any actions after write... */);

request(media.url)
   .on('error', function(err) { /* Handle error */ })
   .pipe(stream);
manuscript80 commented 7 years ago

That did the trick, thank you. What a flexible package!

On Aug 15, 2016, at 7:11 PM, Vaughn Iverson notifications@github.com wrote:

Hi, rather than use the Meteor HTTP calls, I'd just use the npm request package https://www.npmjs.com/package/request#streaming, which will return a readable stream that you can pipe to the result of fc.upsertStream()

So something more like the code below will enable you to efficiently handle retrieving large files and writing them into the fileCollection:

var request = require('request');

var stream = MediaDocs.upsertStream( { _id: new Meteor.Collection.ObjectID(), filename: media.filename, contentType: 'image/' + media.extension }, function (err, fileObj) { if (err) { console.log(err); } else { console.log("FileObj: ", fileObj); } } );

stream.on('finish', function () { /* Perform any actions after write... */);

request(media.url) .on('error', function(err) { /* Handle error */ }) .pipe(stream); — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vsivsi/meteor-file-collection/issues/140#issuecomment-239956416, or mute the thread https://github.com/notifications/unsubscribe-auth/AQPIW0kd8UEmfxfiXO9l2jJDwwf_bYK7ks5qgPILgaJpZM4JkCeT.

vsivsi commented 7 years ago

Great!