push to maple-street

This commit is contained in:
Tiel Coder
2026-11-20 04:07:02 +00:00
parent 2cec220a24
commit 5adf1cef1f
345 changed files with 86105 additions and 1079 deletions
+131
View File
@@ -0,0 +1,131 @@
/**
* Runtime archive destination settings.
*
* NFS is a mounted filesystem, not an upload API. The app writes to a path that
* must already be visible inside the container; the NFS export is mounted by
* 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.
*/
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: `Archive destination is not available: ${detail}`,
freeBytes: null,
};
}
}