Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(database): ensure local database directory en boot (#294)

Corentin Thomassetb400b3f18ddb
diff --git a/.changeset/large-words-chew.md b/.changeset/large-words-chew.md
new file mode 100644
index 00000000..7ca0b148
--- /dev/null
+++ b/.changeset/large-words-chew.md
@@ -0,0 +1,5 @@
+---
+"@papra/app-server": minor
+---
+
+Ensure local database directory en boot
diff --git a/apps/papra-server/src/index.ts b/apps/papra-server/src/index.ts
index 8cf257e1..a7aa1af6 100644
--- a/apps/papra-server/src/index.ts
+++ b/apps/papra-server/src/index.ts
@@ -2,6 +2,7 @@
import process, { env } from 'node:process';
import { serve } from '@hono/node-server';
import { setupDatabase } from './modules/app/database/database';
+import { ensureLocalDatabaseDirectoryExists } from './modules/app/database/database.services';
import { createServer } from './modules/app/server';
import { parseConfig } from './modules/config/config';
import { createIngestionFolderWatcher } from './modules/ingestion-folders/ingestion-folders.usecases';
@@ -12,6 +13,8 @@ import { taskDefinitions } from './modules/tasks/tasks.defiitions';
const logger = createLogger({ namespace: 'app-server' });
const { config } = await parseConfig({ env });
+
+await ensureLocalDatabaseDirectoryExists({ config });
const { db, client } = setupDatabase(config.database);
const { app } = await createServer({ config, db });
diff --git a/apps/papra-server/src/modules/app/database/database.services.ts b/apps/papra-server/src/modules/app/database/database.services.ts
index 09bc09f0..e1aa7079 100644
--- a/apps/papra-server/src/modules/app/database/database.services.ts
+++ b/apps/papra-server/src/modules/app/database/database.services.ts
@@ -1,10 +1,36 @@
+import type { Config } from '../../config/config.types';
import type { Database } from './database.types';
-import { join } from 'node:path';
+import { dirname, join } from 'node:path';
import { migrate } from 'drizzle-orm/libsql/migrator';
-import { getRootDirPath } from '../../shared/path';
+import { ensureDirectoryExists } from '../../shared/fs/fs.services';
+import { createLogger } from '../../shared/logger/logger';
+import { fileUrlToPath, getRootDirPath } from '../../shared/path';
+
+const logger = createLogger({ namespace: 'database-services' });
export async function runMigrations({ db }: { db: Database }) {
const migrationsFolder = join(getRootDirPath(), 'migrations');
await migrate(db, { migrationsFolder });
}
+
+export async function ensureLocalDatabaseDirectoryExists({ config }: { config: Config }) {
+ const { url } = config.database;
+
+ if (!url.startsWith('file:')) {
+ return;
+ }
+
+ const dbPath = fileUrlToPath({ fileUrl: url });
+ const dbDir = dirname(dbPath);
+
+ try {
+ const { hasBeenCreated } = await ensureDirectoryExists({ path: dbDir });
+
+ if (hasBeenCreated) {
+ logger.info({ dbUrl: url, dbDir, dbPath }, 'Database directory missing, created it');
+ }
+ } catch (error) {
+ logger.error({ error, dbDir, dbPath }, 'Failed to ensure that the database directory exists, error while creating the directory');
+ }
+}
diff --git a/apps/papra-server/src/modules/shared/fs/fs.services.ts b/apps/papra-server/src/modules/shared/fs/fs.services.ts
index dc368868..b26ead3e 100644
--- a/apps/papra-server/src/modules/shared/fs/fs.services.ts
+++ b/apps/papra-server/src/modules/shared/fs/fs.services.ts
@@ -21,8 +21,16 @@ export function createFsServices({ fs = fsNative }: { fs?: FsNative } = {}) {
);
}
-export async function ensureDirectoryExists({ path, fs = fsNative }: { path: string; fs?: FsNative }) {
+export async function ensureDirectoryExists({ path, fs = fsNative }: { path: string; fs?: FsNative }): Promise<{ hasBeenCreated: boolean }> {
+ const exists = await fs.access(path, fs.constants.F_OK).then(() => true).catch(() => false);
+
+ if (exists) {
+ return { hasBeenCreated: false };
+ }
+
await fs.mkdir(path, { recursive: true });
+
+ return { hasBeenCreated: true };
}
export async function checkFileExists({ path, fs = fsNative }: { path: string; fs?: FsNative }) {
diff --git a/apps/papra-server/src/modules/shared/path.test.ts b/apps/papra-server/src/modules/shared/path.test.ts
new file mode 100644
index 00000000..04fd9f9d
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/path.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, test } from 'vitest';
+import { fileUrlToPath } from './path';
+
+describe('path', () => {
+ describe('fileUrlToPath', () => {
+ test('transform a path using file protocol to a path, with support for relative paths', () => {
+ expect(fileUrlToPath({ fileUrl: 'file:./test.txt' })).to.equal('./test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file:/test.txt' })).to.equal('/test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file:test.txt' })).to.equal('test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file:../test.txt' })).to.equal('../test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file://../test.txt' })).to.equal('../test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file:./foo/bar/test.txt' })).to.equal('./foo/bar/test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file:///foo/bar/test.txt' })).to.equal('/foo/bar/test.txt');
+ expect(fileUrlToPath({ fileUrl: 'file://foo/bar/test.txt' })).to.equal('foo/bar/test.txt');
+ });
+
+ test('transform url encoded characters', () => {
+ expect(fileUrlToPath({ fileUrl: 'file:./foo%20bar/test.txt' })).to.equal('./foo bar/test.txt');
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/shared/path.ts b/apps/papra-server/src/modules/shared/path.ts
index c7ef405c..74445c13 100644
--- a/apps/papra-server/src/modules/shared/path.ts
+++ b/apps/papra-server/src/modules/shared/path.ts
@@ -2,3 +2,11 @@ import process from 'node:process';
import { memoize } from 'lodash-es';
export const getRootDirPath = memoize(() => process.cwd());
+
+// Working with libsql, file url can be relative, which is not correct according to RFC 8089, which standardizes the `file` scheme
+// see https://github.com/tursodatabase/libsql-client-ts/blob/ee036574f5c23335c8b2d6d0c0e117cbe14bf376/packages/libsql-core/src/uri.ts
+export function fileUrlToPath({ fileUrl }: { fileUrl: string }) {
+ const rawPath = fileUrl.replace(/^file:(\/\/)?/, '');
+
+ return decodeURIComponent(rawPath);
+}