Added file validation pipe
This commit is contained in:
parent
ef56213830
commit
cf4a2f16bd
|
|
@ -3,6 +3,7 @@ import { GraphQLUpload } from 'graphql-upload-ts';
|
|||
import type { FileUpload } from 'graphql-upload-ts';
|
||||
import { CasesService } from './cases.service';
|
||||
import { CaseLaw } from './entities/case-law.entity';
|
||||
import { CaseFileValidationPipe } from 'src/common/pipes/file-validation.pipe';
|
||||
|
||||
@Resolver(() => CaseLaw)
|
||||
export class CasesResolver {
|
||||
|
|
@ -21,7 +22,7 @@ export class CasesResolver {
|
|||
|
||||
@Mutation(() => CaseLaw)
|
||||
async uploadCase(
|
||||
@Args({ name: 'file', type: () => GraphQLUpload })
|
||||
@Args({ name: 'file', type: () => GraphQLUpload }, CaseFileValidationPipe)
|
||||
{ createReadStream, filename, mimetype }: FileUpload,
|
||||
) {
|
||||
const chunks: any[] = [];
|
||||
|
|
|
|||
23
src/common/pipes/file-validation.pipe.ts
Normal file
23
src/common/pipes/file-validation.pipe.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
|
||||
import type { FileUpload } from 'graphql-upload-ts';
|
||||
|
||||
@Injectable()
|
||||
export class CaseFileValidationPipe implements PipeTransform {
|
||||
private readonly allowedMimeTypes = ['application/pdf', 'text/html'];
|
||||
|
||||
async transform(value: FileUpload): Promise<FileUpload> {
|
||||
if (!value) {
|
||||
throw new BadRequestException('No file provided');
|
||||
}
|
||||
|
||||
const { mimetype } = value;
|
||||
|
||||
if (!this.allowedMimeTypes.includes(mimetype)) {
|
||||
throw new BadRequestException(
|
||||
`Invalid file type: ${mimetype}. Only PDF and HTML are allowed.`,
|
||||
);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue