118 lines
3.9 KiB
TypeScript
118 lines
3.9 KiB
TypeScript
import { Signal, signal } from '@angular/core';
|
|
import { Observable, map, tap } from 'rxjs';
|
|
import { PdfDetectResponse, PreviewResponse, RegionSpec, SourceLocator, TableCandidate } from '../api/api.types';
|
|
import { fileToBase64 } from '../api/file-encoding';
|
|
import { SourceGateway } from '../api/source-gateway';
|
|
import { DataConnection, EncodedFile, SourceTable } from './data-source';
|
|
import { DocumentFiles, DocumentTable } from './document-source';
|
|
|
|
/**
|
|
* A PDF, readable three ways:
|
|
* - tables the server detects in its text layer;
|
|
* - columns the user draws over the pages (OCR'd when scanned);
|
|
* - as a clinical document: fields extracted from labels and free text,
|
|
* one record per patient (`DocumentTable`), for reports that aren't tables.
|
|
*/
|
|
export class PdfConnection extends DataConnection implements DocumentFiles {
|
|
readonly icon = 'picture_as_pdf';
|
|
readonly typeLabel = 'Documento PDF';
|
|
readonly encoded: EncodedFile;
|
|
readonly documentTable: DocumentTable;
|
|
private readonly _detection = signal<PdfDetectResponse | null>(null);
|
|
readonly detection: Signal<PdfDetectResponse | null> = this._detection.asReadonly();
|
|
|
|
constructor(readonly file: File) {
|
|
super(file.name);
|
|
this.encoded = new EncodedFile(file, fileToBase64);
|
|
this.documentTable = new DocumentTable(this, 'Campos del documento');
|
|
}
|
|
|
|
get encodedFiles(): EncodedFile[] {
|
|
return [this.encoded];
|
|
}
|
|
|
|
addRegions(name: string, regions: RegionSpec[]): PdfRegionTable {
|
|
const table = new PdfRegionTable(this, name, regions);
|
|
this.addTable(table);
|
|
return table;
|
|
}
|
|
|
|
protected discover(gateway: SourceGateway): Observable<SourceTable[]> {
|
|
return gateway.pdf.detect(this.file).pipe(
|
|
tap((detection) => this._detection.set(detection)),
|
|
map((detection) => {
|
|
const drawn = this.tables().filter((table) => table instanceof PdfRegionTable);
|
|
const detected = detection.table_candidates.map(
|
|
(candidate, index) => new PdfDetectedTable(this, `Tabla ${index + 1} · pág. ${candidate.page_number}`, candidate),
|
|
);
|
|
return [this.documentTable, ...detected, ...drawn];
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
export class PdfDetectedTable extends SourceTable {
|
|
readonly icon = 'table_chart';
|
|
readonly kindLabel = 'Tabla detectada';
|
|
|
|
constructor(
|
|
override readonly connection: PdfConnection,
|
|
name: string,
|
|
readonly candidate: TableCandidate,
|
|
) {
|
|
super(connection, name);
|
|
}
|
|
|
|
preview(gateway: SourceGateway, target: string | null): Observable<PreviewResponse> {
|
|
return gateway.pdf.tablePreview(this.connection.file, this.candidate.page_number, this.candidate.bbox, target);
|
|
}
|
|
|
|
async locator(): Promise<SourceLocator> {
|
|
return {
|
|
source_type: 'pdf',
|
|
payload: {
|
|
file_base64: await this.connection.encoded.base64(),
|
|
mode: 'table',
|
|
page_number: this.candidate.page_number,
|
|
bbox: this.candidate.bbox,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
export class PdfRegionTable extends SourceTable {
|
|
readonly icon = 'crop_free';
|
|
readonly kindLabel = 'Regiones dibujadas';
|
|
|
|
constructor(
|
|
override readonly connection: PdfConnection,
|
|
name: string,
|
|
readonly regions: RegionSpec[],
|
|
) {
|
|
super(connection, name);
|
|
}
|
|
|
|
override get removable(): boolean {
|
|
return true;
|
|
}
|
|
|
|
preview(gateway: SourceGateway, target: string | null): Observable<PreviewResponse> {
|
|
return gateway.pdf.extractRegions(this.connection.file, this.regions, target).pipe(
|
|
map((extraction) => ({
|
|
columns: extraction.columns,
|
|
sample_rows: extraction.rows.slice(0, 50),
|
|
total_rows: extraction.rows.length,
|
|
suggested_mapping: extraction.suggested_mapping,
|
|
source_metadata: { warnings: extraction.warnings },
|
|
})),
|
|
);
|
|
}
|
|
|
|
async locator(): Promise<SourceLocator> {
|
|
return {
|
|
source_type: 'pdf',
|
|
payload: { file_base64: await this.connection.encoded.base64(), mode: 'regions', regions: this.regions },
|
|
};
|
|
}
|
|
}
|