bp123
bp123

Reputation: 3417

How to pass extra data from the client over to slingshot s3storage?

I'm trying to pass the user id from FlowRouter.getParam('id'); to the server to upload a file into amazon. This is an admin account so I'm using the FlowRouter.getParam('id'); to access the correct user's profile information. The problem is I'm not correctly passing the id over so it all just errors and stops working.

How do I pass the id over correctly?

Path uploadFile.js

let _uploadFileToAmazon = ( file ) => {
  var id = FlowRouter.getParam('id');
  const uploader = new Slingshot.Upload( "uploadProfileImgAdmin", id );
  uploader.send( (file), ( error, url ) => {
    if ( error ) {
      Bert.alert( error.message, "warning" );
      _setPlaceholderText();
    } else {
      _addUrlToDatabase( url );
    }
  });
};

Path server/uploadFile.js

Slingshot.createDirective( "uploadProfileImgAdmin", Slingshot.S3Storage, {
  bucket: "bhr-app",
  region: "ap-southeast-2",
  acl: "public-read",
  authorize: function (id) {
    console.log("user id: ", id);
    return Files.findOne( { "userId": id } );
  },
  key: function ( file ) {
    var user = Meteor.users.findOne( _id: id );

    return "profile-images" + "/" + user.emails[0].address + "/" + file.name;
  }
});

Upvotes: 0

Views: 140

Answers (1)

MasterAM
MasterAM

Reputation: 16478

First of all, in order to get the current user's id, you should use this.userId on the server in the authorize method and don't simply trust the data passed by the client (to make sure that the user is, in fact, an admin and validate the parameters).

The meta-context added to the upload should be an object (you are passing a string) and it is available as the second parameter of your directive methods.

const uploader = new Slingshot.Upload("uploadProfileImgAdmin", {id});

And on the server, your directive's methods get the file and the meta you passed:

Slingshot.createDirective( "uploadProfileImgAdmin", Slingshot.S3Storage, {
  bucket: "bhr-app",
  region: "ap-southeast-2",
  acl: "public-read",
  authorize: function (file, meta) {
    console.log("user id: ", meta.id);
    // validate meta, make sure that the user is an admin and 
    // return a Boolean or throw an error
  },
  key: function (file, meta) {
    var user = Meteor.users.findOne(meta.id);
    return "profile-images" + "/" + user.emails[0].address + "/" + file.name;
  }
});

Upvotes: 2

Related Questions