mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-08 14:29:15 +08:00
fix(ocr): 改进OCR处理中的消息管理和错误处理
在useOcr钩子中统一管理OCR处理的消息提示,并完善错误处理逻辑 移除TranslatePage中重复的消息管理代码,简化OCR处理流程
This commit is contained in:
parent
7afa9f7ab0
commit
1cce646e4b
@ -1,8 +1,11 @@
|
|||||||
|
import { loggerService } from '@logger'
|
||||||
import { useAppSelector } from '@renderer/store'
|
import { useAppSelector } from '@renderer/store'
|
||||||
import { ImageFileMetadata, isImageFile, SupportedOcrFile } from '@renderer/types'
|
import { ImageFileMetadata, isImageFile, SupportedOcrFile } from '@renderer/types'
|
||||||
|
import { uuid } from '@renderer/utils'
|
||||||
|
import { formatErrorMessage } from '@renderer/utils/error'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
export const useImageOcr = () => {}
|
const logger = loggerService.withContext('useOcr')
|
||||||
|
|
||||||
export const useOcr = () => {
|
export const useOcr = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
@ -18,14 +21,24 @@ export const useOcr = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对支持的文件进行OCR识别
|
* 对支持的文件进行OCR识别.
|
||||||
* @param file 支持OCR的文件
|
* @param file 支持OCR的文件
|
||||||
* @returns OCR识别结果的Promise
|
* @returns OCR识别结果的Promise
|
||||||
* @throws 当文件类型不支持时抛出错误
|
* @throws 当文件类型不支持或OCR失败时抛出错误
|
||||||
*/
|
*/
|
||||||
const ocr = async (file: SupportedOcrFile) => {
|
const ocr = async (file: SupportedOcrFile) => {
|
||||||
if (isImageFile(file)) {
|
const key = uuid()
|
||||||
return ocrImage(file)
|
window.message.loading({ content: t('ocr.processing'), key, duration: 0 })
|
||||||
|
try {
|
||||||
|
if (isImageFile(file)) {
|
||||||
|
return ocrImage(file)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('Failed to ocr.', e as Error)
|
||||||
|
window.message.error(t('ocr.error.unknown') + ': ' + formatErrorMessage(e))
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
window.message.destroy(key)
|
||||||
}
|
}
|
||||||
// @ts-expect-error all types should be covered
|
// @ts-expect-error all types should be covered
|
||||||
throw new Error(t('ocr.file.not_supported', { type: file.type }))
|
throw new Error(t('ocr.file.not_supported', { type: file.type }))
|
||||||
|
|||||||
@ -20,7 +20,6 @@ import { setTranslating as setTranslatingAction } from '@renderer/store/runtime'
|
|||||||
import { setTranslatedContent as setTranslatedContentAction } from '@renderer/store/translate'
|
import { setTranslatedContent as setTranslatedContentAction } from '@renderer/store/translate'
|
||||||
import {
|
import {
|
||||||
type AutoDetectionMethod,
|
type AutoDetectionMethod,
|
||||||
isImageFile,
|
|
||||||
isSupportedOcrFile,
|
isSupportedOcrFile,
|
||||||
type Model,
|
type Model,
|
||||||
type TranslateHistory,
|
type TranslateHistory,
|
||||||
@ -446,21 +445,14 @@ const TranslatePage: FC = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extensible
|
// extensible
|
||||||
const shouldOCR = isImageFile(file)
|
const shouldOCR = isSupportedOcrFile(file)
|
||||||
|
|
||||||
if (shouldOCR) {
|
if (shouldOCR) {
|
||||||
if (isSupportedOcrFile(file)) {
|
try {
|
||||||
window.message.loading({ content: t('ocr.processing'), key: 'translate_ocr_processing', duration: 0 })
|
const ocrResult = await ocr(file)
|
||||||
try {
|
setText(ocrResult.text)
|
||||||
const ocrResult = await ocr(file)
|
} finally {
|
||||||
setText(ocrResult.text)
|
// do nothing when failed.
|
||||||
} catch (e) {
|
|
||||||
logger.error('Failed to ocr.', e as Error)
|
|
||||||
window.message.error(t('ocr.error.unknown') + ': ' + formatErrorMessage(e))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// @ts-expect-error all situations covered. just for robustness
|
|
||||||
window.message.error(t('ocr.file.not_supported', { type: file.type }))
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// the threshold may be too large
|
// the threshold may be too large
|
||||||
@ -474,6 +466,8 @@ const TranslatePage: FC = () => {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Failed to read text file.', e as Error)
|
logger.error('Failed to read text file.', e as Error)
|
||||||
window.message.error(t('translate.files.error.unknown') + ': ' + formatErrorMessage(e))
|
window.message.error(t('translate.files.error.unknown') + ': ' + formatErrorMessage(e))
|
||||||
|
} finally {
|
||||||
|
window.message.destroy('translate_files_reading')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -482,8 +476,6 @@ const TranslatePage: FC = () => {
|
|||||||
window.message.error(t('translate.files.error.unknown') + ': ' + formatErrorMessage(e))
|
window.message.error(t('translate.files.error.unknown') + ': ' + formatErrorMessage(e))
|
||||||
} finally {
|
} finally {
|
||||||
setIsProcessing(false)
|
setIsProcessing(false)
|
||||||
window.message.destroy('translate_ocr_processing')
|
|
||||||
window.message.destroy('translate_files_reading')
|
|
||||||
}
|
}
|
||||||
}, [ocr, onSelectFile, selecting, t])
|
}, [ocr, onSelectFile, selecting, t])
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user