Skip to content
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

file update refactor, make it simple, and useful for many cases #494

Merged
merged 2 commits into from
Nov 29, 2023
Merged
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ack-nestjs-boilerplate",
"version": "5.6.1",
"version": "5.6.2",
"description": "Ack NestJs Boilerplate",
"repository": {
"type": "git",
Expand Down Expand Up @@ -73,7 +73,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"crypto-js": "^4.2.0",
"google-auth-library": "^9.2.0",
"google-auth-library": "^9.3.0",
"helmet": "^7.1.0",
"joi": "^17.11.0",
"moment": "^2.29.4",
Expand Down
2 changes: 1 addition & 1 deletion src/common/aws/constants/aws.s3.constant.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const AwsS3MaxPartNumber = 10000;
export const AWS_S3_MAX_PART_NUMBER = 10000;
7 changes: 5 additions & 2 deletions src/common/file/constants/file.constant.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const FILE_CUSTOM_MAX_SIZE_META_KEY = 'FileCustomMaxSizeMetaKey';
export const FILE_CUSTOM_MAX_FILES_META_KEY = 'FileCustomMaxFilesMetaKey';
import bytes from 'bytes';

export const FILE_SIZE_IN_BYTES: number = bytes('1mb');
export const FILE_SIZE_MEDIUM_IN_BYTES: number = bytes('5mb');
export const FILE_SIZE_LARGE_IN_BYTES: number = bytes('10mb');
86 changes: 74 additions & 12 deletions src/common/file/decorators/file.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,97 @@ import {
applyDecorators,
createParamDecorator,
ExecutionContext,
SetMetadata,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
FILE_CUSTOM_MAX_FILES_META_KEY,
FILE_CUSTOM_MAX_SIZE_META_KEY,
FILE_SIZE_IN_BYTES,
FILE_SIZE_LARGE_IN_BYTES,
FILE_SIZE_MEDIUM_IN_BYTES,
} from 'src/common/file/constants/file.constant';
import { FileCustomMaxSizeInterceptor } from 'src/common/file/interceptors/file.custom-max-size.interceptor';
import { IRequestApp } from 'src/common/request/interfaces/request.interface';

export function FileUploadSingle(field?: string): MethodDecorator {
return applyDecorators(UseInterceptors(FileInterceptor(field ?? 'file')));
return applyDecorators(
UseInterceptors(
FileInterceptor(field ?? 'file', {
limits: {
fileSize: FILE_SIZE_IN_BYTES,
files: 1,
},
})
)
);
}

export function FileUploadSingleMedium(field?: string): MethodDecorator {
return applyDecorators(
UseInterceptors(
FileInterceptor(field ?? 'file', {
limits: {
fileSize: FILE_SIZE_MEDIUM_IN_BYTES,
files: 1,
},
})
)
);
}

export function FileUploadMultiple(field?: string): MethodDecorator {
return applyDecorators(UseInterceptors(FilesInterceptor(field ?? 'files')));
export function FileUploadSingleLarge(field?: string): MethodDecorator {
return applyDecorators(
UseInterceptors(
FileInterceptor(field ?? 'file', {
limits: {
fileSize: FILE_SIZE_LARGE_IN_BYTES,
files: 1,
},
})
)
);
}

export function FileUploadMultiple(
field?: string,
maxFiles?: number
): MethodDecorator {
return applyDecorators(
UseInterceptors(
FilesInterceptor(field ?? 'files', maxFiles ?? 2, {
limits: {
fileSize: FILE_SIZE_IN_BYTES,
},
})
)
);
}

export function FileCustomMaxFile(customMaxFiles: number): MethodDecorator {
export function FileUploadMultipleMedium(
field?: string,
maxFiles?: number
): MethodDecorator {
return applyDecorators(
SetMetadata(FILE_CUSTOM_MAX_FILES_META_KEY, customMaxFiles)
UseInterceptors(
FilesInterceptor(field ?? 'files', maxFiles ?? 2, {
limits: {
fileSize: FILE_SIZE_MEDIUM_IN_BYTES,
},
})
)
);
}

export function FileCustomMaxSize(customMaxSize: string): MethodDecorator {
export function FileUploadMultipleLarge(
field?: string,
maxFiles?: number
): MethodDecorator {
return applyDecorators(
UseInterceptors(FileCustomMaxSizeInterceptor),
SetMetadata(FILE_CUSTOM_MAX_SIZE_META_KEY, customMaxSize)
UseInterceptors(
FilesInterceptor(field ?? 'files', maxFiles ?? 2, {
limits: {
fileSize: FILE_SIZE_LARGE_IN_BYTES,
},
})
)
);
}

Expand Down
38 changes: 0 additions & 38 deletions src/common/file/interceptors/file.custom-max-files.interceptor.ts

This file was deleted.

38 changes: 0 additions & 38 deletions src/common/file/interceptors/file.custom-max-size.interceptor.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/common/file/pipes/file.max-files.pipe.ts

This file was deleted.

Loading