Added file validation pipe

This commit is contained in:
GeorgeWebberley 2026-03-01 12:18:53 +01:00
parent ef56213830
commit cf4a2f16bd
2 changed files with 25 additions and 1 deletions

View file

@ -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[] = [];

View 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;
}
}