Files
ERitors-Scribe-Desktop/electron/database/System.ts
natreex d46aecc80d Integrate session management, multilingual IPC handlers, and Book database operations
- Add `LocalSystem` with handlers for session retrieval (`userId`, `lang`) and error handling.
- Extend IPC handlers to support multilingual operations (`fr`, `en`) and session data injection
2025-11-18 19:51:17 -05:00

66 lines
2.2 KiB
TypeScript

import { getDatabaseService } from './database.service.js';
import { encryptDataWithUserKey, decryptDataWithUserKey, hashElement } from './encryption.js';
import type { Database } from 'node-sqlite3-wasm';
import crypto from 'crypto';
export default class System {
public static getDb(): Database {
const db: Database | null = getDatabaseService().getDb();
if (!db) {
throw new Error('Database not initialized');
}
return db;
}
public static encryptDataWithUserKey(data: string, userKey: string): string {
return encryptDataWithUserKey(data, userKey);
}
public static decryptDataWithUserKey(encryptedData: string, userKey: string): string {
return decryptDataWithUserKey(encryptedData, userKey);
}
public static createUniqueId(): string {
return crypto.randomUUID();
}
static htmlToText(htmlNode: string): string {
let text: string = htmlNode
.replace(/<\/?p[^>]*>/gi, '\n')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/?(span|h[1-6])[^>]*>/gi, '');
text = text
.replace(/&apos;/g, "'")
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&#39;/g, "'");
text = text.replace(/\r?\n\s*\n/g, '\n');
text = text.replace(/[ \t]+/g, ' ');
return text.trim();
}
public static getCurrentDate(): string {
return new Date().toISOString();
}
static dateToMySqlDate(isoDateString: string): string {
const dateObject: Date = new Date(isoDateString);
function padWithZeroes(value: number): string {
return value.toString().padStart(2, '0');
}
const year: number = dateObject.getFullYear();
const month: string = padWithZeroes(dateObject.getMonth() + 1);
const day: string = padWithZeroes(dateObject.getDate());
return `${year}-${month}-${day}`;
}
public static hashElement(element: string): string {
return hashElement(element);
}
}