Files
ERitors-Scribe-Desktop/electron/database/System.ts
natreex bb331b5c22 Add BooksSyncContext, refine database schema, and enhance synchronization support
- Introduce `BooksSyncContext` for managing book synchronization states (server-only, local-only, to-sync, etc.).
- Remove `UserContext` and related dependencies.
- Refine localization strings (`en.json`) with sync-related updates (e.g., "toSyncFromServer", "toSyncToServer").
- Extend database schema with additional tables and fields for syncing books, chapters, and related entities.
- Add `last_update` fields and update corresponding repository methods to support synchronization logic.
- Enhance IPC handlers with stricter typing, data validation, and sync-aware operations.
2025-12-07 14:36:03 -05:00

71 lines
2.4 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 timeStampInSeconds(): number {
const date:number = new Date().getTime();
return Math.floor(date / 1000);
}
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);
}
}