Skip to content

feat(platform) multer support for fastify platform #2088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"fastify": "2.3.0",
"fastify-cors": "2.1.2",
"fastify-formbody": "3.1.0",
"fastify-multer": "^1.4.2",
"fastify-multipart": "0.8.1",
"graphql": "14.2.1",
"grpc": "1.20.0",
Expand Down
1 change: 1 addition & 0 deletions packages/platform-fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export * from './adapters';
export * from './interfaces';
export * from './multer';
1 change: 1 addition & 0 deletions packages/platform-fastify/multer/files.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MULTER_MODULE_OPTIONS = 'MULTER_MODULE_OPTIONS';
3 changes: 3 additions & 0 deletions packages/platform-fastify/multer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './interceptors';
export * from './interfaces';
export * from './multer.module';
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as multer from 'fastify-multer';
import { Observable } from 'rxjs';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
import { MulterModuleOptions } from '../interfaces';
import { transformException } from '../multer/multer.utils';
import {
CallHandler,
ExecutionContext,
Inject,
mixin,
NestInterceptor,
Optional,
Type,
} from '@nestjs/common';
import {
MulterField,
MulterOptions,
} from '../interfaces/multer-options.interface';

type MulterInstance = any;

export function FileFieldsInterceptor(
uploadFields: MulterField[],
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((resolve, reject) =>
this.multer.fields(uploadFields)(
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>;
}
61 changes: 61 additions & 0 deletions packages/platform-fastify/multer/interceptors/file.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as multer from 'fastify-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';
import {
CallHandler,
ExecutionContext,
Inject,
mixin,
NestInterceptor,
Optional,
Type,
} from '@nestjs/common';

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((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>;
}
62 changes: 62 additions & 0 deletions packages/platform-fastify/multer/interceptors/files.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as multer from 'fastify-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';
import {
CallHandler,
ExecutionContext,
Inject,
mixin,
NestInterceptor,
Optional,
Type,
} from '@nestjs/common';

type MulterInstance = any;

export function FilesInterceptor(
fieldName: string,
maxCount?: number,
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((resolve, reject) =>
this.multer.array(fieldName, maxCount)(
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>;
}
3 changes: 3 additions & 0 deletions packages/platform-fastify/multer/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './file-fields.interceptor';
export * from './file.interceptor';
export * from './files.interceptor';
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Type } from '@nestjs/common';
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { MulterOptions } from '../interfaces/multer-options.interface';

export interface MulterModuleOptions extends MulterOptions {}

export interface MulterOptionsFactory {
createMulterOptions(): Promise<MulterModuleOptions> | MulterModuleOptions;
}

export interface MulterModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<MulterOptionsFactory>;
useClass?: Type<MulterOptionsFactory>;
useFactory?: (
...args: any[]
) => Promise<MulterModuleOptions> | MulterModuleOptions;
inject?: any[];
}
1 change: 1 addition & 0 deletions packages/platform-fastify/multer/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './files-upload-module.interface';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @see https://github.com/expressjs/multer
*/
export interface MulterOptions {
dest?: string;
/** The storage engine to use for uploaded files. */
storage?: any;
/**
* An object specifying the size limits of the following optional properties. This object is passed to busboy
* directly, and the details of properties can be found on https://github.com/mscdex/busboy#busboy-methods
*/
limits?: {
/** Max field name size (Default: 100 bytes) */
fieldNameSize?: number;
/** Max field value size (Default: 1MB) */
fieldSize?: number;
/** Max number of non- file fields (Default: Infinity) */
fields?: number;
/** For multipart forms, the max file size (in bytes)(Default: Infinity) */
fileSize?: number;
/** For multipart forms, the max number of file fields (Default: Infinity) */
files?: number;
/** For multipart forms, the max number of parts (fields + files)(Default: Infinity) */
parts?: number;
/** For multipart forms, the max number of header key=> value pairs to parse Default: 2000(same as node's http). */
headerPairs?: number;
/** Keep the full path of files instead of just the base name (Default: false) */
preservePath?: boolean;
};
fileFilter?(
req: any,
file: {
/** Field name specified in the form */
fieldname: string;
/** Name of the file on the user's computer */
originalname: string;
/** Encoding type of the file */
encoding: string;
/** Mime type of the file */
mimetype: string;
/** Size of the file in bytes */
size: number;
/** The folder to which the file has been saved (DiskStorage) */
destination: string;
/** The name of the file within the destination (DiskStorage) */
filename: string;
/** Location of the uploaded file (DiskStorage) */
path: string;
/** A Buffer of the entire file (MemoryStorage) */
buffer: Buffer;
},
callback: (error: Error | null, acceptFile: boolean) => void,
): void;
}

export interface MulterField {
/** The field name. */
name: string;
/** Optional maximum number of files per field to accept. */
maxCount?: number;
}
Loading