Skip to content

Commit 3f4d540

Browse files
fix: database related issues
1 parent 4de96c8 commit 3f4d540

3 files changed

Lines changed: 60 additions & 31 deletions

File tree

electron/main/index.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ process.on("uncaughtException", (error) => {
5858
app.quit();
5959
});
6060

61+
6162
// Defer autoUpdater configuration
6263
function configureAutoUpdater() {
6364
try {
@@ -412,7 +413,7 @@ ipcMain.handle("decrypt", async (event, { packedKeys, method, isSaveHistory }) =
412413
if (decrypted !== "invalid") {
413414
if (isSaveHistory) {
414415
await trySaveHistory(
415-
"dtext",
416+
"dtext_logs",
416417
"success",
417418
"",
418419
"",
@@ -423,7 +424,7 @@ ipcMain.handle("decrypt", async (event, { packedKeys, method, isSaveHistory }) =
423424
} else {
424425
if (isSaveHistory) {
425426
await trySaveHistory(
426-
"dtext",
427+
"dtext_logs",
427428
"fail",
428429
"",
429430
"",
@@ -450,7 +451,7 @@ ipcMain.handle("encrypt", async (event, { text, method, isSaveHistory, isShareab
450451

451452
if (isSaveHistory) {
452453
await trySaveHistory(
453-
"etext",
454+
"etext_logs",
454455
"success",
455456
"",
456457
"",
@@ -491,7 +492,7 @@ ipcMain.handle(
491492
});
492493
if (canceled || !filePaths.length) {
493494
if (isSaveHistory)
494-
await trySaveHistory("efile", "canceled", "", "", method, startTime);
495+
await trySaveHistory("efile_logs", "canceled", "", "", method, startTime);
495496
return [];
496497
}
497498
outputFolder = filePaths[0];
@@ -517,7 +518,7 @@ ipcMain.handle(
517518
if (canceled || !savePath) {
518519
if (isSaveHistory)
519520
await trySaveHistory(
520-
"efile",
521+
"efile_logs",
521522
"canceled",
522523
filePath,
523524
"",
@@ -539,7 +540,7 @@ ipcMain.handle(
539540
);
540541
if (isSaveHistory)
541542
await trySaveHistory(
542-
"efile",
543+
"efile_logs",
543544
"success",
544545
filePath,
545546
encryptedPath,
@@ -558,7 +559,7 @@ ipcMain.handle(
558559
console.error("Encryption failed:", err);
559560
if (isSaveHistory)
560561
await trySaveHistory(
561-
"efile",
562+
"efile_logs",
562563
"canceled",
563564
filePath,
564565
finalOutputPath,
@@ -598,7 +599,7 @@ ipcMain.handle(
598599

599600
if (canceled || filePaths.length === 0) {
600601
if (isSaveHistory) {
601-
await trySaveHistory("dfile", "canceled", "N/A", "N/A", method, startTime);
602+
await trySaveHistory("dfile_logs", "canceled", "N/A", "N/A", method, startTime);
602603
}
603604
return [];
604605
}
@@ -621,7 +622,7 @@ ipcMain.handle(
621622
if (validate === "bad_validate") {
622623
if (isSaveHistory) {
623624
await trySaveHistory(
624-
"dfile",
625+
"dfile_logs",
625626
"fail",
626627
filePath,
627628
filePath,
@@ -635,7 +636,7 @@ ipcMain.handle(
635636
} catch {
636637
if (isSaveHistory) {
637638
await trySaveHistory(
638-
"dfile",
639+
"dfile_logs",
639640
"fail",
640641
filePath,
641642
filePath,
@@ -665,7 +666,7 @@ ipcMain.handle(
665666
if (canceled || !savePath) {
666667
if (isSaveHistory) {
667668
await trySaveHistory(
668-
"dfile",
669+
"dfile_logs",
669670
"canceled",
670671
filePath,
671672
filePath,
@@ -690,7 +691,7 @@ ipcMain.handle(
690691
if (decryptedPath === "bad_decrypt") {
691692
if (isSaveHistory) {
692693
await trySaveHistory(
693-
"dfile",
694+
"dfile_logs",
694695
"fail",
695696
filePath,
696697
"",
@@ -701,7 +702,7 @@ ipcMain.handle(
701702
} else {
702703
if (isSaveHistory) {
703704
await trySaveHistory(
704-
"dfile",
705+
"dfile_logs",
705706
"success",
706707
filePath,
707708
decryptedPath,
@@ -724,7 +725,7 @@ ipcMain.handle(
724725
console.error("Decryption failed:", err);
725726
if (isSaveHistory) {
726727
await trySaveHistory(
727-
"dfile",
728+
"dfile_logs",
728729
"canceled",
729730
filePath,
730731
finalOutputPath,
@@ -773,7 +774,7 @@ ipcMain.handle(
773774
results.push({ inputPath: imagePath, output: "canceled" });
774775
if (isSaveHistory)
775776
await trySaveHistory(
776-
"steg-in",
777+
"steg-in_logs",
777778
"canceled",
778779
imagePath,
779780
"N/A",
@@ -790,7 +791,7 @@ ipcMain.handle(
790791
results.push({ inputPath: imagePath, output: result.response });
791792
if (isSaveHistory)
792793
await trySaveHistory(
793-
"steg-in",
794+
"steg-in_logs",
794795
"fail",
795796
imagePath,
796797
result.response,
@@ -803,7 +804,7 @@ ipcMain.handle(
803804
results.push({ inputPath: imagePath, output: result.outputPath });
804805
if (isSaveHistory)
805806
await trySaveHistory(
806-
"steg-in",
807+
"steg-in_logs",
807808
"success",
808809
imagePath,
809810
result.outputPath,
@@ -825,7 +826,7 @@ ipcMain.handle(
825826
console.error("Data hiding error:", error);
826827
results.push({ inputPath: imagePath, output: "fail" });
827828
if (isSaveHistory)
828-
await trySaveHistory("steg-in", "fail", imagePath, "N/A", method, startTime);
829+
await trySaveHistory("steg-in_logs", "fail", imagePath, "N/A", method, startTime);
829830
}
830831
}
831832

@@ -858,7 +859,7 @@ ipcMain.handle(
858859

859860
if (canceled || filePaths.length === 0) {
860861
if (isSaveHistory) {
861-
await trySaveHistory("steg-out", "canceled", "N/A", "N/A", method, startTime);
862+
await trySaveHistory("steg-out_logs", "canceled", "N/A", "N/A", method, startTime);
862863
}
863864
return [];
864865
}
@@ -874,7 +875,7 @@ ipcMain.handle(
874875
results.push({ inputPath: filePath, output: "BAD_EXTRACT" });
875876
if (isSaveHistory) {
876877
await trySaveHistory(
877-
"steg-out",
878+
"steg-out_logs",
878879
"fail",
879880
filePath,
880881
"N/A",
@@ -905,7 +906,7 @@ ipcMain.handle(
905906
results.push({ inputPath: filePath, output: "canceled" });
906907
if (isSaveHistory) {
907908
await trySaveHistory(
908-
"steg-out",
909+
"steg-out_logs",
909910
"canceled",
910911
filePath,
911912
"N/A",
@@ -932,7 +933,7 @@ ipcMain.handle(
932933

933934
if (isSaveHistory) {
934935
await trySaveHistory(
935-
"steg-out",
936+
"steg-out_logs",
936937
"success",
937938
filePath,
938939
movedPaths.join(","),
@@ -954,7 +955,7 @@ ipcMain.handle(
954955
console.error("Extraction failed:", err);
955956
results.push({ inputPath: filePath, output: "fail" });
956957
if (isSaveHistory) {
957-
await trySaveHistory("steg-out", "fail", filePath, "N/A", method, startTime);
958+
await trySaveHistory("steg-out_logs", "fail", filePath, "N/A", method, startTime);
958959
}
959960
}
960961
}

electron/main/utils/db/DBService.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ export async function saveKEMKeyToDB(secret: string, pub: string, recipient: str
4848
await ensureDBReady();
4949
await run(`INSERT OR REPLACE INTO kem_keys (id, secret, public, recipient) VALUES (0, ?, ?, ?)`, secret, pub, recipient);
5050
}
51+
5152
export async function getKeys() {
5253
await ensureDBReady();
5354
return all(`SELECT * FROM kem_keys`);
5455
}
56+
5557
export async function saveHistory(table: string, columns: string[], values: any[]) {
5658
await ensureDBReady();
5759
const id = uuidv4();
@@ -61,18 +63,22 @@ export async function saveHistory(table: string, columns: string[], values: any[
6163
const placeholders = cols.map(() => "?").join(",");
6264
return run(`INSERT INTO ${table} (${cols.join(",")}) VALUES (${placeholders})`, ...vals);
6365
}
66+
6467
export async function getLogs(table: string) {
6568
await ensureDBReady();
6669
return all(`SELECT * FROM ${table} ORDER BY timestamp DESC`);
6770
}
71+
6872
export async function deleteLogById(table: string, id: string) {
6973
await ensureDBReady();
7074
return run(`DELETE FROM ${table} WHERE id = ?`, id);
7175
}
76+
7277
export async function deleteAllLogs(table: string) {
7378
await ensureDBReady();
7479
return run(`DELETE FROM ${table}`);
7580
}
81+
7682
export async function nukeAllTables() {
7783
await ensureDBReady();
7884
const names: string[] = [];
@@ -212,11 +218,11 @@ async function performResetAndReinit() {
212218
safeWriteLog(`[DB RESET] unlink error: ${err}`);
213219
}
214220

215-
// Reset in-memory state so ensureDBReady will recreate
221+
// Reset in-memory state so ensureDBReady will re-create
216222
db = null;
217223
initPromise = null;
218224

219-
// Reinitialize (generates a new key inside init)
225+
// Reinitialize (this generates a new key inside init)
220226
await ensureDBReady();
221227
}
222228

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1+
import fs from "fs-extra";
12
import { saveHistory } from "./db/DBService";
23
import { safeWriteLog } from "./writeLog";
34

45
export async function trySaveHistory(
5-
operation: "dtext" | "dfile" | "etext" | "efile" | "steg-in" | "steg-out",
6+
operation: "dtext_logs" | "dfile_logs" | "etext_logs" | "efile_logs" | "steg-in_logs" | "steg-out_logs",
67
status: "success" | "fail" | "canceled",
78
inputPath: string,
89
outputPath: string,
910
algorithm: string,
1011
startTime: number
1112
) {
13+
const duration = Date.now() - startTime;
14+
15+
let columns: string[] = ["algorithm", "status", "duration"];
16+
let values: any[] = [algorithm, status, duration];
17+
let outputStats;
18+
let inputStats;
19+
20+
try {
21+
inputStats = await fs.stat(inputPath);
22+
} catch (err) {
23+
inputStats = { input_path: "N\A", outputPath: "N\A", output_size: 0 };
24+
}
25+
26+
try {
27+
outputStats = await fs.stat(outputPath);
28+
} catch (err) {
29+
outputStats = { input_path: "N\A", outputPath: "N\A", output_size: 0 };
30+
}
31+
32+
if (["efile_logs", "dfile_logs", "steg-in_logs", "steg-out_logs"].includes(operation)) {
33+
columns = ["input_path", "output_path", "input_size", "output_size", "algorithm", "status", "duration"];
34+
values = [inputPath, outputPath, inputStats.size, outputStats.size, algorithm, status, duration];
35+
}
36+
1237
try {
13-
await saveHistory(operation, inputPath, outputPath, algorithm, {
14-
duration: Date.now() - startTime,
15-
status,
16-
});
38+
await saveHistory(operation, columns, values);
1739
} catch (err) {
18-
console.error(`Failed to save history [${status}]:`, err);
40+
safeWriteLog(`Failed to save history [${status}]: ${err}`);
1941
}
2042
}

0 commit comments

Comments
 (0)