forked from hangshuo652/aurak
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import {
|
|
Controller,
|
|
Logger,
|
|
Post,
|
|
UseGuards,
|
|
UseInterceptors,
|
|
UploadedFile,
|
|
} from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
|
|
import { OcrService } from './ocr.service';
|
|
import { I18nService } from '../i18n/i18n.service';
|
|
|
|
@Controller('ocr')
|
|
@UseGuards(CombinedAuthGuard)
|
|
@UseGuards(CombinedAuthGuard)
|
|
export class OcrController {
|
|
private readonly logger = new Logger(OcrController.name);
|
|
|
|
constructor(
|
|
private readonly ocrService: OcrService,
|
|
private readonly i18n: I18nService,
|
|
) {}
|
|
|
|
@Post('recognize')
|
|
@UseInterceptors(FileInterceptor('image'))
|
|
async recognizeText(@UploadedFile() image: Express.Multer.File) {
|
|
this.logger.log('OCR recognition endpoint called');
|
|
if (!image) {
|
|
this.logger.error('No image uploaded');
|
|
throw new Error(this.i18n.getMessage('noImageUploaded'));
|
|
}
|
|
this.logger.log('Received image. Size: ' + image.size + ' bytes');
|
|
const text = await this.ocrService.extractTextFromImage(image.buffer);
|
|
this.logger.log('OCR extraction completed. Text length: ' + text.length);
|
|
return { text };
|
|
}
|
|
}
|