From 91cff239ab84dd7c16fb57969cccb49d4362c42f Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Sat, 26 Apr 2025 14:07:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=AF=B9=E8=B1=A1=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=BF=9B=E8=A1=8C=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/src/hook/db.ts | 93 +++++++++++++++++++++++++++++++--- service/src/hook/mcp-server.ts | 12 +---- service/src/hook/ocr.ts | 10 +++- 3 files changed, 96 insertions(+), 19 deletions(-) diff --git a/service/src/hook/db.ts b/service/src/hook/db.ts index 0f1838a..74724b3 100644 --- a/service/src/hook/db.ts +++ b/service/src/hook/db.ts @@ -2,12 +2,15 @@ import sqlite3 from 'sqlite3'; import { open } from 'sqlite'; import * as os from 'os'; import * as path from 'path'; +import * as fs from 'fs'; interface Entity { id: string | number; [key: string]: any; } +const dbConnections: Record = {}; + export class LocalDB { private db: any; @@ -16,14 +19,17 @@ export class LocalDB { } private async init() { - // 默认存储在用户目录的 .openmcp 目录下 const homedir = os.homedir(); const filename = path.join(homedir, '.openmcp', 'index.db'); - - this.db = await open({ - filename, - driver: sqlite3.Database - }); + + if (!dbConnections[filename]) { + dbConnections[filename] = await open({ + filename, + driver: sqlite3.Database + }); + } + + this.db = dbConnections[filename]; await this.db.exec(` CREATE TABLE IF NOT EXISTS ${this.tableName} ( @@ -66,4 +72,77 @@ export class LocalDB { async close(): Promise { await this.db.close(); } -} \ No newline at end of file +} + + +class DiskStorage { + #storageHome: string; + + constructor() { + const homedir = os.homedir(); + const imageStorageFolder = path.join(homedir, '.openmcp', 'storage'); + + // 确保存储目录存在 + if (!fs.existsSync(imageStorageFolder)) { + fs.mkdirSync(imageStorageFolder, { recursive: true }); + } + + this.#storageHome = imageStorageFolder; + } + + public async get(filename: string): Promise { + const filePath = path.join(this.#storageHome, filename); + if (fs.existsSync(filePath)) { + return fs.promises.readFile(filePath); + } + return null; + } + + public async set(filename: string, data: string | Buffer, options?: fs.WriteFileOptions): Promise { + const filePath = path.join(this.#storageHome, filename); + await fs.promises.writeFile(filePath, data, options); + } + + public async delete(filename: string): Promise { + const filePath = path.join(this.#storageHome, filename); + if (fs.existsSync(filePath)) { + await fs.promises.unlink(filePath); + } + } + + public getSync(filename: string): Buffer | null { + const filePath = path.join(this.#storageHome, filename); + if (fs.existsSync(filePath)) { + return fs.readFileSync(filePath); + } + return null; + } + + public setSync(filename: string, data: string | Buffer, options?: fs.WriteFileOptions): void { + const filePath = path.join(this.#storageHome, filename); + fs.writeFileSync(filePath, data, options); + } + + public deleteSync(filename: string): void { + const filePath = path.join(this.#storageHome, filename); + if (fs.existsSync(filePath)) { + fs.unlinkSync(filePath); + } + } +} + +interface SettingItem extends Entity { + MODEL_INDEX: number; + [key: string]: any; +} + +interface OcrItem extends Entity { + filename: string; + text?: string; + textCreateTime: number; +} + +export const diskStorage = new DiskStorage(); + +export const settingDB = new LocalDB('setting'); +export const ocrDB = new LocalDB('ocr'); \ No newline at end of file diff --git a/service/src/hook/mcp-server.ts b/service/src/hook/mcp-server.ts index b58b4a7..fa656a5 100644 --- a/service/src/hook/mcp-server.ts +++ b/service/src/hook/mcp-server.ts @@ -2,28 +2,20 @@ import * as os from 'os'; import * as path from 'path'; import * as fs from 'fs'; import { v4 as uuidv4 } from 'uuid'; +import { diskStorage } from './db'; export function saveBase64ImageData( base64String: string, mimeType: string ): string { - const homedir = os.homedir(); - const imageStorageFolder = path.join(homedir, '.openmcp', 'storage'); - - // 确保存储目录存在 - if (!fs.existsSync(imageStorageFolder)) { - fs.mkdirSync(imageStorageFolder, { recursive: true }); - } // 从 base64 字符串中提取数据部分 const base64Data = base64String.replace(/^data:.+;base64,/, ''); // 生成唯一文件名 const fileName = `${uuidv4()}.${mimeType.split('/')[1]}`; - const filePath = path.join(imageStorageFolder, fileName); - // 将 base64 数据写入文件 - fs.writeFileSync(filePath, base64Data, { encoding: 'base64' }); + diskStorage.setSync(fileName, base64Data, { encoding: 'base64' }); return fileName; } diff --git a/service/src/hook/ocr.ts b/service/src/hook/ocr.ts index 1d5af27..d191881 100644 --- a/service/src/hook/ocr.ts +++ b/service/src/hook/ocr.ts @@ -1,7 +1,5 @@ import Tesseract from 'tesseract.js'; - - export async function tesseractOCR( imagePath: string, logger: (message: Tesseract.LoggerMessage) => void, @@ -21,4 +19,12 @@ export async function tesseractOCR( console.error('OCR error:', error); } return '无法识别图片'; +} + +export async function ocr( + filename: string, + logger: (message: Tesseract.LoggerMessage) => void, + lang: string = 'eng+chi_sim' +) { + } \ No newline at end of file