Search code examples
node.jssails.jswaterline

How to upload a file using the new controller actions format in sails.js


Someone know how I could upload a file to a controller action using the (inputs, exists) format of sails.js, this is my try:

  module.exports = {


  friendlyName: 'Upload file recording',


  description: 'Upload a recording to AWS',

  inputs: {
    name: {
      required: true,
      type: 'string'
    },
    mimeType: {
      required: true,
      type: 'string'
    },
    recording: {
      type: 'ref'
    }
  },

  exits: {

    noRecordings: {
      responseType: 'no recordings',
      description: `You don't have any recording for this event`,
    },
    serverError: {
      responseType: 'server error',
      description: `Failed to upload the file`,
    }

  },


  fn: async function (inputs, exits) {

    //  fixme    
    inputs.file('recording').upload({
      adapter: require('skipper-s3'),
      key: process.env.AWS_S3_KEY,
      secret: process.env.AWS_S3_SECRET,
      bucket: process.env.AWS_S3_BUCKETNAME
    }, function (err, filesUploaded) {
      if (err) return exits.serverError(err);
      return exits.success({
        files: filesUploaded,
        textParams: req.allParams()
      });
    });

  }
};

The upload files because the inputs.file is not a function. So basically the question is, how can I pass a file here.

Any help is appreciated.


Solution

  • So finally found the solution to that, I just need to use:

    files: ['recording'],
    

    in addition to:

    inputs:
     recording: {
          example: '===',
          required: true
     },
    

    And then inside the action function:

    inputs.recording.upload({
          adapter: require('skipper-s3'),
          key: process.env.AWS_S3_KEY,
          secret: process.env.AWS_S3_SECRET,
          bucket: process.env.AWS_S3_BUCKETNAME
        }, function (err, filesUploaded) {
          if (err) return exits.serverError(err);
          console.log(filesUploaded);
          return exits.success(newFileRecording);
        });