Search code examples
node.jsexpressformidable

how to make formidable not save to var/folders on nodejs and express app


I'm using formidable to parse incoming files and store them on AWS S3

When I was debugging the code I found out that formidable is first saving it to disk at /var/folders/ and overtime some unnecessary files are stacked up on disk which could lead to a big problem later.

It's very silly of me using a code without fully understanding it and now

I have to figure out how to either remove the parsed file after saving it to S3 or save it to s3 without storing it in disk. But the question is how do I do it?

I would appreciate if someone could point me in the right direction

this is how i handle the files:

import formidable, { Files, Fields } from 'formidable';

const form = new formidable.IncomingForm();
form.parse(req, async (err: any, fields: Fields, files: Files) => {
let uploadUrl = await util
            .uploadToS3({
                file: files.uploadFile,
                pathName: 'myPathName/inS3',
                fileKeyName: 'file',
            })
            .catch((err) => console.log('S3 error =>', err));
}

Solution

  • This is how i solved this problem:

    When I parse incoming form-multipart data I have access to all the details of the files. Because it's already parsed and saved to local disk on the server/my computer. So using the path variable given to me by formidable I unlink/remove that file using node's built-in fs.unlink function. Of course I remove the file after saving it to AWS S3.

    This is the code:

    import fs from 'fs';
    import formidable, { Files, Fields } from 'formidable';
    
    const form = new formidable.IncomingForm();
    form.multiples = true;
    
    form.parse(req, async (err: any, fields: Fields, files: Files) => {
       const pathArray = [];
       try {
          const s3Url = await util.uploadToS3(files);
          // do something with the s3Url
          pathArray.push(files.uploadFileName.path);
       } catch(error) {
          console.log(error)
       } finally {
          pathArray.forEach((element: string) => {
             fs.unlink(element, (err: any) => {
                if (err) console.error('error:',err);
                });
            });
       }
    })
    

    I also found a solution which you can take a look at here but due to the architecture if found it slightly hard to implement without changing my original code (or let's just say I didn't fully understand the given implementation)