Skip to content

Commit

Permalink
fix: 简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Jan 4, 2025
1 parent c58a26e commit c46ec32
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/onebot/action/extends/OCRImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ class OCRImageBase extends OneBotAction<Payload, any> {
payloadSchema = SchemaData;

async _handle(payload: Payload) {
const { path, success } = (await uriToLocalFile(this.core.NapCatTempPath, payload.image));
const { path, success } = await uriToLocalFile(this.core.NapCatTempPath, payload.image);
if (!success) {
throw new Error(`OCR ${payload.image}失败,image字段可能格式不正确`);
throw new Error(`OCR ${payload.image}失败, image字段可能格式不正确`);
}
if (path) {
await checkFileExist(path, 5000); // 避免崩溃
const ret = await this.core.apis.SystemApi.ocrImage(path);
fs.unlink(path, () => { });

if (!ret) {
throw new Error(`OCR ${payload.image}失败`);
try {
await checkFileExist(path, 5000); // 避免崩溃
const ret = await this.core.apis.SystemApi.ocrImage(path);
if (!ret) {
throw new Error(`OCR ${payload.image}失败`);
}
return ret.result;
} finally {
fs.unlink(path, () => { });
}
return ret.result;
}
fs.unlink(path, () => { });
throw new Error(`OCR ${payload.image}失败,文件可能不存在`);
throw new Error(`OCR ${payload.image}失败, 文件可能不存在`);
}
}

Expand All @@ -39,4 +40,4 @@ export class OCRImage extends OCRImageBase {

export class IOCRImage extends OCRImageBase {
actionName = ActionName.IOCRImage;
}
}

0 comments on commit c46ec32

Please sign in to comment.