-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
43 lines (37 loc) · 981 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict'
const fp = require('fastify-plugin')
const kMultipart = Symbol('multipart')
const fileUpload = require('express-fileupload')
function setMultipart (request, payload, done) {
request[kMultipart] = true
done()
}
function fastifyUpload (fastify, options, done) {
fastify.addContentTypeParser('multipart', setMultipart)
options = options || {}
try {
fastify.use(fileUpload(options))
} catch (e) {
Promise.all([
fastify.register(require('middie'))
]).then(() => {
fastify.use(fileUpload(options))
})
}
fastify.addHook('preValidation', (request, reply, done) => {
if (request.raw) {
!request.body && (request.body = request.raw.body || {})
if (request.raw.files) {
for (const key in request.raw.files) {
request.body[key] = request.raw.files[key]
}
}
}
done()
})
done()
}
module.exports = fp(fastifyUpload, {
fastify: '>=3.0.0',
name: 'fastify-file-upload'
})