defaults to `<owner home>/fofos`, and stores the FULL path — never the tilde. a tilde means "whoever is asking", and the people asking will not all be the same person: this plugin is grantable, and the site is in one place regardless of who is looking at it. stored as `~/fofos` it would give every user a different answer to a question that has one. THE DEFAULT IS COMPUTED, NOT WRITTEN. no row means the default is worked out on every read, so it follows the machine. writing it in at install freezes whichever home existed on install day, and a restore onto a different machine then points at a directory that is not there while looking deliberately chosen. a saved path is a decision and is stored exactly as given; only its absence is derived. the form says which of the two it is showing, because "not set" reading identically to "set to the default" is how somebody comes to believe they configured something they did not. THE PICKER USES THE PLATFORM'S FILE API — /file-browser/ls and /mkdir — not routes of this plugin's own. they already refuse to leave home, and they already tell a missing directory from a forbidden one, which is a distinction that cost somebody an afternoon and would have been re-guessed here. shipping a second listing and mkdir would be a second filesystem surface to audit for the sake of one form. that API is an execution capability and never grantable, so for anybody but the owner it 403s — said in a sentence rather than shown as an empty tree, because an empty listing is data and must never be how a refusal looks. it also browses the owner's home and nothing above it; a path outside home is still reachable by typing, since the picker fills the field and the FIELD is what saves. one value on screen, one Save, and picking a folder commits nothing by itself. "Use this folder" takes the one that is OPEN rather than one highlighted in the list. that makes looking inside and choosing one gesture, and it is the only way to choose a folder with nothing in it — which a freshly made one always is. a SECOND table, not a column on fofos_admin_connection, whose url and token are both notNull: a root path could not be saved there without also having an HTTP connection, and that connection is being replaced rather than extended. when it goes, its table is a clean drop instead of surgery on columns that outlived it. the rule against tables in this plugin is unbent — what it forbids is the CLIENT'S work, and this row holds a path somebody can retype. needs `bun db:push`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
96 lines
3.8 KiB
TypeScript
96 lines
3.8 KiB
TypeScript
import { isAbsolute, join, normalize } from 'node:path';
|
|
import { db } from 'officerdb/db';
|
|
import { eq } from 'drizzle-orm';
|
|
import { getOwnerHomeDir } from '@@/data-path';
|
|
import { fofosAdminSettings } from '../db/schema';
|
|
|
|
/**
|
|
* Administration's settings — currently one, where the fofos site is on this machine.
|
|
*
|
|
* ── The default is COMPUTED, not written ──
|
|
*
|
|
* No row means `<owner home>/fofos`, worked out on every read. The alternative — writing the default
|
|
* in at install — freezes whatever the home directory was on the day the plugin was installed, and a
|
|
* restore onto a machine with a different owner then points at a directory that does not exist while
|
|
* looking deliberately chosen. Computed, it follows the machine; written, it follows the install.
|
|
*
|
|
* A saved path is a DECISION and is stored exactly as given. Only the absence of one is derived.
|
|
*
|
|
* ── Absolute, always ──
|
|
*
|
|
* `~` is expanded here and never stored. It means "whoever is asking", and the people asking are not
|
|
* all the same person: this plugin is grantable, and the site is in one place regardless of who is
|
|
* looking at it. Storing the tilde would give every user a different answer to a question that has
|
|
* one.
|
|
*/
|
|
|
|
export type SettingsView = {
|
|
/** Absolute. Never null — an unset root reports its default rather than nothing. */
|
|
rootPath: string;
|
|
/** False while the default is standing in, so a form can show it as a suggestion, not a choice. */
|
|
isSet: boolean;
|
|
};
|
|
|
|
const row = async () => {
|
|
const rows = await db.select().from(fofosAdminSettings).limit(1);
|
|
return rows[0] ?? null;
|
|
};
|
|
|
|
/** `<owner home>/fofos`. The owner's, not the caller's — see the note on absoluteness above. */
|
|
export const defaultRootPath = (email: string): string => join(getOwnerHomeDir(email), 'fofos');
|
|
|
|
export async function readSettings(email: string): Promise<SettingsView> {
|
|
const found = await row();
|
|
const stored = found?.rootPath ?? null;
|
|
return { rootPath: stored ?? defaultRootPath(email), isSet: stored !== null };
|
|
}
|
|
|
|
/**
|
|
* Expand `~`, normalise, and refuse anything that is still not absolute.
|
|
*
|
|
* Refuses rather than resolving against a working directory: a relative path here would be resolved
|
|
* against whatever `process.cwd()` happened to be, which is the platform's checkout — a silently wrong
|
|
* answer that only shows up as an empty listing much later.
|
|
*/
|
|
export function normaliseRootPath(input: string, email: string): string {
|
|
const trimmed = input.trim();
|
|
if (!trimmed) throw new Error('A path is required.');
|
|
|
|
const home = getOwnerHomeDir(email);
|
|
const expanded = trimmed === '~' ? home : trimmed.startsWith('~/') ? join(home, trimmed.slice(2)) : trimmed;
|
|
|
|
if (!isAbsolute(expanded)) throw new Error('The root location must be an absolute path.');
|
|
|
|
// No trailing slash, so two spellings of the same directory cannot be stored as different values.
|
|
const cleaned = normalize(expanded).replace(/\/+$/, '');
|
|
return cleaned === '' ? '/' : cleaned;
|
|
}
|
|
|
|
export async function saveRootPath(input: string, email: string): Promise<SettingsView> {
|
|
const rootPath = normaliseRootPath(input, email);
|
|
const existing = await row();
|
|
|
|
if (existing) {
|
|
await db
|
|
.update(fofosAdminSettings)
|
|
.set({ rootPath, updatedAt: new Date() })
|
|
.where(eq(fofosAdminSettings.id, existing.id));
|
|
} else {
|
|
await db.insert(fofosAdminSettings).values({ rootPath });
|
|
}
|
|
|
|
return readSettings(email);
|
|
}
|
|
|
|
/** Back to the computed default. The row stays, so this is a reset rather than a delete. */
|
|
export async function clearRootPath(email: string): Promise<SettingsView> {
|
|
const existing = await row();
|
|
if (existing) {
|
|
await db
|
|
.update(fofosAdminSettings)
|
|
.set({ rootPath: null, updatedAt: new Date() })
|
|
.where(eq(fofosAdminSettings.id, existing.id));
|
|
}
|
|
return readSettings(email);
|
|
}
|