From f3da4a6e36d7a9e908dee87365ccc9e0f4ddcd55 Mon Sep 17 00:00:00 2001 From: icarus Date: Sat, 23 Aug 2025 13:45:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ocr):=20=E7=BB=9F=E4=B8=80=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20SupportedOcrFile=20=E7=B1=BB=E5=9E=8B=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=20FileMetadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 OCR 服务及其 Tesseract 实现,使用 SupportedOcrFile 类型替代原有的 FileMetadata 类型,以提高类型安全性和一致性。同时在 OcrService 中添加重复注册的警告日志。 --- src/main/services/ocr/OcrService.ts | 10 ++++++++-- src/main/services/ocr/tesseract/TesseractService.ts | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/services/ocr/OcrService.ts b/src/main/services/ocr/OcrService.ts index d709115b9c..73907574b2 100644 --- a/src/main/services/ocr/OcrService.ts +++ b/src/main/services/ocr/OcrService.ts @@ -1,13 +1,19 @@ -import { BuiltinOcrProviderIds, FileMetadata, OcrProvider, OcrResult, SupportedOcrFile } from '@types' +import { loggerService } from '@logger' +import { BuiltinOcrProviderIds, OcrProvider, OcrResult, SupportedOcrFile } from '@types' import { tesseractService } from './tesseract/TesseractService' -type OcrHandler = (file: FileMetadata) => Promise +type OcrHandler = (file: SupportedOcrFile) => Promise + +const logger = loggerService.withContext('OcrService') export class OcrService { private registry: Map = new Map() register(providerId: string, handler: OcrHandler): void { + if (this.registry.has(providerId)) { + logger.warn(`Provider ${providerId} has existing handler. Overwrited.`) + } this.registry.set(providerId, handler) } diff --git a/src/main/services/ocr/tesseract/TesseractService.ts b/src/main/services/ocr/tesseract/TesseractService.ts index 06f460dfea..77225d4afe 100644 --- a/src/main/services/ocr/tesseract/TesseractService.ts +++ b/src/main/services/ocr/tesseract/TesseractService.ts @@ -1,7 +1,7 @@ import { loggerService } from '@logger' import { getIpCountry } from '@main/utils/ipService' import { MB } from '@shared/config/constant' -import { FileMetadata, ImageFileMetadata, isImageFile, OcrResult } from '@types' +import { ImageFileMetadata, isImageFile, OcrResult, SupportedOcrFile } from '@types' import { app } from 'electron' import fs from 'fs' import path from 'path' @@ -149,7 +149,7 @@ export class TesseractService { return { text: result.data.text } } - async ocr(file: FileMetadata): Promise { + async ocr(file: SupportedOcrFile): Promise { if (!isImageFile(file)) { throw new Error('Only image files are supported currently') }