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.
MIT License
7k stars 680 forks source link

Migration guide from v2 to v3? #946

Closed SamTV12345 closed 11 months ago

SamTV12345 commented 11 months ago

Support plan

Context

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

SamTV12345 commented 11 months ago

This is the pull request we are working on: https://github.com/ether/etherpad-lite/pull/5796

GrosSacASac commented 11 months ago

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 https://github.com/node-formidable/formidable/issues/787#issuecomment-1612105698

SamTV12345 commented 11 months ago

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.

webzwo0i commented 11 months ago

@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..