node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Migration guide from v2 to v3?

SamTV12345 opened this issue · comments

Support plan

  • which support plan is this issue covered by? (e.g. Community, Sponsor, or
    Enterprise): Community
  • is this issue currently blocking your project? (no):
  • is this issue affecting a production system? (no):

Context

  • node version: v20.2.0
  • module (formidable) version: 2.1.2 to 3.5.0
  • environment (e.g. node, browser, native, OS):
  • used with (i.e. popular names of modules): etherpad
  • any other relevant information:

What problem are you trying to solve?

We tried to update to the next major version as v2 is going to be deprecated.

We don't use Formidable very extensive but we have a few occurrences where the user can upload a pad. The problem is that in that case the file is not updated and stays the same.

const {Formidable} = require('formidable');

...

  const form = new Formidable({
    keepExtensions: true,
    uploadDir: tmpDirectory,
    maxFileSize: settings.importMaxFileSize,
  });

  // locally wrapped Promise, since form.parse requires a callback
  let srcFile = await new Promise((resolve, reject) => {
    form.parse(req, (err, fields, files) => {
      if (err != null) {
        logger.warn(`Import failed due to form error: ${err.stack || err}`);
        // I hate doing indexOf here but I can't see anything to use...
        if (err && err.stack && err.stack.indexOf('maxFileSize') !== -1) {
          return reject(new ImportError('maxFileSize'));
        }
        return reject(new ImportError('uploadFailed'));
      }
      if (!files.file) {
        logger.warn('Import failed because form had no file');
        return reject(new ImportError('uploadFailed'));
      }
      resolve(files.file[0].filepath);
    });
  });

Do you have a new or modified API suggestion to solve the problem?

No

This is the pull request we are working on: ether/etherpad-lite#5796

You can directly compare err.code with formidableErrors.biggerThanMaxFileSize

no need to wrap form.parse in a promise. if no callback is provided it returns a promise

make sure uploadDir exists before form.parse is called

For the rest see #787 (comment)

It is still erroring out. I have no idea why. We have a test that updates the text of a pad but it stays the same without updating it.

@GrosSacASac Thanks for the pointers regarding error handling & promise. Apart from the code above, we use formidable for parsing fields in our API. When using multipart/form-data, the fields values are Arrays now - but iirc that is already documented in the Changelog..