1
0
Fork 0
polaris-task-force/src/lib/modlistPreset.ts
Z8MB1E cc9d3b8073 fix(missions): only accept Arma 3 Launcher preset exports as mod lists
Root cause of the upload failures: the launcher writes an
<?xml version="1.0"?> prologue, Payload sniffs uploads with
fileTypeFromBuffer, and the collection mimeTypes allowlist lacked
application/xml, so every genuine launcher export was rejected with
'Invalid MIME type: application/xml'.

- Add application/xml to the mod-lists upload mimeTypes (the document is
  still stored as text/html; verified end to end).
- New pure parser src/lib/modlistPreset.ts validates the launcher export
  structure (arma:Type=preset meta + data-type=ModContainer rows) and is
  shared by the server action (authoritative) and CreateSideOpDialog
  (instant client-side feedback).
- Plain-text lists are no longer accepted anywhere: dialog accept/hint,
  collection mimeTypes, and the action now agree on HTML presets only.
- Upload name defaults to the preset's own arma:PresetName.
- Tests: tests/int/modlist-preset.int.spec.ts (9 cases).
2026-10-08 13:25:08 -04:00

64 lines
2.9 KiB
TypeScript

/**
* Validation for Arma 3 Launcher preset exports (the only accepted mod list
* format). Pure module (client-safe): the server action treats it as
* authoritative, and CreateSideOpDialog uses it for instant client-side
* feedback before the upload starts.
*
* A launcher export is an HTML document carrying `<meta name="arma:Type"
* content="preset">`, an optional `<meta name="arma:PresetName" ...>`, and one
* `<tr data-type="ModContainer">` row per mod. We validate those structural
* markers rather than the generator/footer strings, which third-party launcher
* clones rewrite.
*/
export type ArmaPresetFailureReason = "notPreset" | "empty";
export type ArmaPresetSummary =
| { ok: true; presetName: string | null; modCount: number }
| { ok: false; reason: ArmaPresetFailureReason };
const META_TAG_RE = /<meta\b[^>]*>/gi;
const NAME_ATTR_RE = /\bname\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
const CONTENT_ATTR_RE = /\bcontent\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
const MOD_ROW_RE = /\bdata-type\s*=\s*(?:"ModContainer"|'ModContainer'|ModContainer)/gi;
function attr(tag: string, attribute: RegExp): string | null {
const match = tag.match(attribute);
if (!match) return null;
return (match[1] ?? match[2] ?? match[3] ?? "").trim();
}
/**
* Inspect a candidate file's text. Returns the preset name and mod count when
* the content matches the launcher export format, otherwise a failure reason:
* - `notPreset`: not an HTML Arma 3 preset export at all (wrong file type).
* - `empty`: a valid preset containing zero mod entries.
*/
export function parseArmaPreset(source: string): ArmaPresetSummary {
const html = source.replace(/^\uFEFF/, "");
if (!/<html\b/i.test(html)) return { ok: false, reason: "notPreset" };
let armaType: string | null = null;
let presetName: string | null = null;
for (const tag of html.match(META_TAG_RE) ?? []) {
const name = attr(tag, NAME_ATTR_RE)?.toLowerCase();
if (name === "arma:type") armaType = attr(tag, CONTENT_ATTR_RE)?.toLowerCase() ?? null;
else if (name === "arma:presetname") presetName = attr(tag, CONTENT_ATTR_RE) || null;
}
if (armaType !== "preset") return { ok: false, reason: "notPreset" };
const modCount = (html.match(MOD_ROW_RE) ?? []).length;
if (modCount === 0) return { ok: false, reason: "empty" };
return { ok: true, presetName, modCount };
}
/** Shared user-facing copy for each failure reason (client + server). */
export function describeArmaPresetFailure(reason: ArmaPresetFailureReason): string {
switch (reason) {
case "notPreset":
return "That file is not an Arma 3 Launcher mod list export. In the launcher, open the MODS tab, use the menu and choose 'Export preset to HTML', then upload that .html file.";
case "empty":
return "That Arma 3 Launcher preset does not contain any mods. Select your mods in the launcher, export again, and re-upload.";
}
}