Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading image on server from URL #140

Closed
manuscript80 opened this issue Aug 15, 2016 · 3 comments
Closed

Loading image on server from URL #140

manuscript80 opened this issue Aug 15, 2016 · 3 comments
Labels

Comments

@manuscript80
Copy link

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
Copy link
Owner

vsivsi commented Aug 15, 2016

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
Copy link
Author

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 #140 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AQPIW0kd8UEmfxfiXO9l2jJDwwf_bYK7ks5qgPILgaJpZM4JkCeT.

@vsivsi
Copy link
Owner

vsivsi commented Aug 22, 2016

Great!

@vsivsi vsivsi closed this as completed Aug 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants