使用对象数据库进行重载

This commit is contained in:
锦恢 2025-04-26 14:07:36 +08:00
parent 5fc7a9e468
commit 91cff239ab
3 changed files with 96 additions and 19 deletions

View File

@ -2,12 +2,15 @@ import sqlite3 from 'sqlite3';
import { open } from 'sqlite'; import { open } from 'sqlite';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs';
interface Entity { interface Entity {
id: string | number; id: string | number;
[key: string]: any; [key: string]: any;
} }
const dbConnections: Record<string, any> = {};
export class LocalDB<T extends Entity> { export class LocalDB<T extends Entity> {
private db: any; private db: any;
@ -16,14 +19,17 @@ export class LocalDB<T extends Entity> {
} }
private async init() { private async init() {
// 默认存储在用户目录的 .openmcp 目录下
const homedir = os.homedir(); const homedir = os.homedir();
const filename = path.join(homedir, '.openmcp', 'index.db'); const filename = path.join(homedir, '.openmcp', 'index.db');
this.db = await open({ if (!dbConnections[filename]) {
dbConnections[filename] = await open({
filename, filename,
driver: sqlite3.Database driver: sqlite3.Database
}); });
}
this.db = dbConnections[filename];
await this.db.exec(` await this.db.exec(`
CREATE TABLE IF NOT EXISTS ${this.tableName} ( CREATE TABLE IF NOT EXISTS ${this.tableName} (
@ -67,3 +73,76 @@ export class LocalDB<T extends Entity> {
await this.db.close(); await this.db.close();
} }
} }
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<Buffer | null> {
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<void> {
const filePath = path.join(this.#storageHome, filename);
await fs.promises.writeFile(filePath, data, options);
}
public async delete(filename: string): Promise<void> {
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<SettingItem>('setting');
export const ocrDB = new LocalDB<OcrItem>('ocr');

View File

@ -2,28 +2,20 @@ import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { diskStorage } from './db';
export function saveBase64ImageData( export function saveBase64ImageData(
base64String: string, base64String: string,
mimeType: string mimeType: string
): string { ): string {
const homedir = os.homedir();
const imageStorageFolder = path.join(homedir, '.openmcp', 'storage');
// 确保存储目录存在
if (!fs.existsSync(imageStorageFolder)) {
fs.mkdirSync(imageStorageFolder, { recursive: true });
}
// 从 base64 字符串中提取数据部分 // 从 base64 字符串中提取数据部分
const base64Data = base64String.replace(/^data:.+;base64,/, ''); const base64Data = base64String.replace(/^data:.+;base64,/, '');
// 生成唯一文件名 // 生成唯一文件名
const fileName = `${uuidv4()}.${mimeType.split('/')[1]}`; const fileName = `${uuidv4()}.${mimeType.split('/')[1]}`;
const filePath = path.join(imageStorageFolder, fileName);
// 将 base64 数据写入文件 diskStorage.setSync(fileName, base64Data, { encoding: 'base64' });
fs.writeFileSync(filePath, base64Data, { encoding: 'base64' });
return fileName; return fileName;
} }

View File

@ -1,7 +1,5 @@
import Tesseract from 'tesseract.js'; import Tesseract from 'tesseract.js';
export async function tesseractOCR( export async function tesseractOCR(
imagePath: string, imagePath: string,
logger: (message: Tesseract.LoggerMessage) => void, logger: (message: Tesseract.LoggerMessage) => void,
@ -22,3 +20,11 @@ export async function tesseractOCR(
} }
return '无法识别图片'; return '无法识别图片';
} }
export async function ocr(
filename: string,
logger: (message: Tesseract.LoggerMessage) => void,
lang: string = 'eng+chi_sim'
) {
}