0b69683080
Test Suite / test (24.15.0) (push) Has been cancelled
- Added new FlyerParsingModule to the application - Updated AppModule to import the new FlyerParsingModule - Added new directory structure for flyer-parsing module
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
HttpCode,
|
|
Post,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { memoryStorage } from 'multer';
|
|
import { ParseFlyerDto } from './dto/parse-flyer.dto';
|
|
import { FlyerParseResponse, FlyerParsingService } from './flyer-parsing.service';
|
|
|
|
const ALLOWED_UPLOAD_MIMES = new Set([
|
|
'application/pdf',
|
|
'application/octet-stream',
|
|
'text/plain',
|
|
]);
|
|
|
|
@Controller('flyer')
|
|
export class FlyerParsingController {
|
|
constructor(private readonly flyerParsingService: FlyerParsingService) {}
|
|
|
|
@Post('parse')
|
|
@HttpCode(200)
|
|
@UseInterceptors(
|
|
FileInterceptor('file', {
|
|
storage: memoryStorage(),
|
|
limits: { fileSize: 15 * 1024 * 1024 },
|
|
fileFilter: (_req, file, cb) => {
|
|
if (ALLOWED_UPLOAD_MIMES.has(file.mimetype)) {
|
|
cb(null, true);
|
|
return;
|
|
}
|
|
cb(
|
|
new BadRequestException('Otillåten filtyp för flyer-parser. Stöd: PDF eller textfil.'),
|
|
false,
|
|
);
|
|
},
|
|
}),
|
|
)
|
|
async parseFlyer(
|
|
@Body() body: ParseFlyerDto,
|
|
@UploadedFile() file?: Express.Multer.File,
|
|
): Promise<FlyerParseResponse> {
|
|
if (!file && !body?.text?.trim()) {
|
|
throw new BadRequestException('Skicka antingen fil under "file" eller text i body.text.');
|
|
}
|
|
|
|
const text = body?.text?.trim();
|
|
return this.flyerParsingService.parseFlyer({
|
|
file,
|
|
text,
|
|
retailer: body?.retailer ?? 'willys',
|
|
});
|
|
}
|
|
}
|