690525:1541 ADR-028-228-migration-OCR #05 ocr-sidecar working
CI / CD Pipeline / build (push) Successful in 4m14s
CI / CD Pipeline / deploy (push) Successful in 5m0s

This commit is contained in:
2026-05-25 15:41:56 +07:00
parent 87c3defc76
commit d315488d83
3 changed files with 24 additions and 1 deletions
+22 -1
View File
@@ -2,6 +2,7 @@
// Change Log
// - 2026-05-15: เพิ่ม OCR auto-detection service สำหรับ ADR-023A.
// - 2026-05-25: แก้ไข AggregateError (empty message) จาก axios โดย wrap เป็น Error พร้อม context ที่ชัดเจน.
// - 2026-05-25: เพิ่ม path remapping (OCR_UPLOAD_BASE_PATH) เพื่อแปลง local upload path เป็น path ที่ sidecar เห็นผ่าน CIFS.
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@@ -28,6 +29,8 @@ export class OcrService {
private readonly logger = new Logger(OcrService.name);
private readonly threshold: number;
private readonly ocrApiUrl: string;
private readonly localUploadBase: string;
private readonly sidecarUploadBase: string;
constructor(private readonly configService: ConfigService) {
this.threshold = this.configService.get<number>('OCR_CHAR_THRESHOLD', 100);
@@ -35,6 +38,22 @@ export class OcrService {
'OCR_API_URL',
'http://localhost:8765'
);
// path ที่ backend เห็น → path ที่ sidecar เห็น (ผ่าน CIFS mount)
this.localUploadBase = this.configService
.get<string>('UPLOAD_PERMANENT_DIR', '/app/uploads/permanent')
.replace(/\/permanent$/, '');
this.sidecarUploadBase = this.configService.get<string>(
'OCR_SIDECAR_UPLOAD_BASE',
'/mnt/uploads'
);
}
/** แปลง local upload path เป็น path ที่ sidecar เห็นผ่าน CIFS mount */
private remapPath(localPath: string): string {
if (this.localUploadBase && localPath.startsWith(this.localUploadBase)) {
return localPath.replace(this.localUploadBase, this.sidecarUploadBase);
}
return localPath;
}
/** ตรวจสอบ text layer ก่อนเลือก OCR slow path */
@@ -54,9 +73,11 @@ export class OcrService {
}
try {
const sidecarPath = this.remapPath(input.pdfPath);
this.logger.debug(`OCR path remap: ${input.pdfPath}${sidecarPath}`);
const response = await axios.post<PaddleOcrResponse>(
`${this.ocrApiUrl}/ocr`,
{ pdfPath: input.pdfPath },
{ pdfPath: sidecarPath },
{ timeout: 90000 }
);
return {