Save video file received from multipart form in Serverless Offline

1.1k Views Asked by At

I have an website running Angular4 with a simple form uploading data using ng2-file-upload. I'm sending those files to a Node.js-based serverless offline server where my intention is to simply write those files received from the form to disk.

I tried to do in many different ways, and in the end I found this right here that parses that form from the event into a json. The resulting json contains a buffer in one of the fields with the video data like so:

{ Host: 'localhost:3000',
  'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0',
  Accept: '*/*',
  'Accept-Language': 'en-US,en;q=0.5',
  'Accept-Encoding': 'gzip, deflate',
  Referer: 'http://localhost:4200/myupload',
  'Content-Length': 2391623,
  'Content-Type': 'multipart/form-data; boundary=---------------------------2125290100942661667805976894',
  Origin: 'http://localhost:4200',
  Connection: 'keep-alive' }
{ file:
   { type: 'file',
     filename: 'y9K18CGEeiI.webm',
     contentType: 'video/webm',
     content: <Buffer 1a 45 e3 01 00 00 00 00 00 00 1f 42 fd fd 01 42 fd fd 01 42 fd 04 42 fd 08 42 fd fd 77 65 62 6d 42 fd fd 02 42 fd fd 02 18 53 fd 67 01 00 00 00 00 14 ... > } }

Now what I'm trying to do is to save the file in the buffer using fs:

module.exports.handler = (event, context, callback) => {
    let data = multipart.parse(event, false);

    fs.writeFile('meme.webm', data.file.content, 'binary', function(err) {
        if(err) { 
          console.log(err);
        } else {  
          console.log('saved!');
        }
    }); 

    // etc ...
};

The file saves to the disk with the correct size (1.3MB), same as the original file. Unfortunately, I can't seem to open it on the other side and I assume it's either because othe encoding or because of the way I'm writing it to disk. Any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

For anyone with this problem, check this issue right here. It's a problem with serverless offline converting file data and there's not much that can be done it seems other than applying the fork.