restify / restify/node-restify

multipartHandler and multipartFileHandler should have access to the incoming form.

Open
#1,870 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.7k
Forks
975
Avg merge
1d 6h
Merged PRs (30d)
5

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the bodyParser integration for multipartFileHandler and multipartHandler, then trace how formidable receives each part and populates request.files. Confirm how the callback arguments can expose the incoming form while preserving default handling, and add coverage showing that supported parts are still recorded correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.