Hello guys i want to upload two files one is requried and the other is optional
idk if thats possible any helpful resource or help will be thankful
@UseInterceptors(
FileFieldsInterceptor([
{ name: 'logoURL', maxCount: 1 },
{ name: 'backgroundURL', maxCount: 1 },
]),
)
async create(
@Req() req: IAuthRequest,
@Body() dto: CreateStoreDto,
@UploadedFiles(FilesValidationPipe)
files: {
logoURL: Express.Multer.File;
backgroundURL?: Express.Multer.File;
},
): Promise<StoreResponse> {
console.log(files);
}
I want the fileValidationPipe to validate that !
@Injectable()
export class FilesValidationPipe implements PipeTransform {
private readonly allowedMimeTypes = ['image/jpeg', 'image/png', 'image/webp'];
private readonly maxMB = 5; // 5MB
private readonly maxFileSize = this.maxMB * 1024 * 1024;
transform(files: FilesParameter): Express.Multer.File[] {
const validFiles: Express.Multer.File[] = [];
for (const key in files) {
const file = files[key][0];
if (!this.isMimeTypeValid(file.mimetype)) {
console.log(file);
throw new BadRequestException(
'Invalid file type. Only JPEG, PNG, and WebP images are allowed.',
);
}
if (file.size > this.maxFileSize) {
throw new BadRequestException(
`File size exceeds the allowed limit. Max allowed size is ${this.maxMB}MB.`,
);
}
validFiles.push(file);
}
return validFiles;
}
private isMimeTypeValid(mimeType: string): boolean {
return this.allowedMimeTypes.includes(mimeType);
}
}
BTW I am using multer the right way ?
I mean i want to upload one image per one am i using the right methods ?