import { render, type TemplateVars } from "./template.js"; /** * Folder and file names for deliveries (Nextcloud and others). Each client has its own rules, so both are * templates. Values coming from variables are made safe for a path segment; the template's own `/` are the * only folder separators. */ /** Characters that break Nextcloud/WebDAV or Windows paths, plus control characters. */ const UNSAFE = /[\\/:*?"<>|\u0000-\u001f\u007f]/g; export function safeSegment(value: string): string { return value.normalize("NFC").replace(UNSAFE, "-").replace(/\s+/g, " ").replace(/^[.\s]+|[.\s]+$/g, ""); } function safeVars(vars: TemplateVars): TemplateVars { const out: Record = {}; for (const [key, value] of Object.entries(vars)) { if (typeof value === "string") out[key] = safeSegment(value); else if (Array.isArray(value)) out[key] = value.map((v) => safeSegment(v)); else out[key] = value; } return out; } export class PathTemplateError extends Error { constructor( message: string, readonly missing: string[] = [], ) { super(message); } } /** Renders, turning a bad modifier (`{event:xyz}`) into a template error. */ function renderPath(template: string, vars: TemplateVars) { try { return render(template, vars); } catch (err) { throw new PathTemplateError(err instanceof Error ? err.message : String(err)); } } /** Renders a folder template into a normalised absolute path ending in `/`. Missing variables are an error. */ export function renderFolder(template: string, vars: TemplateVars): string { const { text, missing } = renderPath(template, safeVars(vars)); if (missing.length) throw new PathTemplateError(`missing variables: ${missing.join(", ")}`, missing); const segments = text.split("/").map((s) => s.trim()).filter(Boolean); if (segments.some((s) => s === "." || s === "..")) throw new PathTemplateError("path cannot contain . or .."); return `/${segments.join("/")}/`; } /** * Renders a file name. The extension of the original file is kept when the template has none. * `{originalName}` is the camera file name without extension, `{n}` a counter (use `{n:pad3}` to pad). */ export function renderFileName(template: string, vars: TemplateVars, originalFileName: string): string { const dot = originalFileName.lastIndexOf("."); const base = dot > 0 ? originalFileName.slice(0, dot) : originalFileName; const ext = dot > 0 ? originalFileName.slice(dot).toLowerCase() : ""; const { text, missing } = renderPath(template, safeVars({ ...vars, originalName: base })); if (missing.length) throw new PathTemplateError(`missing variables: ${missing.join(", ")}`, missing); const name = safeSegment(text); if (!name) throw new PathTemplateError("file name is empty"); return /\.[a-z0-9]{2,4}$/i.test(name) ? name : name + (ext === ".jpeg" ? ".jpg" : ext); } /** * Subfolders for the local copy of an export: the capture day, the game, and the destination's name * (`20261018 - Águas Santas Milaneza x Sporting CP - Record`). `{destination}` is filled only for that copy. */ export const LOCAL_COPY_FOLDER = "/{iptcdate} - {event} - {destination}/"; /** Example destination rules taken from today's deliveries. */ export const DESTINATION_EXAMPLES = { fpa: { folder: "/FPA/Fotos/{season}/Competições Nacionais/{competitionCode} - {competition}/{dateCompact} - {homeShort} x {awayShort}/", fileName: "PS_{homeShort} x {awayShort}_{n}.jpg", }, liga: { folder: "/LPBetclic {seasonShort}/Jornada {round}/{matchNumber}. {home} x {away}/", fileName: "{originalName}", }, /** A Bola (Ringier SFTP), as in Photo Mechanic: a folder per game, the photographer's renamed originals. */ abola: { folder: "/{event}/", fileName: "PS_{event}_{n:pad3}", }, } as const;