Files
spark 9ff98530d9
ci / rust (pull_request) Failing after 37s
ci / typescript (pull_request) Failing after 6m25s
Recognise a delivered game by its federation code, and ship the local copy and the game's people.
A folder the program writes as AAAS x SCP is Águas Santas x Sporting, so the archive check that already runs when the worker starts attaches the official game. Suggestions on a linked game stay inside the two squads. An export can also keep the same file in a local folder named by the day, the game and the destination, and a gallery can pick anyone named in that game.
2026-10-18 19:29:07 +01:00

90 lines
3.8 KiB
TypeScript

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<string, string | number | readonly string[] | undefined> = {};
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;