mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import {
|
|
CallHandler,
|
|
ExecutionContext,
|
|
Inject,
|
|
mixin,
|
|
NestInterceptor,
|
|
Optional,
|
|
Type,
|
|
} from '@nestjs/common';
|
|
import * as multer from 'multer';
|
|
import { Observable } from 'rxjs';
|
|
import { MULTER_MODULE_OPTIONS } from '../files.constants';
|
|
import { MulterModuleOptions } from '../interfaces';
|
|
import { MulterOptions } from '../interfaces/multer-options.interface';
|
|
import { transformException } from '../multer/multer.utils';
|
|
|
|
type MulterInstance = any;
|
|
|
|
export function FileInterceptor(
|
|
fieldName: string,
|
|
localOptions?: MulterOptions,
|
|
): Type<NestInterceptor> {
|
|
class MixinInterceptor implements NestInterceptor {
|
|
protected multer: MulterInstance;
|
|
|
|
constructor(
|
|
@Optional()
|
|
@Inject(MULTER_MODULE_OPTIONS)
|
|
options: MulterModuleOptions = {},
|
|
) {
|
|
this.multer = (multer as any)({
|
|
...options,
|
|
...localOptions,
|
|
});
|
|
}
|
|
|
|
async intercept(
|
|
context: ExecutionContext,
|
|
next: CallHandler,
|
|
): Promise<Observable<any>> {
|
|
const ctx = context.switchToHttp();
|
|
|
|
await new Promise<void>((resolve, reject) =>
|
|
this.multer.single(fieldName)(
|
|
ctx.getRequest(),
|
|
ctx.getResponse(),
|
|
(err: any) => {
|
|
if (err) {
|
|
const error = transformException(err);
|
|
return reject(error);
|
|
}
|
|
resolve();
|
|
},
|
|
),
|
|
);
|
|
return next.handle();
|
|
}
|
|
}
|
|
const Interceptor = mixin(MixinInterceptor);
|
|
return Interceptor as Type<NestInterceptor>;
|
|
}
|