Canceling saving uploaded files with Sails.js and Skipper

216 Views Asked by At

Is it possible to cancel file saving in the skipper upload function when we detect some constraints because of saving file should not be done?

I have a snippet of code like this:

req
  .file("multimedia")
  .upload({
    // Destination directory
    dirname : dirName,
    // Maximum image size in bytes
    maxBytes : MAX_IMG_SIZE,
    saveAs : function(__newFileStream, cb) {
      Blueprint.create(req, res, function(err, newInstance) {
        if (err) {
          // This call breaks down the application
          // This is the place where saving file should be canceled
          cb(err);
        } else {
          // Image name will be image Id
          cb(null, newInstance.id.toString());
        }
      });
    }
  },
  function whenDone(err, uploadedFiles) {
    if (err) {
      res.negotiate(err);
    } else {
      res.ok();
    }
  });

In the saveAs function I first save record in the database and then save the uploaded file with the name like id returned from the saved record. But if saving into database not succeed (for example because of validation errors) how can I cancel saving file on the disk and pass that error further?

0

There are 0 best solutions below