Skip to content

Commit

Permalink
feat(script): 新增OCS 桌面版 MAC 版本的 OCR 适配
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Oct 21, 2023
1 parent e08e1cc commit 1b5f72a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package-lock.json

**/*.zip

**/*/.DS_Store

# tsc

lib/
Expand Down
10 changes: 7 additions & 3 deletions packages/app/src/utils/ocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function ocr(base64: string) {
const img = path.join(img_cache, uuid + '.png');
writeFile(img, base64, 'base64', () => {
// 要使用 "" 去包裹路径,防止出现空格
const cmd = [`"${path.join(getOcrFolder(), './ocr.exe')}"`, '--ocr', `"${img}"`].join(' ');
const cmd = [`".${path.join(getOcrFolder(), getOCRFileName())}"`, '--ocr', `"${img}"`].join(' ');
logger.log('cmd', cmd);

child_process.exec(cmd, (err, stdout, stderr) => {
Expand Down Expand Up @@ -65,7 +65,7 @@ export function det(det_target_base64: string, det_bg_base64: string) {
writeFileSync(img2, det_bg_base64, 'base64');

const cmd = [
`"${path.join(getOcrFolder(), './ocr.exe')}"`,
`"${path.join(getOcrFolder(), getOCRFileName())}"`,
'--det-target',
`"${img1}"`,
'--det-bg',
Expand All @@ -91,5 +91,9 @@ export function det(det_target_base64: string, det_bg_base64: string) {

/** 判断是否能够进行验证码识别 */
export function canOCR() {
return existsSync(path.join(getOcrFolder(), './ocr.exe'));
return existsSync(path.join(getOcrFolder(), getOCRFileName()));
}

function getOCRFileName() {
return process.platform === 'win32' ? './ocr.exe' : './ocr';
}
5 changes: 5 additions & 0 deletions packages/common/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export interface ResourceFile {
description?: string;
icon?: string;
homepage?: string;
platforms?: {
// eslint-disable-next-line no-undef
platform: NodeJS.Platform;
url: string;
}[];
}

/** 资源组 */
Expand Down
3 changes: 2 additions & 1 deletion packages/web/src/utils/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ function registerRemote<T>(eventName: string) {
}

function send(channel: string, args: any[]): Promise<any> {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
ipcRenderer.once(args[0], (e: any, ...respondArgs) => {
if (respondArgs[0].error) {
console.log({ respondArgs, channel, args });
notify('remote 模块错误', respondArgs[0].error, 'remote', { copy: true, type: 'error' });
reject(String(respondArgs[0].error));
} else {
resolve(respondArgs[0].data);
}
Expand Down
25 changes: 17 additions & 8 deletions packages/web/src/utils/resources.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ export class ResourceLoader {
if (await remote.fs.call('existsSync', downloadPath)) {
await remote.fs.call('unlinkSync', downloadPath);
}
const platform = await remote.methods.call('getPlatform');
const url = platform === 'win32' ? file.url : file.platforms?.find((p) => p.platform === platform)?.url || '';
if (!url) {
throw new Error('资源下载失败,路径为空');
}
// 下载
await remote.methods.call('download', 'download-file-' + file.id, file.url, downloadPath);
await remote.methods.call('download', 'download-file-' + file.id, url, downloadPath);
}

/** 解压资源 */
Expand Down Expand Up @@ -108,15 +113,19 @@ export class ResourceLoader {
const files: LocalResourceFile[] = [];
// @ts-ignore
const groupnames: string[] = await remote.fs.call('readdirSync', this.resourceRootPath);

for (const groupname of groupnames) {
// TODO 检测是否是文件夹,如果不是则忽略
for (const groupname of groupnames.filter((g) => g !== '.DS_Store')) {
const folder = await remote.path.call('join', this.resourceRootPath, groupname);
if (await remote.fs.call('existsSync', folder)) {
// @ts-ignore
const filenames: string[] = await remote.fs.call('readdirSync', folder);
for (const filename of filenames) {
const path = await remote.path.call('join', folder, filename);
files.push({ groupname, filename, path });
try {
// @ts-ignore
const filenames: string[] = await remote.fs.call('readdirSync', folder);
for (const filename of filenames) {
const path = await remote.path.call('join', folder, filename);
files.push({ groupname, filename, path });
}
} catch (e) {
console.error(e);
}
}
}
Expand Down

0 comments on commit 1b5f72a

Please # to comment.