如何将客户端的额外数据传递给slingshot s3storage?

时间:2016-08-15 02:54:19

标签: javascript meteor meteor-slingshot

我正在尝试将用户ID从FlowRouter.getParam('id');传递到服务器,以将文件上传到亚马逊。这是一个管理员帐户,因此我使用FlowRouter.getParam('id');来访问正确用户的个人资料信息。问题是我没有正确地传递id,所以这一切只是错误并停止工作。

如何正确传递ID?

路径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 );
    }
  });
};

路径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;
  }
});

1 个答案:

答案 0 :(得分:2)

首先,为了获取当前用户的id,您应该在this.userId方法中使用服务器上的authorize,而不是简单地信任客户端传递的数据(确定用户实际上是管理员并验证参数。)

添加到上传中的meta-context应该是一个对象(您传递一个字符串),它可以作为指令方法的第二个参数。

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

在服务器上,您的指令的方法会获得您传递的filemeta

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;
  }
});