refactor(server): resolve the data directory in one place

The data and uploads directories were derived independently in about ten
files, each counting `..` hops from its own `__dirname`, and the file
logger used `process.cwd()` instead. Moving a file or starting the server
from another directory quietly moved where secrets, backups or logs went,
and the backup code named `data/travel.db` directly, so with TREK_DB_FILE
set a backup archived and a restore replaced a file the connection never
opened.

`resolveDataPaths()` in app-config now anchors data/, uploads/, backups,
tmp, logs, the JWT secret and encryption key files and the database file
(TREK_DB_FILE when set, else data/travel.db) on the server package root.
Code that runs before the container calls it directly; Nest classes inject
the new `DataPathsService`. Every previous anchor uses it, and the resolved
paths are unchanged for the image and for `npm run dev`.
This commit is contained in:
Maurice
2026-10-08 15:03:23 +02:00
parent c84d0efb1a
commit 5c054863b9
21 changed files with 239 additions and 57 deletions
+12
View File
@@ -10,6 +10,7 @@ env.schema.ts Zod catalog of the whole env surface; fail-fast at boot
derive.ts pure (raw env) → typed namespace functions, exact per-site coercions
env.ts readEnv() live accessor + validateEnvAtBoot()
app-url.ts getAppUrl()/getMcpSafeUrl() instance base-URL resolution
data-paths.ts resolveDataPaths(): data/, uploads/, backups, tmp, logs, key files, DB file
boot-validate.ts side-effect import used by index.ts (validates before other modules load)
parsers.ts shared coercion helpers (boolTrueLoose, numberOr, csvList, …)
```
@@ -53,6 +54,17 @@ tokens, `RuntimeEnvService`) and consumes the SAME derive functions.
rate limits, …) keep freeze-at-import timing, merely sourcing the value from
`readEnv()` at module top. Request-time reads stay request-time.
## data-paths.ts: the one data layout
`resolveDataPaths()` is the only place the server anchors `data/` and
`uploads/` (on `SERVER_ROOT`, the package directory, never on `process.cwd()`)
and names what lives in them: backups, the scratch dir, the logs, the JWT
secret and encryption key files, and the database file (TREK_DB_FILE when set,
else `data/travel.db`). Code that runs before the container exists imports it
directly; Nest classes inject `DataPathsService` (`src/nest/app-config/`),
which resolves the same layout once per built app. Never derive one of these
paths from `__dirname` in a new file: a moved file would quietly move the data.
## app-url.ts — instance base-URL resolution (moved 2026-07-28)
`getAppUrl()` (APP_URL → first ALLOWED_ORIGINS entry → `http://localhost:PORT`,
+62
View File
@@ -0,0 +1,62 @@
import path from 'node:path';
import { readEnv, type AppEnv } from './env';
/**
* `<server>/`, the directory holding package.json. `src/app-config` and
* `dist/app-config` both sit two levels below it, so the anchor is the same
* under vitest, `npm run dev` and the built image.
*/
export const SERVER_ROOT = path.resolve(__dirname, '..', '..');
/**
* Where TREK keeps what it writes. In the image `data/` and `uploads/` are
* symlinks into `/app`, which is why every consumer used to anchor on its own
* `__dirname` and why none of them may use `process.cwd()` instead.
*/
export interface DataPaths {
/** `<server>/data`: the database, the key files, backups, logs and plugin trees. */
readonly dataDir: string;
/** `<server>/uploads`: the local upload categories (files, covers, photos, ...). */
readonly uploadsDir: string;
/** `data/backups`: archives written by the manual and the automatic backup. */
readonly backupsDir: string;
/** `data/tmp`: driver-agnostic scratch space (restore spool, mirror staging). */
readonly tmpDir: string;
/** `data/logs`: the rotating `trek.log`. */
readonly logsDir: string;
/** `data/.jwt_secret`: written on first start and by the admin's JWT rotation. */
readonly jwtSecretFile: string;
/** `data/.encryption_key`: the at-rest key when ENCRYPTION_KEY does not provide it. */
readonly encryptionKeyFile: string;
/**
* The SQLite file a non-test process opens: TREK_DB_FILE when set, else
* `data/travel.db`. Under NODE_ENV=test the connection is `:memory:` instead
* (db/db-path.ts), which is a property of the connection, not of the layout.
*/
readonly dbFile: string;
}
/**
* The data layout, derived from the server root and the live environment.
*
* The only variable involved is TREK_DB_FILE, read through `readEnv()` like
* every other live value, so a test that points it somewhere else is seen on
* the next call. Nothing here touches the disk: creating a directory is the
* caller's decision.
*/
export function resolveDataPaths(
env: { db: Pick<AppEnv['db'], 'trekDbFile'> } = readEnv(),
root: string = SERVER_ROOT,
): DataPaths {
const dataDir = path.join(root, 'data');
return {
dataDir,
uploadsDir: path.join(root, 'uploads'),
backupsDir: path.join(dataDir, 'backups'),
tmpDir: path.join(dataDir, 'tmp'),
logsDir: path.join(dataDir, 'logs'),
jwtSecretFile: path.join(dataDir, '.jwt_secret'),
encryptionKeyFile: path.join(dataDir, '.encryption_key'),
dbFile: env.db.trekDbFile || path.join(dataDir, 'travel.db'),
};
}
+1
View File
@@ -25,3 +25,4 @@ export {
} from './derive';
export * from './parsers';
export { envSchema } from './env.schema';
export { resolveDataPaths, SERVER_ROOT, type DataPaths } from './data-paths';
+2 -4
View File
@@ -1,10 +1,9 @@
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { readEnv } from './app-config';
import { resolveDataPaths } from './app-config/data-paths';
const dataDir = path.resolve(__dirname, '../data');
const jwtSecretFile = path.join(dataDir, '.jwt_secret');
const { dataDir, jwtSecretFile, encryptionKeyFile: encKeyFile } = resolveDataPaths();
// ENCRYPTION_KEY is used to derive at-rest encryption keys for stored secrets
// (API keys, MFA TOTP secrets, SMTP password, OIDC client secret, etc.).
@@ -20,7 +19,6 @@ const jwtSecretFile = path.join(dataDir, '.jwt_secret');
// data/.encryption_key so JWT rotation can never break decryption later.
// 4. Auto-generated — fresh install with none of the above; persisted to
// data/.encryption_key.
const encKeyFile = path.join(dataDir, '.encryption_key');
let _encryptionKey: string = process.env.ENCRYPTION_KEY || '';
// Under test the suite sets its own fixed ENCRYPTION_KEY. Comparing it with, or
+7 -12
View File
@@ -1,4 +1,5 @@
import { readEnv } from '../app-config';
import { resolveDataPaths } from '../app-config/data-paths';
import fs from 'fs';
import path from 'path';
@@ -19,16 +20,10 @@ export function resolveDbPath(): string {
// parallel forks can't race on the same file or share migration state.
if (readEnv().app.isTest) return ':memory:';
const explicit = readEnv().db.trekDbFile;
if (explicit) {
// Explicit DB file (used by the Playwright E2E harness to run against an
// isolated, throwaway database instead of the real data/travel.db).
const dir = path.dirname(explicit);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
return explicit;
}
const dataDir = path.join(__dirname, '../../data');
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
return path.join(dataDir, 'travel.db');
// TREK_DB_FILE (the Playwright E2E harness runs against a throwaway file this
// way) or data/travel.db, both resolved by the one data layout.
const file = resolveDataPaths().dbFile;
const dir = path.dirname(file);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
return file;
}
+2 -2
View File
@@ -2,11 +2,11 @@ import fs from 'fs';
import path from 'path';
import { RequestContext, type EntityManager } from '@mikro-orm/core';
import { readEnv } from '../app-config';
import { resolveDataPaths } from '../app-config/data-paths';
import { DemoRepository, type DemoAdminCredentialsRow, type DemoInstanceKeyRow } from '../db/repositories/DemoRepository';
import { DatabaseConnectionLostError, type DatabaseBackupStrategy } from '../nest/database/database-backup.interface';
const dataDir = path.join(__dirname, '../../data');
const baselinePath = path.join(dataDir, 'travel-baseline.db');
const baselinePath = path.join(resolveDataPaths().dataDir, 'travel-baseline.db');
/**
* The active fork's `EntityManager`, resolved via `RequestContext.getEntityManager()`
+6 -7
View File
@@ -4,7 +4,6 @@ import 'dotenv/config';
// variable aborts before any other module runs its import-time side effects
// (config.ts key resolution, ...).
import './app-config/boot-validate';
import path from 'node:path';
import fs from 'node:fs';
import http from 'node:http';
import type { INestApplication } from '@nestjs/common';
@@ -16,6 +15,10 @@ import type { INestApplication } from '@nestjs/common';
import type * as Bootstrap from './bootstrap';
import type { DatabaseLifecycle } from './nest/database/database-lifecycle.service';
import { getAppUrl, getMcpSafeUrl, readEnv } from './app-config';
import { resolveDataPaths } from './app-config/data-paths';
import { resolveDbPath } from './db/db-path';
// data/tmp is the driver-agnostic global scratch dir (restore-upload spool,
// mirror stream staging) and stays boot-created here. Driver-owned roots — the
// uploads/ category tree and data/backups — are ensured by LocalDriver.init on
@@ -24,12 +27,9 @@ import type { DatabaseLifecycle } from './nest/database/database-lifecycle.servi
// fails loudly at startup, inside app.init(), instead of as a stray 500 on
// first upload. The Dockerfile `mkdir -p` list is pinned to the registry's
// category prefixes by tests/unit/uploads-dirs.test.ts.
const tmpDir = path.join(__dirname, '../data/tmp');
const { tmpDir } = resolveDataPaths();
if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir, { recursive: true });
import { getAppUrl, getMcpSafeUrl, readEnv } from './app-config';
import { resolveDbPath } from './db/db-path';
const PORT = readEnv().app.port;
const HOST = readEnv().app.host;
const APP_VERSION: string = readEnv().app.appVersion || (require('../package.json') as { version: string }).version;
@@ -130,11 +130,10 @@ async function bootstrap(): Promise<void> {
async function restoreBeforeTheDatabaseOpens() {
const { restoreOnFirstBoot } = require('./nest/backup/boot-restore') as typeof import('./nest/backup/boot-restore');
const env = readEnv();
const dataDir = path.join(__dirname, '../data');
return restoreOnFirstBoot({
archive: env.backup.restoreFromBackup,
dbFile: resolveDbPath(),
dataDir,
dataDir: resolveDataPaths(env).dataDir,
});
}
+3 -4
View File
@@ -3,7 +3,6 @@ import { InjectRepository } from '@mikro-orm/nestjs';
import bcrypt from 'bcryptjs';
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import { ADDON_IDS, MCP_GATED_ADDON_IDS } from '../../addons';
import { readEnv } from '../../app-config';
import { updateJwtSecret } from '../../config';
@@ -23,6 +22,7 @@ import { getPhotoProviderConfig } from '../memories/memories.helpers';
import { validatePassword } from '../common/passwordPolicy';
import { UserCleanupService } from '../auth/user-cleanup.service';
import { UnitOfWork } from '../database/unit-of-work';
import { DataPathsService } from '../app-config/data-paths.service';
import { AddonsService } from '../addons/addons.service';
import { RealtimeService } from '../realtime/realtime.service';
import { PasskeyService } from '../auth/passkey.service';
@@ -130,6 +130,7 @@ export class AdminService {
private readonly realtime: RealtimeService,
private readonly uow: UnitOfWork,
@Inject(DATABASE_BACKUP) private readonly database: DatabaseBackupStrategy,
private readonly dataPaths: DataPathsService,
) {}
// ── User CRUD ──────────────────────────────────────────────────────────────
@@ -777,9 +778,7 @@ export class AdminService {
rotateJwtSecret(): { error?: string; status?: number } {
const newSecret = crypto.randomBytes(32).toString('hex');
// Re-anchored one directory deeper for nest/admin/ (was '../../data' in services/).
const dataDir = path.resolve(__dirname, '../../../data');
const secretFile = path.join(dataDir, '.jwt_secret');
const { dataDir, jwtSecretFile: secretFile } = this.dataPaths;
try {
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
fs.writeFileSync(secretFile, newSecret, { mode: 0o600 });
@@ -21,6 +21,7 @@ import { Global, Module } from '@nestjs/common';
import { ConfigModule as NestConfigModule } from '@nestjs/config';
import { BOOT_STABLE_TOKENS } from './tokens';
import { RuntimeEnvService } from './runtime-env.service';
import { DataPathsService } from './data-paths.service';
@Global()
@Module({
@@ -32,7 +33,7 @@ import { RuntimeEnvService } from './runtime-env.service';
load: BOOT_STABLE_TOKENS,
}),
],
providers: [RuntimeEnvService],
exports: [RuntimeEnvService],
providers: [RuntimeEnvService, DataPathsService],
exports: [RuntimeEnvService, DataPathsService],
})
export class AppConfigModule {}
@@ -0,0 +1,36 @@
import { Injectable } from '@nestjs/common';
import { resolveDataPaths, type DataPaths } from '../../app-config/data-paths';
/**
* The data layout for Nest classes: the data directory, uploads, backups,
* scratch space, logs, the key files and the database file.
*
* Code that runs before the container exists (config.ts key resolution, the
* database connection, index.ts, the file logger) calls `resolveDataPaths()`
* directly; this provider hands the same answer to everything that can take a
* constructor argument, so neither side counts `..` hops from its own file.
* Resolved once per built app: none of these move while a process runs.
*/
@Injectable()
export class DataPathsService implements DataPaths {
readonly dataDir: string;
readonly uploadsDir: string;
readonly backupsDir: string;
readonly tmpDir: string;
readonly logsDir: string;
readonly jwtSecretFile: string;
readonly encryptionKeyFile: string;
readonly dbFile: string;
constructor() {
const paths = resolveDataPaths();
this.dataDir = paths.dataDir;
this.uploadsDir = paths.uploadsDir;
this.backupsDir = paths.backupsDir;
this.tmpDir = paths.tmpDir;
this.logsDir = paths.logsDir;
this.jwtSecretFile = paths.jwtSecretFile;
this.encryptionKeyFile = paths.encryptionKeyFile;
this.dbFile = paths.dbFile;
}
}
+1
View File
@@ -1,5 +1,6 @@
export { AppConfigModule } from './app-config.module';
export { RuntimeEnvService } from './runtime-env.service';
export { DataPathsService } from './data-paths.service';
export {
httpConfig,
sessionConfig,
+4 -1
View File
@@ -1,4 +1,5 @@
import { readEnv } from '../../app-config';
import { resolveDataPaths } from '../../app-config/data-paths';
import fs from 'fs';
import path from 'path';
@@ -30,7 +31,9 @@ const C = {
// ── File logger with rotation ─────────────────────────────────────────────
const logsDir = path.join(process.cwd(), 'data/logs');
// From the data layout, like every other data path. It used to be cwd-based,
// which agreed only because the image and `npm run dev` both start in server/.
const logsDir = resolveDataPaths().logsDir;
const logFilePath = path.join(logsDir, 'trek.log');
let logsDirReady = false;
@@ -2,6 +2,7 @@ import path from 'node:path';
import fs from 'node:fs';
import { logInfo, logError } from '../audit/audit-log.logger';
import type { StorageService } from '../storage/storage.service';
import { resolveDataPaths } from '../../app-config/data-paths';
/**
* Auto-backup settings and retention — the pure half of the auto-backup cron
@@ -13,7 +14,7 @@ import type { StorageService } from '../storage/storage.service';
* replicas too.
*/
const dataDir = path.join(__dirname, '../../../data');
const { dataDir } = resolveDataPaths();
const settingsFile = path.join(dataDir, 'backup-settings.json');
export const VALID_INTERVALS = ['hourly', 'daily', 'weekly', 'monthly'];
+4 -3
View File
@@ -2,6 +2,7 @@ import archiver from 'archiver';
import path from 'path';
import { pipeline } from 'node:stream/promises';
import { readEnv } from '../../app-config';
import { resolveDataPaths } from '../../app-config/data-paths';
import fs from 'fs';
import { logError, logWarn } from '../audit/audit-log.logger';
import type { DatabaseBackupStrategy } from '../database/database-backup.interface';
@@ -33,7 +34,7 @@ const describeError = (err: unknown): string => (err instanceof Error ? err.mess
// The scratch and key directory. The database itself may live elsewhere
// (TREK_DB_FILE); the database port knows where.
const dataDir = path.join(__dirname, '../../../data');
const { dataDir, encryptionKeyFile } = resolveDataPaths();
const PRECOMPRESSED = /\.(jpe?g|png|webp|gif|heic|heif|avif|mp4|mov|m4v|webm|pdf|zip|gz)$/i;
// Compressed upload cap for restore archives. Defaults to 500 MB, raisable via
@@ -281,7 +282,7 @@ export async function createBackup(
// them. NOTE: this makes the backup file as sensitive as the key itself —
// store/transfer it securely. Skipped when ENCRYPTION_KEY is provided via
// env, since in that case the file is not the source of truth.
const encKeyPath = path.join(dataDir, '.encryption_key');
const encKeyPath = encryptionKeyFile;
if (!readEnv().backup.encryptionKeyFromEnv && fs.existsSync(encKeyPath)) {
archive.file(encKeyPath, { name: '.encryption_key' });
}
@@ -438,7 +439,7 @@ export async function restoreFromZip({ storage, database }: BackupDeps, zipPath:
// still overrides the file).
const extractedEncKey = path.join(extractDir, '.encryption_key');
if (fs.existsSync(extractedEncKey)) {
fs.copyFileSync(extractedEncKey, path.join(dataDir, '.encryption_key'));
fs.copyFileSync(extractedEncKey, encryptionKeyFile);
}
} finally {
// The restored DB has different permission-override rows from
+3 -4
View File
@@ -1,5 +1,6 @@
import path from 'path';
import { readEnv } from '../../app-config';
import { resolveDataPaths } from '../../app-config/data-paths';
/**
* File-domain constants and pure helpers, split out of FilesService because
@@ -43,10 +44,8 @@ export const BLOCKED_EXTENSIONS = [
// Executables
'.exe', '.bat', '.sh', '.cmd', '.msi', '.dll', '.com', '.vbs', '.ps1', '.app',
];
// One directory level deeper than the legacy src/services/fileService.ts, so
// the extra '..' keeps the same absolute <server>/uploads/files under both the
// src (vitest) and dist (runtime) layouts.
export const filesDir = path.join(__dirname, '../../../uploads/files');
/** `<server>/uploads/files`, from the one data layout. */
export const filesDir = path.join(resolveDataPaths().uploadsDir, 'files');
/**
* Whether a trip-file upload named `originalname` passes the extension rules:
+2 -1
View File
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { readEnv } from '../../app-config';
import { resolveDataPaths } from '../../app-config/data-paths';
/**
* Filesystem layout for the plugin system (#plugins). Code and data are two
@@ -12,7 +13,7 @@ import { readEnv } from '../../app-config';
* so a Docker deployment can point them at dedicated volumes.
*/
const DATA_ROOT = path.resolve(__dirname, '../../../data');
const DATA_ROOT = resolveDataPaths().dataDir;
// Read lazily so a deployment (or a test) can point these at dedicated volumes
// via env without import-order surprises.
+12 -15
View File
@@ -1,23 +1,20 @@
import path from 'node:path';
import { resolveDataPaths } from '../../app-config/data-paths';
/**
* The storage domain's one `__dirname` anchor (the uploads-root.ts idiom, and
* eventually its replacement: the registry's defaults make this the single
* source of truth the other 16 copies collapse onto in later slices).
*
* `__dirname` moves with the file — `server/{src,dist}/nest/storage` sits
* three levels under `server/`, so three `..` hops resolve to `<server>/`
* under both the src (vitest) and dist (runtime) layouts. The depth is stated
* rather than counted by hand and pinned by storage-keys.test.ts, exactly
* like uploads-root.test.ts pins UPLOADS_ROOT. In Docker both anchors are
* symlinks (`/app/server/uploads → /app/uploads`, `/app/server/data →
* /app/data`), which is why LocalDriver realpaths its root at init.
* The storage domain's view of the data layout. Both roots come from
* `resolveDataPaths()` (src/app-config/data-paths.ts), the one place the server
* anchors `data/` and `uploads/`, so this file no longer counts `..` hops of
* its own. In Docker both are symlinks (`/app/server/uploads -> /app/uploads`,
* `/app/server/data -> /app/data`), which is why LocalDriver realpaths its root
* at init. storage-keys.test.ts pins the resolved values.
*/
export const DEFAULT_UPLOADS_ROOT = path.resolve(__dirname, '..', '..', '..', 'uploads');
export const DATA_ROOT = path.resolve(__dirname, '..', '..', '..', 'data');
export const DEFAULT_BACKUPS_ROOT = path.join(DATA_ROOT, 'backups');
const LAYOUT = resolveDataPaths();
export const DEFAULT_UPLOADS_ROOT = LAYOUT.uploadsDir;
export const DATA_ROOT = LAYOUT.dataDir;
export const DEFAULT_BACKUPS_ROOT = LAYOUT.backupsDir;
/** Driver-agnostic global scratch space (`data/tmp`) — see StorageService.tempDir(). */
export const GLOBAL_TEMP_DIR = path.join(DATA_ROOT, 'tmp');
export const GLOBAL_TEMP_DIR = LAYOUT.tmpDir;
/** Seed-once boot provisioning file — imported only when no storage.* row exists. */
const DEFAULT_SEED_CONFIG_PATH = path.join(DATA_ROOT, 'storage-config.json');