-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #771 from hngprojects/dev
Merge branch dev into staging
- Loading branch information
Showing
11 changed files
with
113 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as path from 'path'; | ||
|
||
export const MAX_PROFILE_PICTURE_SIZE = 2 * 1024 * 1024; | ||
export const VALID_UPLOADS_MIME_TYPES = ['image/jpeg', 'image/png']; | ||
export const BASE_URL = "https://staging.api-nestjs.boilerplate.hng.tech"; | ||
export const PROFILE_PHOTO_UPLOADS = path.join(__dirname, '..', 'uploads') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,49 @@ | ||
import { PipeTransform, Injectable, ArgumentMetadata, HttpStatus } from '@nestjs/common'; | ||
import { CustomHttpException } from '../../../helpers/custom-http-filter'; | ||
import * as fileType from 'file-type-mime'; | ||
import { FILE_EXCEEDS_SIZE, INVALID_FILE_TYPE } from '../../../helpers/SystemMessages'; | ||
|
||
|
||
export interface CustomUploadTypeValidatorOptions { | ||
fileType: string[]; | ||
} | ||
|
||
@Injectable() | ||
export class FileValidator implements PipeTransform { | ||
constructor(private readonly options: { maxSize: number; mimeTypes: string[] }) {} | ||
constructor(private readonly options: { maxSize: number; mimeTypes: string[] }) { | ||
|
||
} | ||
|
||
transform(value: Express.Multer.File, metadata: ArgumentMetadata) { | ||
async transform(value: Express.Multer.File, metadata: ArgumentMetadata) { | ||
if (!value) { | ||
throw new CustomHttpException('No file provided', HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
if (value.size > this.options.maxSize) { | ||
throw new CustomHttpException( | ||
`File size exceeds ${this.options.maxSize / (1024 * 1024)}MB limit`, | ||
this.validateFileSize(value.size) | ||
await this.validateFileType(value.buffer) | ||
|
||
return value; | ||
} | ||
|
||
private validateFileSize(size: number) { | ||
if (size > this.options.maxSize) { | ||
throw new CustomHttpException( | ||
FILE_EXCEEDS_SIZE(this.options.maxSize / (1024 * 1024)), | ||
HttpStatus.BAD_REQUEST | ||
); | ||
} | ||
} | ||
|
||
if (!this.options.mimeTypes.includes(value.mimetype)) { | ||
throw new CustomHttpException( | ||
`Invalid file type. Allowed types: ${this.options.mimeTypes.join(', ')}`, | ||
|
||
private async validateFileType(buffer: Buffer) { | ||
const response = await fileType.parse(buffer); | ||
if (!response || !this.options.mimeTypes.includes(response.mime)) { | ||
throw new CustomHttpException( | ||
INVALID_FILE_TYPE(this.options.mimeTypes.join(', ')), | ||
HttpStatus.BAD_REQUEST | ||
); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.