Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): open a resources folder moved outside the data directory

Elian Doran0a32a4ba87ca
diff --git a/apps/desktop/src/services/shell.spec.ts b/apps/desktop/src/services/shell.spec.ts
index c335e1575d..e0d66562e1 100644
--- a/apps/desktop/src/services/shell.spec.ts
+++ b/apps/desktop/src/services/shell.spec.ts
@@ -16,7 +16,14 @@ vi.mock("electron", () => ({
}
}));
+const resourcesDir = fs.mkdtempSync(path.join(os.tmpdir(), "trilium-open-path-resources-"));
+vi.mock("@triliumnext/server/src/services/data_dir.js", async (importOriginal) => {
+ const actual = await importOriginal<{ default: object }>();
+ return { default: { ...actual.default, RESOURCES_DIR: resourcesDir } };
+});
+
const {
+ getConfiguredOpenRoots,
validateDownloadUrl,
validateOpenCustomPath,
validateOpenExternalUrl,
@@ -205,6 +212,19 @@ describe("validateOpenPath", () => {
});
});
+ describe("resources directory root", () => {
+ afterAll(() => fs.rmSync(resourcesDir, { recursive: true, force: true }));
+
+ it("accepts a resources directory moved outside the data directory, and what it holds", () => {
+ const resource = path.join(resourcesDir, "transcription.parakeet-0123456789ab");
+ fs.mkdirSync(resource);
+
+ expect(validateOpenPath(resource, dataDir, tmpDir, ...getConfiguredOpenRoots()))
+ .toBe(path.resolve(resource));
+ expect(() => validateOpenPath(resource, dataDir, tmpDir)).toThrow(/outside data dir/);
+ });
+ });
+
describe("a file as a root of its own (the database, wherever it is kept)", () => {
let documentPath: string;
diff --git a/apps/desktop/src/services/shell.ts b/apps/desktop/src/services/shell.ts
index b68d64273f..a65079019b 100644
--- a/apps/desktop/src/services/shell.ts
+++ b/apps/desktop/src/services/shell.ts
@@ -252,7 +252,8 @@ export function setupShellHandlers() {
electron.ipcMain.handle("open-path", (_event, filePath: string) => {
try {
- const resolved = validateOpenPath(filePath, dataDirs.TRILIUM_DATA_DIR, dataDirs.TMP_DIR, getConfiguredBackupDir());
+ const resolved = validateOpenPath(filePath, dataDirs.TRILIUM_DATA_DIR, dataDirs.TMP_DIR,
+ ...getConfiguredOpenRoots());
return electron.shell.openPath(resolved);
} catch (e) {
getLog().error(`open-path failed: ${coreUtils.safeExtractMessageAndStackFromError(e)}`);
@@ -265,7 +266,7 @@ export function setupShellHandlers() {
electron.ipcMain.on("show-item-in-folder", (_event, filePath: string) => {
try {
const resolved = validateOpenPath(filePath, dataDirs.TRILIUM_DATA_DIR, dataDirs.TMP_DIR,
- getConfiguredBackupDir(), dataDirs.DOCUMENT_PATH);
+ ...getConfiguredOpenRoots(), dataDirs.DOCUMENT_PATH);
electron.shell.showItemInFolder(resolved);
} catch (e) {
getLog().error(`show-item-in-folder failed: ${coreUtils.safeExtractMessageAndStackFromError(e)}`);
@@ -382,6 +383,14 @@ function shouldOpenViaProtocolHandler(filePath: string): boolean {
}
}
+/**
+ * The roots `open-path` and `show-item-in-folder` accept besides the data and tmp directories:
+ * directories the user can configure outside the data directory.
+ */
+export function getConfiguredOpenRoots() {
+ return [ getConfiguredBackupDir(), dataDirs.RESOURCES_DIR ];
+}
+
/**
* The directory backups currently go to, which the user may have moved outside the data directory.
* Read from the backup service rather than accepted from the renderer, so it stays the app's own