You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
915 B
39 lines
915 B
import { IncomingMessage } from 'http'
|
|
import fp from 'fastify-plugin'
|
|
import { PluginOptions } from 'fastify-plugin'
|
|
import { FastifyInstance, FastifyRequest } from 'fastify'
|
|
|
|
const kMultipart = Symbol('multipart')
|
|
|
|
function setMultipart(
|
|
req: FastifyRequest,
|
|
_payload: IncomingMessage,
|
|
done: (err: Error | null) => void,
|
|
) {
|
|
// nothing to do, it will be done by multer in beforeHandler method
|
|
;(req as any)[kMultipart] = true
|
|
done(null)
|
|
}
|
|
|
|
export function isMultipart(this: FastifyRequest): boolean {
|
|
return (this.raw as any)[kMultipart] || false
|
|
}
|
|
|
|
function fastifyMulter(
|
|
fastify: FastifyInstance,
|
|
_options: PluginOptions,
|
|
next: (err?: Error) => void,
|
|
) {
|
|
fastify.addContentTypeParser('multipart', setMultipart)
|
|
fastify.decorateRequest('isMultipart', isMultipart)
|
|
|
|
next()
|
|
}
|
|
|
|
const multer = fp(fastifyMulter, {
|
|
fastify: '>= 3.0.0',
|
|
name: 'fastify-multer',
|
|
})
|
|
|
|
export default multer
|