Files
glenloch-media/src/db/storage-settings.ts
T
bhursey 982f9048e1
CI / build (push) Has been cancelled
fix(media): refuse local disk when a NAS share is configured
A bind-mounted host directory made the app report storage as ready while
media landed on the container host's local filesystem, so the NFS export was
never actually used. mediaStorage() now reads the mount table: a share written
as host:/export is only accepted when the media root is on a network
filesystem, otherwise uploads, edits and filing are blocked with the real
device and the fix. Settings and the Media page show the detected filesystem
and host-mount steps, README section 9 documents the OS-level mount (including
Docker-in-LXC) with docker-compose.nfs.yml as the alternative, and the E2E
suites use path-style shares for local test directories.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-21 00:45:36 +00:00

133 lines
4.3 KiB
TypeScript

/**
* Runtime media-storage settings: where original photos and videos live.
*
* NFS is a mounted filesystem, not an upload API. The app writes to a path that
* must already be visible inside the container; the export is mounted either by
* the Docker daemon (docker-compose.nfs.yml) or on the Docker host and
* bind-mounted in (see docker-compose.yml). The stored `nfsShare` is the
* human-readable export name so operators can confirm which filesystem they are
* writing to, while `archivePath` is the actual mounted path used by the
* archiver. `mediaStorage()` cross-checks the two against the mount table.
*/
import { constants } from "node:fs";
import { access, stat, statfs } from "node:fs/promises";
import { inArray } from "drizzle-orm";
import path from "node:path";
import { db } from "@/db";
import { appSettings } from "@/db/schema";
export interface StorageSettings {
/** e.g. `nas.local:/volume1/church-media`; informational. */
nfsShare: string;
/** Absolute path inside the app container, e.g. `/archive`. */
archivePath: string;
/** Folder created under the mounted export for church archives. */
archiveFolder: string;
}
export interface StorageStatus {
ready: boolean;
message: string;
freeBytes: number | null;
}
const KEY_NFS_SHARE = "nfs_share";
const KEY_ARCHIVE_PATH = "archive_path";
const KEY_ARCHIVE_FOLDER = "archive_folder";
const ALL_KEYS = [KEY_NFS_SHARE, KEY_ARCHIVE_PATH, KEY_ARCHIVE_FOLDER];
const DEFAULT_ARCHIVE_PATH = process.env.ARCHIVE_DIR ?? "/archive";
const DEFAULT_ARCHIVE_FOLDER = process.env.ARCHIVE_FOLDER ?? "Glenloch Media";
export function defaultStorageSettings(): StorageSettings {
return {
nfsShare: process.env.NFS_SHARE ?? "",
archivePath: DEFAULT_ARCHIVE_PATH,
archiveFolder: DEFAULT_ARCHIVE_FOLDER,
};
}
async function readValues(): Promise<Map<string, string>> {
const rows = await db
.select({ key: appSettings.key, value: appSettings.value })
.from(appSettings)
.where(inArray(appSettings.key, ALL_KEYS));
return new Map(rows.map((row) => [row.key, row.value]));
}
export async function getStorageSettings(): Promise<StorageSettings> {
const values = await readValues();
const defaults = defaultStorageSettings();
return {
nfsShare: values.get(KEY_NFS_SHARE) ?? defaults.nfsShare,
archivePath: values.get(KEY_ARCHIVE_PATH) || defaults.archivePath,
archiveFolder: values.get(KEY_ARCHIVE_FOLDER) || defaults.archiveFolder,
};
}
async function upsert(key: string, value: string): Promise<void> {
await db
.insert(appSettings)
.values({ key, value, updatedAt: new Date() })
.onConflictDoUpdate({
target: appSettings.key,
set: { value, updatedAt: new Date() },
});
}
export async function saveStorageSettings(
input: StorageSettings,
): Promise<StorageSettings> {
await upsert(KEY_NFS_SHARE, input.nfsShare.trim());
await upsert(KEY_ARCHIVE_PATH, input.archivePath.trim());
await upsert(KEY_ARCHIVE_FOLDER, input.archiveFolder.trim());
return getStorageSettings();
}
/** Check the mounted destination without creating it on a page render. */
export async function checkArchiveDestination(
settings: StorageSettings,
): Promise<StorageStatus> {
const archivePath = settings.archivePath.trim();
if (!path.isAbsolute(archivePath)) {
return {
ready: false,
message: "Archive path must be an absolute path inside the app container.",
freeBytes: null,
};
}
try {
const info = await stat(archivePath);
if (!info.isDirectory()) {
return {
ready: false,
message: `${archivePath} exists but is not a directory.`,
freeBytes: null,
};
}
await access(archivePath, constants.W_OK);
let freeBytes: number | null = null;
try {
const filesystem = await statfs(archivePath);
freeBytes = Number(filesystem.bavail) * Number(filesystem.bsize);
} catch {
// statfs is not available on every platform; write access is enough.
}
return {
ready: true,
message: `Ready to archive to ${archivePath}.`,
freeBytes,
};
} catch (error) {
const detail = error instanceof Error ? error.message : "not accessible";
return {
ready: false,
message: `Media storage is not available: ${detail}`,
freeBytes: null,
};
}
}