Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specific file names cause the extension to not be set correctly with options.keepExtensions "test(.123).pdf" #980

Open
spudcud opened this issue Oct 3, 2024 · 2 comments
Labels

Comments

@spudcud
Copy link

spudcud commented Oct 3, 2024

Support plan

  • Which support plan is this issue covered by? (Community, Sponsor, Enterprise): community
  • Currently blocking your project/work? (yes/no):
  • Affecting a production system? (yes/no): yes

Context

  • Node.js version: 18.18.0
  • Release Line of Formidable (Legacy, Current, Next): Current
  • Formidable exact version: 3.5.1
  • Environment (node, browser, native, OS): node
  • Used with (popular names of modules):

What are you trying to achieve or the steps to reproduce?

I am setting the option keepExtensions: true but when the file contains a period in the name with other characters it screws up the filePath created. It takes the first instance of the period and uses it instead of the final period. What is odd is I looked at using options.filename to get this corrected (which works) but the ext passed into the function is correctly '.pdf'.

import formidable from 'formidable';

const getFile = (req) => {
  return new Promise((resolve, reject) => {
    const form = formidable({
      keepExtensions: true,
    });

    form.parse(req, (err, fields, files) => {
      if (err) {
        return reject(err);
      }

      const { file: fileArr } = files;
      const file = fileArr[0];

      return resolve({
        path: file.filepath,
        fileName: file.originalFilename,
        uploadType: fields?.uploadType?.[0],
        docType: fields?.docType?.[0],
      });
    });
  });
};

export default getFile;

What was the result you got?

These are files we received from clients that caused issues processing:
'test(.123).pdf' - became '8316b5c96ab192694f1fd3a00.123'
'EERS 1.1-CUR.pdf' - became '8316b5c96ab192694f1fd3a01.1'

image

What result did you expect?

The filepath was expected to have the correct extension .pdf

@spudcud spudcud added the bug label Oct 3, 2024
@GrosSacASac
Copy link
Contributor

If the filename option is not provided the extension that is used will start at the first dot. This allows extensions such as x.test.js to be preserved. The characters allowed here are only numbers and ascii letters. That is why it cuts off at the first parentheses

When the filename option is provided then the filename function will be called with name, ext, part arguments and in this case the ext, name come from nodejs path.parse function. In that case both are not checked for invalid file characters. And the ext will be starting from the last dot and the name will be everything before.

Do you think the formidable library should change ?

In your case, you could overwrite _getExtension private to get a desired result.

@spudcud
Copy link
Author

spudcud commented Oct 25, 2024

In this specific case I would expect formidable to retain the extension. I have worked around this by setting the file manually but it took encountering these failures in production in order to see what the root cause was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants