Accepting User-Contributed Image Uploads

From wiki.gpii
Jump to: navigation, search

Introduction

In preparing to add image support to the Unified Listing, we need to evaluate the options for handling file uploads within a REST API.

Assumptions

It needs to:

  1. Accept file content encoded using multipart/form encoding.
  2. Refuse to accept overly large files.
  3. Refuse to read an overly large amount of field data into memory.
  4. Refuse to accept unacceptable file types.

By "refuse to accept", I mean, reject the upload without attempting to save the content to disk.

Candidates

I started with the express documentation, which mentions a range of candidates:

  1. busboy
  2. multiparty
  3. formidable
  4. multer

Of those, I almost immediately gravitated towards multer because I could see clearly from the docs that they at least intend for people to have a lot of control over the way it works from the outside. For example, check out the amount of control multer gives when choosing what content to accept. You can either use declarative options to configure it, or if that's too limiting, you can wire in your own filter function. They also have clear docs on replacing the storage engine, which appealed to me as it gives us the option to explore using versioned storage down the road. This level of customisability is the type of thing we value in our own work, and makes it easier to produce components that can be reused and reconfigured from the outside.

At least from their docs, all of the rest (multipart, busboy, formidable) seemed to offer only fixed configuration options. They also do not allow you to reject files of a particular type.

Conclusion

In summary, multer was the only candidate that met all of our needs. It also seems to work best with our component-based approach, and to give us the most room to expand or extend its behavior in the future.