1

I have NodeJS restify setup and I'm using mongoose-attachments to attach images to my user model, and storing the images on an S3 bucket.

I'm also allowing users to signup using Facebook, Google etc using Passport.JS.

The problem is that mongoose-attachments expects a local file reference when calling the .attach() function, and PassportJS gives a remote URL - so I need to download the image and then attach it from tmp.

How should I approach this with NodeJS? Is there a good module I can use for this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Routhinator
  • 3,559
  • 4
  • 24
  • 35

2 Answers2

1

I managed to find a working solution to this, via the request module. It does what I need and it seems to be a well rounded tool for any web app. This was the code that worked:

var userImageProp = {};
if ((profile.picture) && (profile.picture.data.is_silhouette == false)) {

    var pictureURL = 'https://graph.facebook.com/'+ profile.id +'/picture?type=large';    

    // Determine file name.
    var filename = profile.picture.data.url.replace(/^.*[\\\/]/, '');

    // Precreate stream and define callback.
    var picStream = fs.createWriteStream('/tmp/'+filename);
    picStream.on('close', function() {
        console.log('Downloaded '+filename);
        userImageProp.path = '/tmp/'+filename;
        finishSave(user, userImageProp);
    });

    // Get and save file.
    request(pictureURL).pipe(picStream);

} else {
    userImageProp.path = config.root + '/defaults/img/faceless_'+user.gender.toLowerCase()+'.png';
    finishSave(user, userImageProp);
}

function finishSave(user, userImageProp) {
    user.attach('userImage', userImageProp, function(err) {
        console.dir(err);
        if (err) { return done(new restify.InternalError(err)); }
        user.save(function (err, user) {
            if (err) { return done(new restify.InternalError(err)); }
            // Saved successfully. Return user for login, and forward client to complete user creation.
            return done(null, user, '/#/sign-up/facebook/save');
        });
    });
}

Credit to these threads for helping me come up with this solution:

Community
  • 1
  • 1
Routhinator
  • 3,559
  • 4
  • 24
  • 35
0

have you look at mongoose-attachments-aws2js

wayne
  • 3,410
  • 19
  • 11