Skip to content

Commit 139ad8b

Browse files
committed
feat: add charset option for multipart forms and update tests for UTF-8 filenames
Signed-off-by: Sebastian Beltran <[email protected]>
1 parent 256da2f commit 139ad8b

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Key | Description
142142
`fileFilter` | Function to control which files are accepted
143143
`limits` | Limits of the uploaded data
144144
`preservePath` | Keep the full path of files instead of just the base name
145+
`charset` | For multipart forms, the default character set to use for values of part header parameters (e.g. filename) that are not extended parameters (that contain an explicit charset). **Default**: `'latin1'`.
145146

146147
In an average web app, only `dest` might be required, and configured as shown in
147148
the following example.

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ function Multer (options) {
1919

2020
this.limits = options.limits
2121
this.preservePath = options.preservePath
22+
// TODO: next major version change default to utf-8
23+
this.charset = options.charset || 'latin1'
2224
this.fileFilter = options.fileFilter || allowAll
2325
}
2426

@@ -47,6 +49,7 @@ Multer.prototype._makeMiddleware = function (fields, fileStrategy) {
4749
return {
4850
limits: this.limits,
4951
preservePath: this.preservePath,
52+
charset: this.charset,
5053
storage: this.storage,
5154
fileFilter: wrappedFileFilter,
5255
fileStrategy: fileStrategy
@@ -77,6 +80,7 @@ Multer.prototype.any = function () {
7780
return {
7881
limits: this.limits,
7982
preservePath: this.preservePath,
83+
charset: this.charset,
8084
storage: this.storage,
8185
fileFilter: this.fileFilter,
8286
fileStrategy: 'ARRAY'

lib/make-middleware.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function makeMiddleware (setup) {
2525
var fileFilter = options.fileFilter
2626
var fileStrategy = options.fileStrategy
2727
var preservePath = options.preservePath
28+
var defParamCharset = options.charset
2829

2930
req.body = Object.create(null)
3031

@@ -35,7 +36,12 @@ function makeMiddleware (setup) {
3536
var busboy
3637

3738
try {
38-
busboy = Busboy({ headers: req.headers, limits: limits, preservePath: preservePath })
39+
busboy = Busboy({
40+
headers: req.headers,
41+
limits,
42+
preservePath,
43+
defParamCharset
44+
})
3945
} catch (err) {
4046
return next(err)
4147
}

test/unicode.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,58 @@ describe('Unicode', function () {
6060
})
6161
})
6262
})
63+
64+
describe('UTF-8 filenames', function () {
65+
var uploadDir, upload
66+
67+
beforeEach(function (done) {
68+
temp.mkdir(function (err, path) {
69+
if (err) return done(err)
70+
71+
var storage = multer.diskStorage({
72+
destination: path,
73+
filename: function (req, file, cb) {
74+
cb(null, file.originalname)
75+
}
76+
})
77+
78+
uploadDir = path
79+
upload = multer({ storage: storage, charset: 'utf-8' })
80+
81+
done()
82+
})
83+
})
84+
85+
afterEach(function (done) {
86+
rimraf(uploadDir, done)
87+
})
88+
89+
it('should handle UTF-8 filenames', function (done) {
90+
var req = new stream.PassThrough()
91+
var boundary = 'AaB03x'
92+
var body = [
93+
'--' + boundary,
94+
'Content-Disposition: form-data; name="small0"; filename="ÖoϪ.dat"',
95+
'Content-Type: text/plain',
96+
'',
97+
'test with UTF-8 filename',
98+
'--' + boundary + '--'
99+
].join('\r\n')
100+
101+
req.headers = {
102+
'content-type': 'multipart/form-data; boundary=' + boundary,
103+
'content-length': body.length
104+
}
105+
106+
req.end(body)
107+
108+
upload.single('small0')(req, null, function (err) {
109+
assert.ifError(err)
110+
111+
assert.strictEqual(req.file.originalname, 'ÖoϪ.dat')
112+
assert.strictEqual(req.file.fieldname, 'small0')
113+
114+
done()
115+
})
116+
})
117+
})

0 commit comments

Comments
 (0)