FEAT: Adiciona validações de tipo e tamanho no envio de mídia
- fileFilter no Multer rejeita tipos não suportados antes de bufferizar - Limite por tipo: imagem 5 MB, áudio/vídeo 16 MB, documento 100 MB - Erro retornado ao frontend em caso de violação
This commit is contained in:
parent
30ec6e4b5e
commit
97a12d700a
@ -1,5 +1,23 @@
|
|||||||
import { Controller, Get, Post, Body, Delete, Param, ForbiddenException, Query, UseInterceptors, UploadedFile, BadRequestException } from '@nestjs/common';
|
import { Controller, Get, Post, Body, Delete, Param, ForbiddenException, Query, UseInterceptors, UploadedFile, BadRequestException } from '@nestjs/common';
|
||||||
import { FileInterceptor } from '@nestjs/platform-express';
|
import { FileInterceptor } from '@nestjs/platform-express';
|
||||||
|
|
||||||
|
const ALLOWED_MEDIA_TYPES = new Set([
|
||||||
|
'image/jpeg', 'image/png', 'image/webp', 'image/gif',
|
||||||
|
'audio/mpeg', 'audio/mp4', 'audio/ogg', 'audio/wav', 'audio/aac', 'audio/amr',
|
||||||
|
'video/mp4', 'video/3gpp',
|
||||||
|
'application/pdf',
|
||||||
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||||
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||||
|
'application/msword',
|
||||||
|
'application/vnd.ms-excel',
|
||||||
|
]);
|
||||||
|
|
||||||
|
const MAX_SIZE_MB: Record<string, number> = {
|
||||||
|
image: 5,
|
||||||
|
audio: 16,
|
||||||
|
video: 16,
|
||||||
|
document: 100,
|
||||||
|
};
|
||||||
import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
|
import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||||
import { WhatsappService } from './whatsapp.service';
|
import { WhatsappService } from './whatsapp.service';
|
||||||
import { AttendanceAssignmentService } from '../attendance/attendance-assignment.service';
|
import { AttendanceAssignmentService } from '../attendance/attendance-assignment.service';
|
||||||
@ -68,7 +86,16 @@ export class WhatsappController {
|
|||||||
@ApiOperation({ summary: 'Envia mídia no WhatsApp' })
|
@ApiOperation({ summary: 'Envia mídia no WhatsApp' })
|
||||||
@ApiResponse({ status: 201, description: 'Mídia enviada.' })
|
@ApiResponse({ status: 201, description: 'Mídia enviada.' })
|
||||||
@Post('send-media')
|
@Post('send-media')
|
||||||
@UseInterceptors(FileInterceptor('file', { limits: { fileSize: 100 * 1024 * 1024 } }))
|
@UseInterceptors(FileInterceptor('file', {
|
||||||
|
limits: { fileSize: 100 * 1024 * 1024 },
|
||||||
|
fileFilter: (_req, file, cb) => {
|
||||||
|
if (ALLOWED_MEDIA_TYPES.has(file.mimetype)) {
|
||||||
|
cb(null, true);
|
||||||
|
} else {
|
||||||
|
cb(new BadRequestException(`Tipo de arquivo não suportado: ${file.mimetype}`), false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}))
|
||||||
async sendMediaMessage(
|
async sendMediaMessage(
|
||||||
@UploadedFile() file: Express.Multer.File,
|
@UploadedFile() file: Express.Multer.File,
|
||||||
@Body('to') to: string,
|
@Body('to') to: string,
|
||||||
@ -77,6 +104,16 @@ export class WhatsappController {
|
|||||||
) {
|
) {
|
||||||
if (!file) throw new BadRequestException('Arquivo não enviado.');
|
if (!file) throw new BadRequestException('Arquivo não enviado.');
|
||||||
if (!to) throw new BadRequestException('Destinatário (to) obrigatório.');
|
if (!to) throw new BadRequestException('Destinatário (to) obrigatório.');
|
||||||
|
|
||||||
|
const mediaType = file.mimetype.startsWith('image/') ? 'image'
|
||||||
|
: file.mimetype.startsWith('audio/') ? 'audio'
|
||||||
|
: file.mimetype.startsWith('video/') ? 'video'
|
||||||
|
: 'document';
|
||||||
|
const limitMB = MAX_SIZE_MB[mediaType];
|
||||||
|
if (file.size > limitMB * 1024 * 1024) {
|
||||||
|
throw new BadRequestException(`Arquivo muito grande. Limite para ${mediaType}: ${limitMB} MB.`);
|
||||||
|
}
|
||||||
|
|
||||||
return this.whatsappService.sendMediaMessage(to, file.buffer, file.mimetype, senderName, caption);
|
return this.whatsappService.sendMediaMessage(to, file.buffer, file.mimetype, senderName, caption);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user