Skip to content

Commit

Permalink
add parseJsonFields option
Browse files Browse the repository at this point in the history
  • Loading branch information
panzi committed Dec 15, 2021
1 parent 4f4326a commit 3341e11
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -138,6 +138,7 @@ Key | Description
`dest` or `storage` | Where to store the files
`fileFilter` | Function to control which files are accepted
`limits` | Limits of the uploaded data
`parseJsonFields` | Parse fields that have the content-type `application/json`
`preservePath` | Keep the full path of files instead of just the base name

In an average web app, only `dest` might be required, and configured as shown in
Expand Down Expand Up @@ -299,6 +300,13 @@ function fileFilter (req, file, cb) {
}
```

### `parseJsonFields`

Fields may also have a `Content-Type` header. If you set `parseJsonFields` to
`true` these fields will be parsed using `JSON.parse()` instead of handled as
plain text strings. This way you don't need to unroll complex JSON structures
that are transmitted alongside uploaded files as url-encoded fields.

## Error handling

When encountering an error, Multer will delegate the error to Express. You can
Expand Down
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -20,6 +20,7 @@ function Multer (options) {
this.limits = options.limits
this.preservePath = options.preservePath
this.fileFilter = options.fileFilter || allowAll
this.parseJsonFields = !!options.parseJsonFields
}

Multer.prototype._makeMiddleware = function (fields, fileStrategy) {
Expand Down Expand Up @@ -49,7 +50,8 @@ Multer.prototype._makeMiddleware = function (fields, fileStrategy) {
preservePath: this.preservePath,
storage: this.storage,
fileFilter: wrappedFileFilter,
fileStrategy: fileStrategy
fileStrategy: fileStrategy,
parseJsonFields: this.parseJsonFields
}
}

Expand Down Expand Up @@ -79,7 +81,8 @@ Multer.prototype.any = function () {
preservePath: this.preservePath,
storage: this.storage,
fileFilter: this.fileFilter,
fileStrategy: 'ARRAY'
fileStrategy: 'ARRAY',
parseJsonFields: this.parseJsonFields
}
}

Expand Down
11 changes: 10 additions & 1 deletion lib/make-middleware.js
Expand Up @@ -24,6 +24,7 @@ function makeMiddleware (setup) {
var fileFilter = options.fileFilter
var fileStrategy = options.fileStrategy
var preservePath = options.preservePath
var parseJsonFields = options.parseJsonFields

req.body = Object.create(null)

Expand Down Expand Up @@ -80,7 +81,7 @@ function makeMiddleware (setup) {
}

// handle text field data
busboy.on('field', function (fieldname, value, fieldnameTruncated, valueTruncated) {
busboy.on('field', function (fieldname, value, fieldnameTruncated, valueTruncated, encoding, mimetype) {
if (fieldname == null) return abortWithCode('MISSING_FIELD_NAME')
if (fieldnameTruncated) return abortWithCode('LIMIT_FIELD_KEY')
if (valueTruncated) return abortWithCode('LIMIT_FIELD_VALUE', fieldname)
Expand All @@ -90,6 +91,14 @@ function makeMiddleware (setup) {
if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY')
}

if (parseJsonFields && mimetype === 'application/json') {
try {
value = JSON.parse(value)
} catch (error) {
return abortWithError(error)
}
}

appendField(req.body, fieldname, value)
})

Expand Down

0 comments on commit 3341e11

Please sign in to comment.