Skip to content

Commit 6f7f7e1

Browse files
add validation for max binary file when string base64 encoding
1 parent 17bf3fe commit 6f7f7e1

File tree

7 files changed

+207
-127
lines changed

7 files changed

+207
-127
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ jobs:
6363
password: ${{ secrets.DOCKERHUB_TOKEN }}
6464

6565
- name: Build against builder
66-
uses: docker/build-push-action@v3
66+
uses: docker/build-push-action@v4
6767
with:
6868
builder: ${{ steps.builder.outputs.name }}
6969
file: ${{ env.DOCKERFILE }}
7070
target: builder
7171

7272
- name: Build against main and push
73-
uses: docker/build-push-action@v3
73+
uses: docker/build-push-action@v4
7474
with:
7575
builder: ${{ steps.main.outputs.name }}
7676
file: ${{ env.DOCKERFILE }}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Describes which version.
9797
* Easy to maintenance
9898
* NestJs Habit
9999
* Component based folder structure
100+
* Stateless authentication and authorization
100101
* Repository Design Pattern or Data Access Layer Design Pattern
101102
* Support Microservice Architecture, Serverless Architecture, Clean Architecture, and/or Hexagonal Architecture
102103
* Follow Community Guide Line
@@ -118,7 +119,7 @@ Describes which version.
118119
* Request validation with `class-validation`
119120
* Serialization with `class-transformer`
120121
* Url Versioning
121-
* Server Side Pagination, there have 3 of types
122+
* Server Side Pagination
122123
* Import and export data with excel by using `decorator`
123124

124125
### Database

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ack-nestjs-boilerplate",
3-
"version": "4.2.5",
3+
"version": "4.2.6",
44
"description": "Ack NestJs Boilerplate",
55
"repository": {
66
"type": "git",
@@ -52,7 +52,7 @@
5252
"rollback": "yarn rollback:setting && yarn rollback:apikey && yarn rollback:user && yarn rollback:role && yarn rollback:permission"
5353
},
5454
"dependencies": {
55-
"@aws-sdk/client-s3": "^3.259.0",
55+
"@aws-sdk/client-s3": "^3.262.0",
5656
"@faker-js/faker": "^7.6.0",
5757
"@joi/date": "^2.1.0",
5858
"@nestjs/axios": "^1.0.1",
@@ -117,10 +117,10 @@
117117
"@types/supertest": "^2.0.12",
118118
"@types/ua-parser-js": "^0.7.36",
119119
"@types/uuid": "^9.0.0",
120-
"@typescript-eslint/eslint-plugin": "^5.49.0",
121-
"@typescript-eslint/parser": "^5.49.0",
120+
"@typescript-eslint/eslint-plugin": "^5.50.0",
121+
"@typescript-eslint/parser": "^5.50.0",
122122
"cspell": "^6.19.2",
123-
"eslint": "^8.32.0",
123+
"eslint": "^8.33.0",
124124
"eslint-config-prettier": "^8.6.0",
125125
"eslint-plugin-import": "^2.27.5",
126126
"husky": "^8.0.3",
@@ -132,6 +132,6 @@
132132
"ts-node": "^10.9.1",
133133
"ts-prune": "^0.10.3",
134134
"tsconfig-paths": "^4.1.2",
135-
"typescript": "^4.9.4"
135+
"typescript": "^4.9.5"
136136
}
137137
}

src/common/file/constants/file.enum.constant.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ export enum ENUM_FILE_VIDEO_MIME {
2020
MP4 = 'video/mp4',
2121
APPLICATION_MP4 = 'application/mp4',
2222
}
23+
24+
export enum ENUM_FILE_TYPE {
25+
AUDIO = 'audio',
26+
IMAGE = 'image',
27+
EXCEL = 'excel',
28+
VIDEO = 'video',
29+
}

src/common/request/request.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { MinGreaterThanConstraint } from './validations/request.min-greater-than
2323
import { IsOnlyDigitsConstraint } from './validations/request.only-digits.validation';
2424
import { SafeStringConstraint } from './validations/request.safe-string.validation';
2525
import { SkipConstraint } from './validations/request.skip.validation';
26+
import { MaxBinaryFileConstraint } from 'src/common/request/validations/request.max-binary-file.validation';
2627

2728
@Module({
2829
controllers: [],
@@ -64,6 +65,7 @@ import { SkipConstraint } from './validations/request.skip.validation';
6465
MinDateTodayConstraint,
6566
MobileNumberAllowedConstraint,
6667
MaxDateTodayConstraint,
68+
MaxBinaryFileConstraint,
6769
],
6870
imports: [RequestMiddlewareModule],
6971
})
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
3+
import {
4+
registerDecorator,
5+
ValidationArguments,
6+
ValidationOptions,
7+
ValidatorConstraint,
8+
ValidatorConstraintInterface,
9+
} from 'class-validator';
10+
import { ENUM_FILE_TYPE } from 'src/common/file/constants/file.enum.constant';
11+
12+
@ValidatorConstraint({ async: true })
13+
@Injectable()
14+
export class MaxBinaryFileConstraint implements ValidatorConstraintInterface {
15+
constructor(private readonly configService: ConfigService) {}
16+
17+
validate(value: string, args: ValidationArguments): boolean {
18+
const [type] = args.constraints;
19+
let fileSize = 0;
20+
21+
switch (type) {
22+
case ENUM_FILE_TYPE.AUDIO:
23+
fileSize = this.configService.get<number>(
24+
'file.audio.maxFileSize'
25+
);
26+
break;
27+
case ENUM_FILE_TYPE.EXCEL:
28+
fileSize = this.configService.get<number>(
29+
'file.excel.maxFileSize'
30+
);
31+
break;
32+
case ENUM_FILE_TYPE.IMAGE:
33+
fileSize = this.configService.get<number>(
34+
'file.image.maxFileSize'
35+
);
36+
break;
37+
case ENUM_FILE_TYPE.VIDEO:
38+
fileSize = this.configService.get<number>(
39+
'file.video.maxFileSize'
40+
);
41+
break;
42+
default:
43+
break;
44+
}
45+
46+
return fileSize <= value.length;
47+
}
48+
}
49+
50+
export function MaxBinaryFile(
51+
type: ENUM_FILE_TYPE,
52+
validationOptions?: ValidationOptions
53+
) {
54+
return function (object: Record<string, any>, propertyName: string): any {
55+
registerDecorator({
56+
name: 'MaxBinaryFile',
57+
target: object.constructor,
58+
propertyName: propertyName,
59+
options: validationOptions,
60+
constraints: [type],
61+
validator: MaxBinaryFileConstraint,
62+
});
63+
};
64+
}

0 commit comments

Comments
 (0)