Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/bridges/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
TouchBarTexts,
WindowStateListenerType,
} from "../schema-types";
import { CustomArgs } from "../general-types";
import { type CustomArgs, type AutosaveNotifyResponse } from "../general-types";
import { IObjectWithKey } from "@fluentui/react";

const utilsBridge = {
Expand Down Expand Up @@ -64,6 +64,27 @@ const utilsBridge = {
)) as string;
},

autosaveBackupData: (writeContent: string): Promise<void> => {
return ipcRenderer.invoke("write-autosave", writeContent);
},

getLastAutosaveTimestamp: (): Promise<number | undefined> => {
return ipcRenderer.invoke("get-last-autosave-timestamp");
},

notifyOfRecoveryNeeded: (
title: string,
detail: string,
buttons: string[],
): Promise<AutosaveNotifyResponse> => {
return ipcRenderer.invoke(
"notify-of-recovery-needed",
title,
detail,
buttons,
);
},

getCacheSize: async (): Promise<number> => {
return await ipcRenderer.invoke("get-cache");
},
Expand Down
5 changes: 5 additions & 0 deletions src/general-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export type CustomArgs = {
// Force using the window frame, regardless of platform.
forceFrame: boolean;
};

export type AutosaveNotifyResponse =
| { response: "ignored" }
| { response: "deleted" }
| { response: "recovered"; contents: string };
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { initApp, openTextMenu } from "./scripts/models/app";
import { startPool } from "./scripts/fetch-pool";
import { rootStore } from "./scripts/reducer";
import { initThemeUpdateListener } from "./scripts/settings";
import { handleAutosaves } from "./scripts/autosave";

startPool(); // Start global fetch pool.
initThemeUpdateListener();
Expand All @@ -20,6 +21,7 @@ applyAnimationMotionPref();
applyThemeSettings();
initializeIcons("icons/");

handleAutosaves();
rootStore.dispatch(initApp());

window.utils.addMainContextListener((pos, text) => {
Expand Down
7 changes: 6 additions & 1 deletion src/main/pure-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/**
* pure-utils.ts is a collection of utilities that are self-contained, and require
* no inter-package imports. None are stateful.
*/

/** Ensure we don't have too long of a message. */
export function trimContent(
s: string,
charLen: number = 512,
lineLen: number = 5,
lineLen: number = 10,
): string {
return limitLines(trimString(s, charLen), lineLen);
}
Expand Down
90 changes: 88 additions & 2 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import {
clipboard,
systemPreferences,
} from "electron";
import path from "path";
import { getFonts } from "font-list";
import { promises, writeFile } from "fs";

import pkg from "../../package.json";
import { WindowManager } from "./window";
import { trimContent, trimString } from "./pure-utils";
import { promises, writeFile } from "fs";
import { ImageCallbackTypes, TouchBarTexts } from "../schema-types";
import { initMainTouchBar } from "./touchbar";
import { getFonts } from "font-list";
import { type AutosaveNotifyResponse } from "../general-types";

function getAutosavePath() {
const userDataPath = app.getPath("userData");
return path.join(userDataPath, "autosaves", "ffr_autosave.frdata");
}

export function setUtilsListeners(manager: WindowManager) {
async function openExternal(url: string, background = false) {
Expand Down Expand Up @@ -159,6 +167,84 @@ export function setUtilsListeners(manager: WindowManager) {
},
);

ipcMain.handle("write-autosave", async (_, writeContent: string) => {
const autosavePath = getAutosavePath();
try {
const dirStat = await promises.stat(path.dirname(autosavePath), {
throwIfNoEntry: false,
});
if (dirStat == undefined) {
await promises.mkdir(path.dirname(autosavePath));
}
await promises.writeFile(getAutosavePath(), writeContent);
} catch (err: any) {
console.error("Failed to write autosave", err);
}
});

ipcMain.handle(
"get-last-autosave-timestamp",
async (): Promise<number | undefined> => {
const autosavePath = getAutosavePath();
try {
const result = await promises.stat(autosavePath, {
throwIfNoEntry: false,
});
return result?.mtimeMs;
} catch (err: any) {
console.error(`Failed to stat ${autosavePath}`, err);
return undefined;
}
},
);

ipcMain.handle(
"notify-of-recovery-needed",
async (
_,
title: string,
detail: string,
buttons: string[],
): Promise<AutosaveNotifyResponse> => {
if (manager.hasWindow()) {
const dialogResponse = await dialog.showMessageBox(
manager.mainWindow,
{
type: "error",
title: title,
message: title,
detail: detail,
buttons: buttons,
cancelId: 1,
defaultId: 2,
},
);
const autosavePath = getAutosavePath();
switch (dialogResponse.response) {
case 0:
await promises.rm(autosavePath);
return { response: "deleted" };
case 1:
return { response: "ignored" };
case 2:
const contents = await promises.readFile(autosavePath, {
encoding: "utf8",
});
return { response: "recovered", contents: contents };
default:
// Unreachable.
throw Error("Unreachable");
}
} else {
dialog.showErrorBox(
title,
"Unable to recover automatically, but autosave exists.",
);
return { response: "ignored" };
}
},
);

ipcMain.handle("get-cache", async () => {
return await session.defaultSession.getCacheSize();
});
Expand Down
97 changes: 97 additions & 0 deletions src/scripts/autosave.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Autosaves have somewhat complicated logic, because it would be really bad
* if we autosaved over a good save with a bad save. So, we have to put in
* some very reliable heuristics to figure out if we recently lost some data.
*
* This is the purpose of the `integrity` table in the IndexedDB database.
* Specifically, if we see we had an autosave available, yet the `integrity`
* is empty, it means that we wiped that table. Since that never happens in
* the renderer code paths, it's very likely there was a corruption and it
* was wiped silently by Electron on `IndexedDB.open`. We then can condition
* on that, and recommend recovery.
*/

import intl from "react-intl-universal";

import { fluentDB, type ItemEntry, type SourceEntry } from "./db";
import { loadFromConfig } from "./settings";

const ONE_DAY = 24 * 60 * 60 * 1000;

export enum AutosaveResponse {
SUCCESS = 0,
SKIPPED = 1,
INTEGRITY_FAILED = 2,
}

export async function handleAutosaves(): Promise<void> {
const autosaveResp = await renewAutosave();
if (autosaveResp === AutosaveResponse.INTEGRITY_FAILED) {
// Dangerous area! Everything in the DB is invalid!
const title = intl.get("autosave.corruption.title");
const detail = intl.get("autosave.corruption.detail");
const buttons = [
intl.get("autosave.corruption.delete"),
intl.get("close"),
intl.get("autosave.corruption.recover"),
];
const response = await window.utils.notifyOfRecoveryNeeded(
title,
detail,
buttons,
);
if (response.response === "recovered") {
// Resets integrity table itself.
await loadFromConfig(JSON.parse(response.contents));
} else if (response.response === "deleted") {
fluentDB.integrity.put({ index: 0, timestamp: new Date() });
}
} else {
fluentDB.integrity.put({ index: 0, timestamp: new Date() });
}
}

/**
* Renew the autosave file in userData if it's been long enough.
*/
async function renewAutosave(): Promise<AutosaveResponse> {
const autosaveMTimeMs = await window.utils.getLastAutosaveTimestamp();
const integrityVals = await fluentDB.integrity.toArray();
if (autosaveMTimeMs !== undefined && integrityVals.length === 0) {
console.error(
"Prior autosave exists, and current IndexedDB" +
" integrity check failed. Database is corrupt.",
);
return AutosaveResponse.INTEGRITY_FAILED;
}

const autosaveStartTime = Date.now();
if (autosaveStartTime < autosaveMTimeMs + ONE_DAY) {
console.debug("Autosave still current, not rewriting.");
return AutosaveResponse.SKIPPED;
}
let sources: SourceEntry[];
let items: ItemEntry[];
const transaction = fluentDB.transaction(
"r",
fluentDB.sources,
fluentDB.items,
async () => {
sources = await fluentDB.sources.toArray();
items = await fluentDB.items.toArray();
},
);
const output = window.settings.getAll();
await transaction;
output["database"] = {
sources: sources,
items: items,
};
await window.utils.autosaveBackupData(JSON.stringify(output));
const autosaveEndTime = Date.now();
console.debug(
"Completed autosave in " +
`${Math.round(autosaveEndTime - autosaveStartTime)}ms.`,
);
return AutosaveResponse.SUCCESS;
}
22 changes: 21 additions & 1 deletion src/scripts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { SourceRule } from "./models/rule";
import { Dexie, type EntityTable, type Transaction } from "dexie";
import type { ThumbnailAttributes } from "./thumb-utils";

// Used to determine if the database has been initialised
// or corrupted.
export interface IntegrityEntry {
index: number;
timestamp: Date;
}

export interface SourceEntry {
sid: number;
url: string;
Expand Down Expand Up @@ -37,9 +44,11 @@ export interface ItemEntry {
}

export const fluentDB = new Dexie("MainDB") as Dexie & {
integrity: EntityTable<IntegrityEntry, "index">;
sources: EntityTable<SourceEntry, "sid">;
items: EntityTable<ItemEntry, "iid">;
};

fluentDB.version(4).stores({
sources: `++sid, &url`,
items: `++iid, source, date, serviceRef`,
Expand Down Expand Up @@ -77,6 +86,13 @@ fluentDB
})
.upgrade(migrateOpenTarget);

// Introduce Database Integrity Validation Table.
fluentDB.version(9).stores({
integrity: `++index`,
sources: `++sid, &url`,
items: `++iid, source, date, serviceRef`,
});

export async function calculateItemSize(): Promise<number> {
await fluentDB.open();
let result = 0;
Expand Down Expand Up @@ -294,7 +310,11 @@ function byteLength(str: string) {
}

export async function init() {
await migrateLovefieldSourcesDB("sourcesDB", 3);
try {
await migrateLovefieldSourcesDB("sourcesDB", 3);
} catch (e: any) {
console.error("Error migrating sources DB", e);
}
try {
await migrateLovefieldItemsDB("itemsDB", 1);
} catch (e) {
Expand Down
8 changes: 8 additions & 0 deletions src/scripts/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@
"Vertical": "Vertical",
"font": "Font"
},
"autosave": {
"corruption": {
"title": "Database corruption detected",
"detail": "IndexedDB database appears to be corrupted, and an autosave file has been detected.\n\nRecover from autosave?",
"delete": "Delete autosave",
"recover": "Recover"
}
},
"context": {
"share": "Share",
"read": "Read",
Expand Down
Loading
Loading