Search code examples
node.jsexpressnext.jsfile-uploadmulter

NodeJs Use Multer as a middleware to parse files and conditionally initiate storage


I'm using NextJS endpoint to receive fromData with file/files

I need to parse the file's data and use metadata to decide the file name in the Multer storage.

For example, based on the file name, I would like to create an entry in the DB for Users "profile" and use that Entry ID for the file path in Multer storage (Upladoing to GCP bucket)

What I need is a way for Multer to let me have access to the file data before initiating the storage object - in which I need to provide the file's destination before it's known to me.

I tried:

  1. Creating two Multer middlewares, the first one has no storage object configured, and the second one has it.
  • Problem: I'm getting the error "multer Unexpected end of form" when the data is going out to the remote destination. - seems like it was ruining its own file parsing
  1. Using Multer alongside formidable
  • Problem: formidable with hoog the files from the response, and I started going down the rabbit hall of manually adding the files key to the req object so Mulert could upload it. But Multer kept overwriting it with an empty array
  1. Tinkering a bit with the callback function from the destination, but it seems like I can't have the string calculated at the time of upload, just at initialization

Solutions in Express.JS will be easily understood if you have a solution, not for Next.Js I'


Solution

  • For anyone who might be stuck with a similar issue. I can't recommend adding using a combination of middlewares to process the form data.

    But something that worked for me is to send the data I needed in the request's query string.

    It ain't pretty, but it works and saves a lot of time to find another way to submit the file's data