|
1 | | -import { BadRequestException, Injectable } from '@nestjs/common' |
| 1 | +import { Injectable } from '@nestjs/common' |
2 | 2 | import pdfParse from 'pdf-parse' |
3 | 3 | import ProcessingDocument from '../contracts/processing-document.interface' |
4 | 4 | import { ExtractedDocumentProps } from '@/contracts/extracted-document.props' |
| 5 | +import { DocumentoConteudoInvalidoException } from '@/errors/document/documento-conteudo-invalido.exception' |
| 6 | +import { DocumentoTituloInvalidoException } from '@/errors/document/documento-titulo-invalido.exception' |
| 7 | +import { DocumentoPdfInvalidoException } from '@/errors/document/documento-pdf-invalido.exception' |
5 | 8 |
|
6 | 9 | @Injectable() |
7 | 10 | export class PdfProcessingService implements ProcessingDocument { |
8 | 11 | async extractTitleAndContent( |
9 | 12 | file: Express.Multer.File, |
10 | 13 | ): Promise<ExtractedDocumentProps> { |
11 | 14 | if (!file.mimetype || file.mimetype !== 'application/pdf') { |
12 | | - throw new BadRequestException( |
13 | | - `O arquivo enviado não é um PDF válido. Tipo recebido: ${file.mimetype}`, |
14 | | - ) |
| 15 | + throw new DocumentoPdfInvalidoException(file.mimetype) |
15 | 16 | } |
16 | 17 |
|
17 | 18 | const data = await pdfParse(file.buffer) |
18 | 19 | const title = data.info.Title || 'Título não encontrado' |
19 | 20 | const content = data.text |
20 | 21 |
|
21 | 22 | if (!content || content.trim() === '') { |
22 | | - throw new BadRequestException('O PDF não contém conteúdo válido.') |
| 23 | + throw new DocumentoConteudoInvalidoException() |
23 | 24 | } |
24 | 25 |
|
25 | 26 | if (!title || title.trim() === 'Título não encontrado') { |
26 | | - throw new BadRequestException('O PDF não contém um título válido.') |
| 27 | + throw new DocumentoTituloInvalidoException() |
27 | 28 | } |
28 | 29 |
|
29 | 30 | return { title, content } as ExtractedDocumentProps |
|
0 commit comments