feat: refactor QuickImportService to remove ReceiptParser and streamline import logic for PDF and image uploads
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -17,6 +17,10 @@ export class ReceiptParser extends RecipeParser {
|
||||
return false;
|
||||
}
|
||||
|
||||
parse(_html: string): import('./base.parser').ParsedRecipe {
|
||||
throw new Error('ReceiptParser does not support HTML parsing');
|
||||
}
|
||||
|
||||
async parseFromPdf(buffer: Buffer): Promise<ParsedReceiptItem[]> {
|
||||
try {
|
||||
this.logger.log('Parsing PDF receipt...');
|
||||
|
||||
@@ -11,7 +11,6 @@ import * as pdfParse from 'pdf-parse';
|
||||
import { createWorker } from 'tesseract.js';
|
||||
import { IcaRecipeParser } from './parsers/ica.parser';
|
||||
import { GenericRecipeParser } from './parsers/generic.parser';
|
||||
import { ReceiptParser } from './parsers/receipt.parser';
|
||||
import { RecipeParser } from './parsers/base.parser';
|
||||
import { downloadAndOptimizeImage } from '../common/utils/download-image';
|
||||
|
||||
@@ -24,21 +23,11 @@ export interface QuickImportResult {
|
||||
imageWarning?: string;
|
||||
}
|
||||
|
||||
export interface ReceiptImportResult {
|
||||
items: Array<{
|
||||
name: string;
|
||||
quantity: number;
|
||||
price: number;
|
||||
}>;
|
||||
source: 'pdf' | 'image';
|
||||
}
|
||||
|
||||
type UploadKind = 'pdf' | 'image';
|
||||
|
||||
@Injectable()
|
||||
export class QuickImportService {
|
||||
private readonly logger = new Logger(QuickImportService.name);
|
||||
private readonly receiptParser = new ReceiptParser();
|
||||
|
||||
constructor() {}
|
||||
|
||||
@@ -75,9 +64,8 @@ export class QuickImportService {
|
||||
/**
|
||||
* Importerar från en uppladdad fil
|
||||
*/
|
||||
async importFromUpload(file: Express.Multer.File): Promise<QuickImportResult | ReceiptImportResult> {
|
||||
this.logger.log('MIME-typ:', file.mimetype);
|
||||
this.logger.log('Token:', file.originalname);
|
||||
async importFromUpload(file: Express.Multer.File): Promise<QuickImportResult> {
|
||||
this.logger.log(`MIME-typ: ${file.mimetype}, filnamn: ${file.originalname}`);
|
||||
const kind = file.mimetype.startsWith('image/') ? 'image' : 'pdf';
|
||||
return this.importFromBuffer(file.buffer, kind);
|
||||
}
|
||||
@@ -88,12 +76,20 @@ export class QuickImportService {
|
||||
async importFromBuffer(
|
||||
buffer: Buffer,
|
||||
kind: UploadKind,
|
||||
): Promise<QuickImportResult | ReceiptImportResult> {
|
||||
): Promise<QuickImportResult> {
|
||||
try {
|
||||
if (kind === 'pdf') {
|
||||
return this.receiptParser.parseFromPdf(buffer);
|
||||
this.logger.log('Parsar PDF med pdf-parse...');
|
||||
const data = await pdfParse(buffer);
|
||||
const markdown = data.text || '(Tom PDF)';
|
||||
return { markdown, source: 'pdf' };
|
||||
} else {
|
||||
return this.receiptParser.parseFromImage(buffer);
|
||||
this.logger.log('Parsar bild med tesseract...');
|
||||
const worker = await createWorker('swe+eng');
|
||||
const ret = await worker.recognize(buffer);
|
||||
await worker.terminate();
|
||||
const markdown = ret.data.text || '(Tom bild)';
|
||||
return { markdown, source: 'image' };
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(`Fel vid import av ${kind}: ${error}`);
|
||||
|
||||
Reference in New Issue
Block a user