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

multipartHandler and multipartFileHandler should have access to the incoming form. #1870

Open
3 tasks done
Iran-110 opened this issue Mar 15, 2021 · 0 comments
Open
3 tasks done

Comments

@Iran-110
Copy link

Iran-110 commented Mar 15, 2021

  • Used appropriate template for the issue type
  • Searched both open and closed issues for duplicates of this issue
  • Title adequately and concisely reflects the feature or the bug

Feature Request

Use Case

By adding the multipartFileHandler or multipartHandler to the bodyParser options, the ordinary process of formidable does not continue (the form.handlePart(part) would not be called anymore). So, we must write our handling function to save the incoming file for example.

server.use(restify.plugins.bodyParser({
  // other options
  // ...
  multipartFileHandler: function (part) {
    const {mime} = part;
    const fileType: string = mime.split('/').pop();
    if (fileType === 'jpg' || fileType === 'png' || fileType === 'jpeg') {
      part.on('data', buffer => {
        fs.writeFile(filePath, buffer, (err) => {
          if (err) {
            console.log(err);
          } else {
            console.log("The file was saved:", filename);
            // form.emit('file', part.name, theSavedFile);
          }
        });
      });
    } else {
      console.log('incorrect file type:', fileType);
    }
  },
  // ...
});

Besides, this method leaves the request.files to be an empty object {}. As we can not call the form.emit('file', part.name, theSavedFile) to ask the formidable: "the {[part.name]: theSavedFile} was successfully received, please add it the the files object".

Example API

We can easily run this snippet and save the file like a piece of cake.

server.use(restify.plugins.bodyParser({
        multiples: true,
        uploadDir,
        multipartFileHandler: function (part, req, form) { // <-- the third argument can be added
                const {mime} = part;
                const fileType: string = mime.split('/').pop();
                if (fileType === 'jpg' || fileType === 'png' || fileType === 'jpeg') {
                        form.handlePart(part); // <--- this line can be used.
                } else {
                        console.log('incorrect file type:', fileType);
                }
        }
})

Are you willing and able to implement this?

It's not so hard. However, I wrote a snippet to inject "form" into the functions.

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

No branches or pull requests

1 participant