feat(collections): export and import a list as GPX

A list could leave the instance as a TREK file, which only another TREK
reads. Export now asks for a format, and GPX is the second one: the
places as waypoints, for OsmAnd, Organic Maps, a Garmin or gpx.studio.
Import takes a GPX in the same dialog as a list file. Asked for in
discussion #2301.

The GPX is written from the very list file the JSON export returns, so
what may leave the instance is still decided in one place. Name,
description and address, notes, website and category go into GPX's own
elements, where every app shows them. Labels, status, phone and the
rest travel in a TREK extension namespace, so a GPX made by TREK comes
back as the same list. A place without coordinates cannot be a
waypoint; it is left out and the export says how many were.

A GPX is read on the server, sent as text, with the XML library the
trip importer already uses. A document with a DOCTYPE is refused before
parsing, which closes both external entities and entity expansion. Size
is capped like the list file, and the element count is capped before a
tree is built. The reader only turns the GPX into a list file: the
browser previews it and imports it through the existing route, so one
contract, one transaction and one skipped counter cover both formats.
Waypoints and named route points become places; tracks and unnamed
route points are counted in the preview and not imported.

The two list-file routes get a body ceiling of twice the file limit.
The global 100 kB cap refused most real favourites files, and it
already refused list files well under the megabyte the contract allows.
This commit is contained in:
Maurice
2026-09-19 21:01:45 +02:00
parent bb6003684c
commit 80bb06c84f
57 changed files with 2506 additions and 109 deletions
@@ -0,0 +1,44 @@
import {
COLLECTION_GPX_PLACE_FIELDS,
MAX_COLLECTION_FILE_BYTES,
collectionFilePlaceSchema,
collectionGpxReadRequestSchema,
} from './collection-file.schema';
import { describe, expect, it } from 'vitest';
describe('COLLECTION_GPX_PLACE_FIELDS (#2301)', () => {
// The type already refuses a missing key; this is the same promise at runtime,
// for a field that reaches the schema through a spread the type cannot see.
it('gives every field of a place in a list file a home in a GPX waypoint', () => {
const fields = Object.keys(COLLECTION_GPX_PLACE_FIELDS).sort();
expect(fields).toEqual(Object.keys(collectionFilePlaceSchema.shape).sort());
});
it('keeps what GPX has an element for out of the extension', () => {
const native = Object.entries(COLLECTION_GPX_PLACE_FIELDS)
.filter(([, home]) => home === 'waypoint')
.map(([field]) => field);
expect(native.sort()).toEqual(['category', 'description', 'lat', 'lng', 'name', 'notes', 'website']);
});
});
describe('collectionGpxReadRequestSchema (#2301)', () => {
const accepts = (body: unknown) => collectionGpxReadRequestSchema.safeParse(body).success;
it('takes the document as text, with the file name optional', () => {
expect(collectionGpxReadRequestSchema.parse({ gpx: '<gpx/>' })).toEqual({ gpx: '<gpx/>' });
const named = collectionGpxReadRequestSchema.parse({ gpx: '<gpx/>', file_name: 'favourites.gpx' });
expect(named.file_name).toBe('favourites.gpx');
});
it('refuses an empty document and one past the size a list file may have', () => {
expect(accepts({ gpx: '' })).toBe(false);
expect(accepts({ gpx: 'x'.repeat(MAX_COLLECTION_FILE_BYTES + 1) })).toBe(false);
});
it('refuses anything that is not text', () => {
expect(accepts({ gpx: { wpt: [] } })).toBe(false);
expect(accepts({})).toBe(false);
});
});
@@ -48,8 +48,9 @@ import { z } from 'zod';
* ── What an imported file is trusted for ─────────────────────────────────
*
* Nothing. It is a stranger's JSON. It is read in the browser that chose it,
* never uploaded as a file, and what is sent to the server goes through this
* contract on both sides. Text is rendered as text; the two fields that would
* never uploaded as a file (a GPX is, as text, for the reasons given at the
* end), and what is sent to the server goes through this contract on both
* sides. Text is rendered as text; the two fields that would
* otherwise reach a browser API (`website` and a link's `url`) are pinned to
* http(s) by the schemas they borrow, so neither can be a `javascript:` value.
* The counts below are the other half: a file cannot ask the server for more
@@ -188,3 +189,91 @@ export interface CollectionImportResult {
/** Places the file carried that the contract refused. */
skipped: number;
}
/*
* ── The same list as GPX (#2301) ─────────────────────────────────────────
*
* GPX is what the apps a list is usually wanted in already read: OsmAnd,
* Organic Maps, a Garmin, gpx.studio. It is not a second file format with its
* own idea of what may leave. A GPX is written from the list file above, after
* the exporter has decided what travels, so it can only ever carry less.
*
* Each place becomes a waypoint, and a waypoint needs coordinates: a place
* without them is left out and counted, never guessed. What GPX 1.1 has an
* element for goes into that element, so every reader shows it. What it has
* none for travels in TREK's own extension namespace, so a GPX made by TREK
* comes back as the same place rather than a pin with a name.
*
* Reading goes the other way through the same contract: a GPX is turned into
* a list file by the server and imported like one, so nothing a GPX says gets
* past `collectionFilePlaceSchema`. The server reads it, sent as text, because
* XML is where the parser itself is the risk (entities, DTDs, sheer size), and
* the server has one parser, already used for trip GPX, whose limits are its
* own and tested, where a browser's differ by engine.
*/
/** TREK's extension namespace. Versioned in the path; nothing resolves it. */
export const COLLECTION_GPX_NAMESPACE = 'https://liketrek.com/xmlns/gpx/collection/1';
/**
* Where each field of a place goes in a waypoint.
*
* `waypoint` has a GPX element of its own (name, lat/lon, desc, cmt, link,
* type); `extension` travels in the namespace above, one element per field,
* named after the field. Keyed by the place contract, so a field added there
* does not build until somebody decides where it goes in a GPX.
*/
export const COLLECTION_GPX_PLACE_FIELDS = {
name: 'waypoint',
lat: 'waypoint',
lng: 'waypoint',
description: 'waypoint',
notes: 'waypoint',
website: 'waypoint',
category: 'waypoint',
address: 'extension',
phone: 'extension',
status: 'extension',
price: 'extension',
currency: 'extension',
image_url: 'extension',
google_place_id: 'extension',
google_ftid: 'extension',
osm_id: 'extension',
labels: 'extension',
links: 'extension',
} as const satisfies Record<keyof CollectionFilePlace, 'waypoint' | 'extension'>;
/**
* A GPX document to be read into a list file. Sent as text: the file is a
* stranger's XML, and it is parsed where the XML parser and its limits are.
*/
export const collectionGpxReadRequestSchema = z.object({
gpx: z.string().min(1).max(MAX_COLLECTION_FILE_BYTES),
/** The chosen file's name, which names the list when the document does not. */
file_name: z.string().max(255).optional(),
});
export type CollectionGpxReadRequest = z.infer<typeof collectionGpxReadRequestSchema>;
/** Why a GPX was not read, as the `code` of the 4xx that says so. */
export const COLLECTION_GPX_PROBLEMS = ['too-large', 'unreadable', 'not-gpx', 'too-many-places'] as const;
export type CollectionGpxProblem = (typeof COLLECTION_GPX_PROBLEMS)[number];
export interface CollectionGpxReadResult {
/** The list file the GPX amounts to, ready for the import above. */
file: CollectionFile;
/** Waypoints that could not become a place, mostly for want of coordinates. */
skipped: number;
/** Points of tracks and unnamed route points: lines, not places, so not imported. */
track_points: number;
}
export interface CollectionGpxExport {
/** The list's name, for the file name. */
name: string;
gpx: string;
/** Places written as waypoints. */
waypoints: number;
/** Places left out because they have no coordinates. */
omitted: number;
}