|
- import { inject, injectable } from 'tsyringe';
- import AppError from '../../erros/AppError';
- import IPdfToTextProvider from '../../containers/providers/pdfToText/IPdfToTextProvider';
- import regexMapaFolha from '../../utils/regex/regexMapaFolha';
- import * as format from '../../utils/format';
-
- interface IRequest {
- files: Express.Multer.File[];
- }
-
- interface ICompanyContent {
- name: string;
- code: string;
- cnpj: string;
- refEndDate: string;
- refStartDate: string;
- address: string;
- contentEmployee: string;
- }
-
- type IResponse = Array<{
- fileName: string;
- content: string;
- }>;
-
- @injectable()
- export default class ExempleService {
- constructor(
- @inject('PdfToTextProvider')
- private pdfToTextProvider: IPdfToTextProvider,
- ) {}
-
- public async execute({ files }: IRequest): Promise<IResponse> {
- if (!files) {
- throw new AppError('File is required');
- }
-
- files.forEach(file => {
- if (file.mimetype !== 'application/pdf') {
- throw new AppError('Only PDF is accepted');
- }
- });
-
- const response: IResponse = [];
-
- for (let indexFile = 0; indexFile < files.length; indexFile++) {
- const file = files[indexFile];
- const contentFile = await this.pdfToTextProvider.extract(file.path);
-
- response.push({
- fileName: file.filename,
- content: contentFile,
- });
- }
-
- return response;
- }
- }
|